1

的我有以下查詢其插入另一個表SQL Server查詢性能與大量數據

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分鐘才能執行。執行計劃顯示錶格4的艙口匹配(內部連接)成本爲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 

請幫我看看這個查詢!

+0

當你有在外在WHERE子句中,加入執行作爲常規內部連接外部表的條件加入。移動到ON子句以執行真正的外連接! (其中ar.RuleUsed = 1 ...) – jarlh

+0

謝謝您的回覆!我添加了條件爲ar.ruleused = 1沒有運氣,執行計劃顯示了一個昂貴的孵化內部連接表4 – S12

+0

你可以發佈涉及'table4'的執行計劃分支的快照嗎?根據查詢優化器選擇的計劃,您可以通過在「Table3MerchantDetailId」的表上添加索引來獲得一些改進。包含的列不能用於索引查找,僅用於避免在查詢其他索引列時訪問聚集索引以返回列。您還缺少索引中包含的列的[State],這可以避免您的計劃可能顯示的集羣密鑰查找。 –

回答

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