2017年10月9日 星期一

[SQL] Compare Operator - Heap, Clustered 與 NonClustered Index 比較之間關係

  • [SQL] Compare Operator - 比較基本索引型態成本效率:介紹所有索引的特性,此篇文章再細說 Heap, Clustered 與 NonClustered Index 比較之間關係
    • "NonClustered Index" 的 row locator 就是儲存 
      • "Heap" 的 Row ID (RID) 
      • "Clustered" 的索引 "鍵值資料行"
  • 資料庫使用 Adventure Works for SQL Server 2012 的資料表 Production.Product 產生 2 種不同資料表
    • tbProduct01:NonClustered Index (Name)
    • tbProduct02:Clustered (ProductID), NonClustered Index (Name)
    • 查詢
select ProductID, Name from tbProduct01 where Name = 'Chainring'
select ProductID, Name from tbProduct02 where Name = 'Chainring'
    • 查詢結果
      • 查詢 1. 資料表建立為 "NonClustered Index",而 "NonClustered Index" 的 row locator 指向 "Heap" 的 Row ID (RID)。輸出資料行缺乏 ProductID,因此透過 "RID 查閱" 取得該資料行資料
      • 查詢 2. 依據 Clustered 建立 NonClustered Index,而 "NonClustered Index" 的 row locator 指向 "Clustered" 的 "鍵值資料行" ProductID。輸出資料行 ProductID 與 Name 包括在 "NonClustered Index" 內,因此不需要額外使用 "查閱"
    • Result:
      • 四個基本的 "索引" 可看待四種不同的 "資料表" 型態
        1. Heap
        2. Clustered
        3. NonClustered Index
        4. NonClustered ColumnStore Index
      • NonClustered Index 除了自己的 "鍵值資料行" 之外,row locator 是指向
        1. Row ID 
        2. Clustered 的 "鍵值資料行" (依據資料表有無建立 Clustered)
      • 因此 NonClustered Index 額外包括 
        1. Heap 資料表的 "Row ID"
        2. Clustered 的 "鍵值資料行"
  • REF:
    • SQL Server 效能調校

沒有留言:

張貼留言