SQL EXCEPT performance

不羁的心 提交于 2019-12-24 02:06:45

问题


I'm trying to use a query similar to the following query to find differences between two tables (the same table in a DEV database vs a TEST database). Each table has ~30K rows and ~5 columns.

select field1,field2,field3,field4,field5 from dev.dbo.table1 
where field1+field2 in ('string1','string2','string3',...,'string50')
except
select field1,field2,field3,field4,field5 from test.dbo.table1 
where field1+field2 in ('string1','string2','string3',...,'string50')

field1 is char(5) and field2 is char(1)

This query essentially never terminates.

When i analyze this query using SET SHOWPLAN_ALL ON, I can see there is a nested loop pretty high in the tree. When I change the above query to

select * from dev.dbo.table1 
except
select * from test.dbo.table2

The query runs quickly and there is no nested loop in the execution plan.

Can someone help explain this? I don't understand why there would be a drastic difference.


回答1:


My best guess is that the optimizer is doing a poor job of estimating the cardinality (size) of the two tables. Because it underestimates the size, it is generating a poor query plan.

In SQL Server, you can use join hints on except. So, you can get the query you want with:

select field1,field2,field3,field4,field5 from dev.dbo.table1 
where field1+field2 in ('string1','string2','string3',...,'string50')
except
select field1,field2,field3,field4,field5 from test.dbo.table1 
where field1+field2 in ('string1','string2','string3',...,'string50')
option (hash join, merge join)

This eliminates the option of the nested loop join, choosing a more favorable method.




回答2:


Your first query is slow because you are concatonating fields, which is essentially a function, in the where clause. This happens pretty much all the time you run functions in a where clause. Here is a simpler example. This will be quick.

where myDateTimeField >= @DateValue
and myDateTimeField < dateadd(day, 1, @DateValue)

This is logically the same, but will be slow

where cast(myDateTimeField as date) = @DateValue


来源:https://stackoverflow.com/questions/16084350/sql-except-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!