【问题标题】:SQL Server Query Performance with a lot of data具有大量数据的 SQL Server 查询性能
【发布时间】:2015-08-28 05:19:10
【问题描述】:

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

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 

请帮我解决这个问题!

【问题讨论】:

  • 当您在 WHERE 子句中使用外部表条件进行外部联接时,该联接将作为常规内部联接执行。移至 ON 子句以作为真正的外连接执行! (其中 ar.RuleUsed=1...)
  • 感谢您的回复!我添加了 ar.ruleused=1 的 on 条件,但没有成功,执行计划显示表 4 有一个昂贵的舱口内连接
  • 您能否发布您的执行计划分支中涉及table4 的快照?根据查询优化器选择的计划,您可能会通过在Table3MerchantDetailId 上向此表添加索引来获得一些改进。包含的列不能用于索引查找,仅用于避免在其他索引列上查找时访问聚集索引以返回该列。您还缺少索引中包含的列中的[State],这将允许它避免您的计划可能显示的聚集键查找。
  • 谢谢!我在 Table3MerchantDetailId 上添加了一个索引,并在我包含的列中添加了状态,但它仍然显示孵化内连接的 70% 成本,现在索引搜索更改为索引扫描,成本增加了 15%

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


【解决方案1】:

我能够通过以下查询将时间减少到 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

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多