Imagine I have this SQL query and the table2 is HUGE.
select product_id, count(product_id)
from table1
where table2_ptr_id in (select id
from table2
where author is not null)
Will SQL first execute the subquery and load all the table2 into memory? like if table1 has 10 rows and table2 has 10 million rows will it be better to join first and then filter? Or DB is smart enough to optimize this query as it is written.
authorandidappear in an index only the index data pages will be used. - Panagiotis KanavosEXPLAINandEXPLAIN ANALYZEto learn more about query planning and execution in PostgreSQL. Details for those statements and their output is available in PostgreSQL's documentation. - Pavel Horal