2015-10-16 32 views
2

我在一个查询中面临问题。最简单的将是一步一步讲解:SQL Server - 提高在表中搜索值的性能

通过使用这样的查询起初我在colum1 table1中搜索一个具体数值,所以现在

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

查询#1我有一个来自column1的值的列表,该列表发生3次以上。

然后我需要使用在在另一个查询条件清单:

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

当我使用的查询1的子查询不幸的是,执行实在是太慢了,我需要找到一个不同的方式来这。任何建议如何提高绩效?

最好的问候,并感谢您提前

+0

索引可以帮助你在考虑。这里是更多的信息http://stackoverflow.com/questions/2955459/what-is-an-index-in-sql – Coder221

回答

2

如果它们是同一个表,然后使用窗口功能:

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; 

两个这样的一个原始查询会从h其上table1(column1)索引中受益。

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