2017年10月11日 星期三

[SQL] Compare Operator - Order by Performance 2

  • 前篇文章 [SQL] Compare Operator - Order by Performance 1,提到排序的技巧
    • 此篇加強使用 "索引" 的 "鍵值資料行" 事先排序的特性,來避免額外 "排序" 成本
  • 資料庫使用 Adventure Works for SQL Server 2012 的資料表 Production.Product 產生 2 種不同資料表
    • tbProduct01:Clustered (ProductID), NonClustered Index (Name)
    • tbProduct02:Clustered (ProductID), NonClustered Index (Name), NonClustered ColumnStore Index (Color, SafetyStockLevel, ReorderPoint) 
    • 查詢
select ProductID, Name, Color, SafetyStockLevel, ReorderPoint from tbProduct01 
where Name like 'Thin-Jam%' order by Name 
select ProductID, Name, Color, SafetyStockLevel, ReorderPoint from tbProduct02 
where Name like 'Thin-Jam%' order by Name 
    • 查詢結果
      • 查詢 2:
        1. NonClustered ColumnStore Index 建立來避免 "查閱" 的產生,而 NonClustered ColumnStore Index 為 "輕量型資料表",所查詢的資料都包含在 NonClustered ColumnStore Index
        2. where 與 order by 符合 "NonClustered Index" 的鍵值資料行,因此使用 "搜尋" 相對查詢 1 的 "掃描" 快上許多
    • Result:
      1. "搜尋" 盡量不要使用 "掃描",所以在條件篩選上避免無法使用 "搜尋" 功能的寫法
      2. "查閱" 是耗費高能本的作業,不論 "RID 查閱" (heap) 或 "Clustered",採用 "NonClustered ColumnStore Index" 進行解決
      3. 盡量不要使用額外 "排序" 功能,需使用 "排序" 盡量採用索引的 "鍵值資料行",因 "鍵值資料行" 是事先排序過
  • REF:
    • SQL Server 效能調校

沒有留言:

張貼留言