- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
本篇文章將建立多個欄位的索引,並透過篩選不同欄位來比較執行計畫的差異
使用 AdventureWorks2017 作為範例資料庫
並已 Sales.SalesOrderDetail 資料表為基礎透過迴圈重複匯入建立多筆資料列的資料表內容
此文章僅記錄不同查詢的執行計畫結果,並不會描述紀錄內容問題的解決方式
檢視資料表結構與內容
資料表筆數為 1213,1700


建立 ProductID 與 UnitPrice 的非叢集索引,並將 ProductID 順序放在前面

檢視查詢與實際執行計畫
先使用 ProductID 作為篩選條件進行查詢
SELECT TOP(1000) [ProductID]
,[UnitPrice]
FROM [Sales].[SalesOrderDetailForIndex]
WHERE [ProductID] = 775
此查詢的執行計畫如下

使用 ProductID 進行查詢時,會對索引進行索引搜尋 (Index Seek)
接下來使用 UnitPrice 作為篩選條件
SELECT TOP(1000) [ProductID]
,[UnitPrice]
FROM [Sales].[SalesOrderDetailForIndex]
WHERE [UnitPrice] = 843.7475
此查詢的執行計畫如下

雖然 UnitPrice 欄位有建立索引
但執行計畫結果是對索引進行索引掃描 (Index Scan)
花費的時間成本相較索引搜尋 (Index Seek) 之下是較高的
取得全部資料列時
若沒有指定 TOP 關鍵字進行 UnitPrice 欄位篩選時
執行計畫會建議要建立 UnitPrice 欄位的索引內容


參考資料
本文章僅做為紀錄支用並無相關參考內容
留言
張貼留言