Site icon N Jinny

How to swap the sides of a left join in SQL?

I have two tables that I would like to query, tableA has ~53_000 rows while tableB has ~530M rows.

SELECT
    b.some_field AS field,
    a.*
FROM tableA a -- 53_462
LEFT JOIN tableB b -- 527_795_032
ON a.user_id = b.user_id
AND a.numeric_field >= b.numeric_field
AND a.numeric_field <= b.other_numeric_field
LIMIT 10;

This kills the query engine because the right hand side is much bigger than the left, so I think for every row on the left it has to query the right.

In such case (size of right hand side being much bigger than left) what is the best to do:

I am thinking about two possibilities:

Exit mobile version