SQL EXCEPT 性能

编程入门 行业动态 更新时间:2024-10-24 17:21:43
本文介绍了SQL EXCEPT 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用类似于以下查询的查询来查找两个表(DEV 数据库与 TEST 数据库中的同一个表)之间的差异.每个表有 ~30K 行和 ~5 列.

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 是 char(5) 和 field2 是 char(1)

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

这个查询基本上永远不会终止.

This query essentially never terminates.

当我使用 SET SHOWPLAN_ALL ON 分析这个查询时,我可以看到树中有一个相当高的嵌套循环.当我将上述查询更改为

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.

推荐答案

我最好的猜测是优化器在估计两个表的基数(大小)方面做得很差.因为它低估了大小,所以它生成了一个糟糕的查询计划.

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.

在 SQL Server 中,您可以对 except 使用 join 提示.因此,您可以获得所需的查询:

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.

更多推荐

SQL EXCEPT 性能

本文发布于:2023-10-16 05:57:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!