SQL Server 2008升级后,使用UNION的查询运行速度非常慢
我们从 SQL Server 2000 迁移到 2008,当我们使用 UNION 运算符时,某些用户(具有某些数据库角色)的查询运行速度非常慢。我尝试使用 UNION ALL
查询如下所示
SELECT 'PONumber'= '','POId'=''
UNION
SELECT DISTINCT
'PONumber'=PONumber,
'POId'=RTRIM(CONVERT(varchar(32),po.POId) )
FROM PurchaseOrder po JOIN ... JOIN ..... JOIN ...................
如果我只是删除第一部分(和 UNION)并运行第二个查询,结果将立即返回。
有什么建议吗?
We migrated from SQL Server 2000 to 2008 and a query for some users (in certain database roles) runs very slowly when we use UNION operator. I tried it with UNION ALL
The query is like below
SELECT 'PONumber'= '','POId'=''
UNION
SELECT DISTINCT
'PONumber'=PONumber,
'POId'=RTRIM(CONVERT(varchar(32),po.POId) )
FROM PurchaseOrder po JOIN ... JOIN ..... JOIN ...................
If I just remove the first part (and UNION) and run the second query the results are returned immediately.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道它在 SQL 2000 中会有什么反应,但是使用
UNION
而不是UNION ALL
将在两个查询的结果为合并(以消除重复项)。这可能是大量的处理,具体取决于结果集的大小。此外,tempdb 使用情况在 SQL 2000 和 SQL 2008 之间变化非常显着。排序操作可能会使用 tempdb,因此您应该检查 tempdb 的介质并确保它有足够的空间等。
I don't know how it would have reacted in SQL 2000, but using
UNION
instead ofUNION ALL
is going to cause a sort operation when the results of the two queries are merged (to get rid of duplicates). That can be a significant amount of processing, depending on the size of your result set.Also, tempdb usage change pretty dramatically between SQL 2000 and SQL 2008. A sort operation is likely to use tempdb, so you should check the media for your tempdb and make sure that it has plenty of space, etc.