Google Search

自訂搜尋

2009年3月7日 星期六

[SQL Tune] 評估 Index 存取方式(Access Methods)

Index 的建立不論是B-tree, Bitmap還是 function-based index, 都是加快資料存取的手法. 簡單的說, Index 有點像是建立資料的捷徑, 而這個捷徑就是RowID.所以 Index存取的目的就是去蒐集RowID, 藉此捷徑快速的取得資料.

常見的Index存取有以下幾個方式

  • Index Range Scan
    這是最常見的存取方式, 從以下SQL可以清楚瞭解.

    select
    employee_name
    from employee
    where Birth_date >sysdate-100;


    Birth_date這個欄位若有建立Index, Oracle透過 B-Tree Index 找到Rowid後, 即可快速找到資料.

  • Fast Full-index Scan
    Full index scan 乍聽之下會不清楚Oracle在搞甚麼? 和 Full Table Scan有何不同? 其實是因為有些SQL只需要Index的資料根本不需要碰到 Table的資料, 像 Count(*), 如以下SQL.

    select distinct country, count(*)
    from employee
    group by country;

    如同 Full-Table Scan, Fast Full-Index Scan也會參考 db_file_multiblock_read_count的參數. 也就是當Fast Full-Index Scan發生時, Oracle也會一次根據此參數設定讀取多個Block.至於 Oracle 是否執行 Fast Full-Index Scan, 前提是 所有在 select 和 where中指定的欄位都 必須存在於 Index中.另外必須有多於 10%回傳的資料是位於Index中, Optimizer才會選擇使 用. 也可以用這個 Hint(/*+ index_ffs() /*)去強迫 Oracle執行Fast Full-Index Scan.
  • Index Full Scan
    通常發生在Optimizer認為SQL回傳的結果會按照Index排序,也就是有 'Order By'的指令. 它會用 Temp space料對Index做排序. 和 Fast Full-Index Scan不同的是, Fast Full-Index Scan是對整個Index做Scan並不會做排序. Index Full Scan使用的是DB Sequential Read而Fast Full-Index Scan使用的是DB Scattered Read.
參考資料
http://www.oracle-training.cc/oracle_tips_index_access.htm
http://www.dbanotes.net/Oracle/Index_full_scan_vs_index_fast_full_scan.htm

沒有留言: