Sql-Server-2012

慢速 SQL Server 查詢

  • December 23, 2019

首先讓我先說我絕不是數據庫專家。我知道足夠危險,但我主要是 ac# 開發人員。我對一個緩慢的視圖進行了查詢,但我在找出根本原因時遇到了問題。我附上了查詢計劃,希望我能在這裡得到一些見解。

https://www.brentozar.com/pastetheplan/?id=H1GT43J8e

這裡也是視圖資訊:

CREATE VIEW [dbo].[ReSyncAdwords]
WITH schemabinding
AS
 SELECT dbo.currentadwordsmaxbid.maxcpc - dervtable.Bid AS Expr1,
        dbo.currentadwordsmaxbid.webid,
        dbo.currentadwordsmaxbid.maxcpc,
        dbo.currentadwordsmaxbid.adgroupid,
        dbo.currentadwordsmaxbid.keywordid,
        dervtable.SKU,
        dervtable.Bid,
        dbo.currentadwordsmaxbid.campaginid
 FROM   dbo.currentadwordsmaxbid
        LEFT OUTER JOIN dbo.WebSkuLookup
                     ON dbo.currentadwordsmaxbid.webid = dbo.WebSkuLookup.webid
        LEFT OUTER JOIN (SELECT SKU,
                                Bid,
                                Channel,
                                DTStamp,
                                campaginid
                         FROM   dbo.BidHistory AS ph1
                         WHERE  ( DTStamp = (SELECT MAX(DTStamp) AS Expr1
                                             FROM   dbo.BidHistory AS ph2
                                             WHERE  ( campaginid = ph1.campaginid )
                                                    AND ( ph1.SKU = SKU )) )) AS dervtable
                     ON dbo.WebSkuLookup.sku = dervtable.SKU
                        AND dbo.currentadwordsmaxbid.campaginid = dervtable.campaginid
 WHERE  ( dbo.currentadwordsmaxbid.maxcpc - dervtable.Bid <> 0 )

GO 

謝謝。

為什麼所有的 ( ) 和長名稱?

為什麼在表名上混合小寫駝峰式大小寫?

只是基本的清理

 SELECT dbo.currentadwordsmaxbid.maxcpc - dervtable.Bid AS Expr1,
        dbo.currentadwordsmaxbid.webid,
        dbo.currentadwordsmaxbid.maxcpc,
        dbo.currentadwordsmaxbid.adgroupid,
        dbo.currentadwordsmaxbid.keywordid,
        dbo.currentadwordsmaxbid.campaginid,
        dervtable.SKU,
        dervtable.Bid
 FROM   dbo.currentadwordsmaxbid
        LEFT OUTER JOIN dbo.WebSkuLookup
                     ON dbo.currentadwordsmaxbid.webid = dbo.WebSkuLookup.webid
        LEFT OUTER JOIN (SELECT SKU, campaginid, Bid
                         FROM   dbo.BidHistory AS ph1
                         WHERE  DTStamp = ( SELECT MAX(DTStamp) AS Expr1
                                            FROM   dbo.BidHistory AS ph2
                                            WHERE  ph1.campaginid = ph2.campaginid
                                            AND    ph1.SKU = ph2.SKU )
                       ) AS dervtable
                     ON dbo.WebSkuLookup.sku = dervtable.SKU
                    AND dbo.currentadwordsmaxbid.campaginid = dervtable.campaginid
                    AND dbo.currentadwordsmaxbid.maxcpc <> dervtable.Bid

行號()

 SELECT dbo.currentadwordsmaxbid.maxcpc - dervtable.Bid AS Expr1,
        dbo.currentadwordsmaxbid.webid,
        dbo.currentadwordsmaxbid.maxcpc,
        dbo.currentadwordsmaxbid.adgroupid,
        dbo.currentadwordsmaxbid.keywordid,
        dbo.currentadwordsmaxbid.campaginid,
        dervtable.SKU,
        dervtable.Bid
 FROM   dbo.currentadwordsmaxbid
        LEFT OUTER JOIN dbo.WebSkuLookup
                     ON dbo.currentadwordsmaxbid.webid = dbo.WebSkuLookup.webid
        LEFT OUTER JOIN (SELECT SKU, campaginid, Bid 
                              , row_number over (partition by SKU, campaginid order by DTStamp desc) as rn
                         FROM   dbo.BidHistory AS ph1
                       ) AS dervtable
                     ON dbo.WebSkuLookup.sku = dervtable.SKU 
                    AND dervtable.rn = 1
                    AND dbo.currentadwordsmaxbid.campaginid = dervtable.campaginid
                    AND dbo.currentadwordsmaxbid.maxcpc <> dervtable.Bid

引用自:https://dba.stackexchange.com/questions/160320