簡體   English   中英

查詢調整和重寫-SQL Server

[英]Query Tuning and rewrite - SQL Server

您能否幫助優化以下查詢以使其表現更好? 我可以降低成本嗎?

SELECT this_.id               AS id1_20_0_, 
       this_.version          AS version2_20_0_, 
       this_.domain           AS domain4_20_0_, 
       this_.createdate       AS createda5_20_0_, 
       this_.lastmodifydate   AS lastmodi6_20_0_, 
       this_.ownerid          AS ownerid7_20_0_, 
       this_.timeperiod       AS timeperi8_20_0_, 
       this_.type             AS type9_20_0_, 
       this_.capturesource    AS capture10_20_0_, 
       this_.value            AS value11_20_0_, 
       this_.siteid           AS siteid12_20_0_, 
       this_.lastmodifyuserid AS lastmod13_20_0_, 
       this_.classid          AS classId3_20_0_ 
FROM   dbo.pcwdepconstraints this_ 
WHERE  this_.classid = 65 
       AND this_.ownerid = 200000000001 
       AND ( this_.capturesource IS NULL 
              OR this_.capturesource IN ( 1073741826, 1073741827, 0, 1, 2 ) ) 

執行計划

我已經在下面的列中重新創建了ix2_pcwdepcons,但是執行計划及其成本仍然沒有變化。

( this_.id , 
       this_.version   , 
       this_.domain ,
       this_.createdate  ,
       this_.lastmodifydate,

       this_.timeperiod  ,
       this_.type  , 
        this_.value   ,       
       this_.siteid       
       this_.lastmodifyuserid )

執行計划表明您既要進行查找(查找基本信息),又要進行“關鍵字查找”,這是一項相當昂貴的操作,應盡量避免。

查看查詢,我發現您在WHERE子句中使用了classidowneridcapturesource ,因此它們需要在索引中。 如果可以,還可以將查詢的SELECT子句中的所有其他列“包括”在該索引中,以避免“鍵查找”。

所以我想嘗試這樣的索引:

CREATE NONCLUSTERED INDEX IX_Test
ON dbo.pcwdepconstraints_bkp (classid, ownerid, capturesource)
INCLUDE (id, version, domain, createdate, lastmodifydate, lastmodifyuserid,
         timeperiod, type, value, siteid)

如果這不是該表中列的全部列表,則包括許多列才有意義。 這些列將占用空間,因此您需要權衡增加的查詢性能和所需的更多磁盤空間。 但是,如果您的表有更多的列,並且您的查詢選擇了“僅”這13個列(因為它確實需要這些列),那么INCLUDE確實可以幫助加快處理速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM