SQL Server查询性能与大量数据

时间:2015-06-12 08:47:26

标签: sql sql-server performance sql-server-2008 database-performance

我有以下查询将结果插入另一个表

Select Distinct * From(                        
select t.RuleId ,t.Table3Id,Null as RiskLeveltypeId,                                                       
(case when r.Count>=t.highlimit  then 60 else                                                                            
case when r.Count>=t.mediumlimit then 30 else                                                         
case when r.Count>=t.lowlimitthen 15 ELSE 0 end end end) as Score                                                     
,CreatedUser,GETDATE() as CreatedDate,CreatedUser as LastActivityUser,GETDATE() as LastActivityDate,                                    
t.Table2Id,
t.Table1Id,
CardId,
249 as ClientId,  
t.StmtDate                                             
from ( (select Table2Id,Table3Date ,COUNT(Distinct Table4.[State]) As Count 
from Table3Data 
join Table4  on Table3Data.Table3MerchantDetailId=Table4.Table3MerchantDetailId                                      
where Table3Data.ClientId=249                                                                                                   
Group By Table2Id,Table3Date  
having COUNT(Distinct Table4.[State])>1 
)r 

join

 (Select ar.CreatedUser,ar.highlimit,ar.mediumlimit,ar.lowlimit, ar.RuleId,                                  
t.Table2Id,ar.RiskLeveltypeId, t.Table3Id,t.Table3date,e.Table1Id,                        
ch.CardId,t.StmtDate  
from Table2sData ch 
    join Table1 e on  e.Table1Id=ch.Table1Id and e.clientid =ch.clientid 
    join Table3Data t on ch.Table2Id=t.Table2Id  and t.ClientId=ch.Clientid and     t.run is null
    left join Table5 ar on e.AuditProfileId=ar.AuditProfileId 
    where ar.RuleUsed=1 and e.AuditProfileId= 205  and ch.CardId  = 1       
    and ar.CardId  = 1   and ar.RuleId=23  and t.StmtDate=CONVERT(varchar,'04/02/2015',112)  and t.run is null  and t.ClientId=249 ) t on r.Table2Id=t.Table2Id                                                            
and r.Table3Date=t.Table3Date) 
)r             where r.Score<>0   

Table3Data有147260条记录,Table2sData有6142条记录。计算状态数的第一个子查询产生270条记录,其中第二个子查询在连接之后(选择限制)产生124619条记录。

此查询大约需要16分钟才能执行。执行计划显示table4的填充匹配(内部联接)的成本为70%。我在table4上已有一个索引,如下所示:

CREATE NONCLUSTERED INDEX IX_1 ON [dbo].table4 
(
    [ClientId] ASC
)
INCLUDE ( [State],
[table3MerchantDetailId]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO 

请帮我解决这个问题!

1 个答案:

答案 0 :(得分:0)

我可以使用以下查询将时间缩短到1秒。我不确定为什么这需要1秒钟而前一个需要16分钟

    select Table2Id,Table3Date ,COUNT(Distinct Table4.[State]) As Count into #temp
    from Table3Data 
    join Table4 on Table3Data.Table3MerchantDetailId=Table4.Table3MerchantDetailId 
    where Table3Data.ClientId=249 
    Group By Table2Id,Table3Date 
    having COUNT(Distinct Table4.[State])>1

 select * from ( Select Distinct * From( 
                                select t.RuleId ,t.Table3Id,Null as RiskLeveltypeId, 
                                (case when r.Count>=t.highlimit then 60 else 
                                case when r.Count>=t.mediumlimit then 30 else 
                                case when r.Count>=t.lowlimit then 15 ELSE 0 end end end) as Score 
                                ,CreatedUser,GETDATE() as CreatedDate,CreatedUser as LastActivityUser,GETDATE() as LastActivityDate, 
                                t.Table2Id,
                                t.Table1Id,
                                CardId,
                                249 as ClientId, 
                                t.StmtDate 
                          from (
                                select  
                                    ar.CreatedUser,
                                    ar.highlimit,ar.mediumlimit,ar.lowlimit, ar.RuleId, 
                                    t.Table2Id,ar.RiskLeveltypeId, t.Table3Id,t.Table3date,e.Table1Id, 
                                    ch.CardId,t.StmtDate 
                                from Table2sData ch 
                                join Table1 e on e.Table1Id=ch.Table1Id and e.clientid =ch.clientid 
                                join Table3Data t on ch.Table2Id=t.Table2Id and t.ClientId=ch.Clientid and t.run is null
                                left join Table5 ar on e.AuditProfileId=ar.AuditProfileId 
                                where 
                                ar.RuleUsed=1 
                                and e.AuditProfileId= 205 
                                and ch.CardId = 1 
                                and ar.CardId = 1 
                                and ar.RuleId=23 
                                and t.StmtDate=CONVERT(varchar,'04/02/2015',112) 
                                and  t.ClientId=249 
                                and exists (select 1 from #temp t1 where t1.Table2Id=t.Table2Id and t1.Table3Date=t.Table3Date) )t

join
(select [Count],Table2Id,Table3Date  from #temp) r on t.Table2Id=r.Table2Id and t.Table3Date=r.Table3Date

)s            where s.Score<>0   



Drop table #temp