繁体   English   中英

SQL where子句中联合的替代方法

[英]Alternative for union in SQL where-clause

where子句中,我使用像

SELECT name 
  FROM products 
 WHERE id IN (
           SELECT product_id 
             FROM orders 
            UNION 
           SELECT 1);

进行union需要时间。

请提供其他选择来改善我的查询性能

关于什么

select name from products
where id in (select product_id from orders)
    or id = 1

union删除重复项,并且不幸的是,当在IN谓词中使用union的结果时,SQL Server可能无法发现不需要这种删除。

SELECT name 
  FROM products 
 WHERE id IN (
           SELECT product_id 
             FROM orders 
            UNION ALL
           SELECT 1);

UNION ALL表示允许重复,因此可以避免删除重复项的昂贵步骤。 即使您可能会认为,由于联合的第二部分只有一个值,所以重复检查应该很快,但事实并非如此。 UNION表示必须消除所有重复项。 它也必须从查询的第一部分中消除所有重复的product_id。


我刚刚进行了一些快速测试,发现了一个repro,其中优化程序不够智能,无法避免重复删除,适用于2000、2005、2008版本。所有这3个查询都显示了一个查询计划,该查询计划显示了级联后使用的独特排序在表扫描( #IDs )和恒定扫描之间:

create table #IDs (
    ID int not null
)
go
insert into #IDs (ID)
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 3
go
select * from sysobjects where id in (select ID from #IDs union select 1)
go

使用以下查询。 最有效的。

select name from products P
where P.id = 1 or 
exists 
(select TOP 1 '1' from orders O
where O.product_id = P.id)
select p1.name from products as p1
    inner join orders as o on o.product_id = p1.id
union
select p2.name from products as p2 where p2.id = 1;

减少总的累积CPU时间:9秒900毫秒结束的作业= job_1442292787903_0035启动的MapReduce作业:Stag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM