gpt4 book ai didi

sql-server - 使用 'where then Union' 或使用 'Union then Where'

转载 作者:行者123 更新时间:2023-12-02 10:41:29 25 4
gpt4 key购买 nike

请记住以下两种类型的查询:

--query1
Select someFields
From someTables
Where someWhereClues
Union all
Select someFields
FROM some Tables
Where someWhereClues

--query2
Select * FROM (
Select someFields
From someTables
Union all
Select someFields
FROM someTables
) DT
Where someMixedWhereClues

Note :
In both queries final result fields are same

我以为是第一个。查询速度更快或者性能更好!
但经过一些研究后,我对此感到困惑note :

SQL Server (as a sample of RDBMS) first reads whole data then seek records. => so in both queries all records will read and seek.

请帮助我解决我的误解,以及 query1 和 query2 之间是否还有其他差异?

<小时/>

编辑:添加示例计划:

select t.Name, t.type from sys.tables t where t.type = 'U'
union all
select t.Name, t.type from sys.objects t where t.type = 'U'

select * from (
select t.Name, t.type from sys.tables t
union all
select t.Name, t.type from sys.objects t
) dt
where dt.type = 'U'

执行计划是: enter image description here enter image description here

both are same and 50%

最佳答案

SQL Server query optimizer ,优化两个查询,使您获得几乎相同的性能。

关于sql-server - 使用 'where then Union' 或使用 'Union then Where',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29099113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com