1. 程式人生 > 其它 >記一次sqlserver 有關排序導致的慢查詢

記一次sqlserver 有關排序導致的慢查詢

1,執行sql語句,如下,發現執行時間17秒左右

SELECT Id,rn,created FROM (
SELECT
a.[Id] AS [Id] , row_number() OVER ( Order by a.Created desc) AS [rn],
a.created
FROM dbo.Listing (NOLOCK) a
JOIN dbo.ListingSetting (NOLOCK) b ON a.id = b.ListingId
JOIN dbo.ListingVersion (NOLOCK) c ON a.id = c.ListingId
JOIN dbo.OnlineListing (NOLOCK) d ON a.Id = d.ListingId
left join dbo.TimmingListingQueue(NOLOCK) e ON a.id=e.ListingId
left join dbo.EbayPromotionalSale(NOLOCK) f ON a.EbayPromotionalSaleId=f.Id
left join dbo.EbayListingOfferSet(NOLOCK) s on a.id=s.listingId
WHERE a.Enabled = 1
and (a.IsDelete = 0 or a.IsDelete is null) and a.ItemType=0
AND EXISTS(select 1 from dbo.V_TortSku t where t.itemid = a.itemid and t.itemtype = a.itemtype)
) a WHERE 1=1
and a.rn BETWEEN 1 AND 20

2,單獨拆出sql,如下,執行時間為600毫秒左右

SELECT
a.[Id] AS [Id] , row_number() OVER ( Order by a.Created desc) AS [rn],
a.created
FROM dbo.Listing (NOLOCK) a
JOIN dbo.ListingSetting (NOLOCK) b ON a.id = b.ListingId
JOIN dbo.ListingVersion (NOLOCK) c ON a.id = c.ListingId
JOIN dbo.OnlineListing (NOLOCK) d ON a.Id = d.ListingId
left join dbo.TimmingListingQueue(NOLOCK) e ON a.id=e.ListingId
left join dbo.EbayPromotionalSale(NOLOCK) f ON a.EbayPromotionalSaleId=f.Id
left join dbo.EbayListingOfferSet(NOLOCK) s on a.id=s.listingId
WHERE a.Enabled = 1
and (a.IsDelete = 0 or a.IsDelete is null) and a.ItemType=0
AND EXISTS(select 1 from dbo.V_TortSku t where t.itemid = a.itemid and t.itemtype = a.itemtype)
) a WHERE 1=1

3,sql去除分頁條件,如下,執行時間為600毫秒左右

SELECT Id,rn,created FROM (
SELECT
a.[Id] AS [Id] , row_number() OVER ( Order by a.Created desc) AS [rn],
a.created
FROM dbo.Listing (NOLOCK) a
JOIN dbo.ListingSetting (NOLOCK) b ON a.id = b.ListingId
JOIN dbo.ListingVersion (NOLOCK) c ON a.id = c.ListingId
JOIN dbo.OnlineListing (NOLOCK) d ON a.Id = d.ListingId
left join dbo.TimmingListingQueue(NOLOCK) e ON a.id=e.ListingId
left join dbo.EbayPromotionalSale(NOLOCK) f ON a.EbayPromotionalSaleId=f.Id
left join dbo.EbayListingOfferSet(NOLOCK) s on a.id=s.listingId
WHERE a.Enabled = 1
and (a.IsDelete = 0 or a.IsDelete is null) and a.ItemType=0
AND EXISTS(select 1 from dbo.V_TortSku t where t.itemid = a.itemid and t.itemtype = a.itemtype)
) a WHERE 1=1

4,分析後發現雖然以時間進行排序,但是資料庫裡面有很多資料時間是一樣,導致每次排序查出來的資料都不一定一樣

5,修改排序規則,將id加入排序規則,如下,執行時間為600毫秒左右

SELECT Id,rn,created FROM (
SELECT
a.[Id] AS [Id] , row_number() OVER ( Order by a.Created desc,a.id) AS [rn],
a.created
FROM dbo.Listing (NOLOCK) a
JOIN dbo.ListingSetting (NOLOCK) b ON a.id = b.ListingId
JOIN dbo.ListingVersion (NOLOCK) c ON a.id = c.ListingId
JOIN dbo.OnlineListing (NOLOCK) d ON a.Id = d.ListingId
left join dbo.TimmingListingQueue(NOLOCK) e ON a.id=e.ListingId
left join dbo.EbayPromotionalSale(NOLOCK) f ON a.EbayPromotionalSaleId=f.Id
left join dbo.EbayListingOfferSet(NOLOCK) s on a.id=s.listingId
WHERE a.Enabled = 1
and (a.IsDelete = 0 or a.IsDelete is null) and a.ItemType=0
AND EXISTS(select 1 from dbo.V_TortSku t where t.itemid = a.itemid and t.itemtype = a.itemtype)
) a WHERE 1=1
and a.rn BETWEEN 1 AND 20

6,至此,不唯一的排序規則查詢會導致查詢效能下降