SQL Server - 提高在表

时间:2015-10-16 14:42:04

标签: sql sql-server performance

我在一个查询中遇到问题。最简单的是逐步解释:

首先,我使用以下查询在table1中的colum1中搜索特定值:

查询#1:

select column1 
from table1 
where column1 in('xxx','yyy','zzz')
group by column1
having count(*) >3

所以现在我有一个关于column1值的列表,它出现的次数超过3次。

然后我需要在另一个查询中的where条件中使用该列表:

select column1, column2, column3
from table1
where column1 in (query 1)

不幸的是,当我使用查询1作为子查询时,执行速度非常慢,我需要找到一种不同的方法。任何建议如何提高性能?

致以最诚挚的问候,并提前感谢

2 个答案:

答案 0 :(得分:2)

如果它们是同一个表,则使用窗口函数:

"DISPLAY=:0"

这两个原始查询都将受益于h select t.* from (select t.*, count(*) over (partition by column1) as cnt from table1 t where column1 in ('xxx', 'yyy', 'zzz') ) t where cnt > 3; 上的索引。

答案 1 :(得分:0)

1)首先看一下查询是否被正确编入索引。

也许您必须在column1上添加索引。

2)尝试一下:

select column1, column2, column3
from table1 as T1 inner join (
                              select column1, column2, column3
                              from table1 
                              where column1 in (query 1)) as T2
                  on t1.column1 = t2.column1