简体   繁体   中英

SQL Server table or subquery for join?

I'm now using a rather cryptic DB (SAP) running on SQL server 2012, only SELECT permission, so some things are difficult to debug/optimize.

I have a big table 'table1', and smaller tables 'table2', 'table3'. The big table with millions of rows will be filtered to 100 rows at most in the "where" statement

I have to start from table1. Would you recommend:

select fields 
from table1
left join table2 
left join table3
where table1.field1 = 'x' 
  and table1.field2 = 'y'

Or

Select 
    fields 
from
    (select fields 
     from table1 
     where table1.field1 = 'x' and table1.field2 = 'y') as t1
left join
    table2
left join
    table3

And, why? I want to understand this a little better.

Thank you!

Ideally

select fields 
from table1
left join table2
left join table3
where table1.field1 = 'x' and table1.field2 = 'y'

This code will first join all the tables and then apply the filter mentioned in the where condition.

Select fields 
from
     (select fields 
      from table1 
      where table1.field1 = 'x' and table1.field2 = 'y') as t1
left join table2
left join table3

Whereas this code will first of all filter the table based on the filter criteria, load it in a temporary location and only then join the filtered rows to the other tables.

If table1 has a lot of rows with very few rows satisfying the filter condition (Low Cardinality) the 2nd code would run faster.

If the filter does not reduce the number of rows much then the bottleneck would be the loading into the temporary space in code 2 due to which code 1 might be better

NOTE: : This answer would change based on your SQL engine and query optimizer. Many optimizers are smart enough to detect that in code 1 only filtered rows need to be joined so in that case code1 would be better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM