Home > other >  Order of execution of join and correlated subquery works in SQL?
Order of execution of join and correlated subquery works in SQL?

Time:05-01

I have 3 tables, Table_A, Table_B, Table_C

Table_A has around 1000 rows,
Table_B has around 10 million rows,
Table_C has around 10 million rows,

Table_B and Table_C has around 7 million common rows

I joined all these three tables as per my requirement as below:

select *from Table_A A Left join Table_B B
on A.id=B.id
where
Not Exists
(select 1 from Table_C C where C.id=B.id )

So my Question is what will be the order of execution,i.e will the Table_B and Table_C be compared first on id and filter out common 7 million rows and then join will be executed with Table_A to filter 1000 rows which not exists in 7 million rows

or

Table_A will join with Table_B and fetch 1000 rows ,and then these 1000 rows of Table_B will be compared with Table_C

CodePudding user response:

The answer is entirely dependant on how your DBMS’s query optimiser decides to execute the query

CodePudding user response:

It depends by the DBMS or you could influencing the optimizer by using the optimizer hints.

  •  Tags:  
  • sql
  • Related