不含工会的组成部分的快捷非评估

时间:2018-07-18 06:18:41

标签: sql-server tsql union

让我们假设我有3个表,其中t1,t2,t3列相同,并且运行

select * 
from
  (
    select 't1' as source, * from t1
    union all
    select 't2' as source, * from t2
    union all
    select 't3' as source, * from t3
  ) view_code 
where source='t3'

优化器是否足够聪明,甚至无法运行t1 / t2查询中的选择?

编辑:我运行以下查询:

select * from (
select top 1000 '1' as source, map,pda,item from pt
union all
select top 1000 '2' as source, map,pda,item from gt
)t
where source='2'

当然,在gt上只有索引扫描,而在pt上根本没有读取操作。但是我想确认一下,每次都会这样。

1 个答案:

答案 0 :(得分:3)

是的。签出执行计划-仅扫描t3。

create table t2 (col1 nvarchar(max), col2 nvarchar(max))
create table t3 (col1 nvarchar(max), col2 nvarchar(max))

insert into t1 values ('¿col1?', '¿col2?')
insert into t2 values ('¿col1?', '¿col2?')
insert into t3 values ('¿col1?', '¿col2?')

select * from ( 
select 't1' as source, * from t1 
union all 
select 't2' as source, * from t2 
union all 
select 't3' as source, * from t3 
)view_code where source='t3'

enter image description here