postgresql 索引優化及瞭解

/*PostgreSQL 提供了∶ B-tree(默認),R-tree,Hash 和 GiST索引*/
1、B-tree 索引是一個 Lehman-Yao 高併發 B-tree 的實現
   比如, col LIKE 'foo%' 或者 col ~ '^foo', 而不是 col LIKE '%bar'。
   但是,如果你的服務器不適用 C 區域,那麼你需要用一個特殊的操作符表創建索引來支持模式匹配查詢
   上的索引操作符表 text_pattern_ops,varchar_pattern_ops, bpchar_pattern_ops 和 name_pattern_ops
   分別支持在類型 text,varchar,char 和 name 上的 B-tree 索引
     CREATE INDEX test_index ON test_table (col varchar_pattern_ops);
2、R-tree 索引用 Guttman 的二次分割算法實現了標準的 R-tree,特別適合於空間數據
     CREATE INDEX name ON table USING RTREE(column);
   --下列操作符之一進行比較的時候:<<,&<,&>,>>,@,~=,&&,PostgreSQL會考慮使用 R-tree 索引
3、hash 索引是 Litwin 的線性散列的一個實現
   散列索引只能處理簡單的等於比較(一般不建議使用)
   當一個索引了的列涉及到使用 = 操作符進行比較的時候, 查詢規劃器會考慮使用散列索引。
     CREATE INDEX name ON table USING HASH (column);
    /*注意: 測試表明 PostgreSQL 的散列索引和 B-tree 索引類似或者慢一些,
    而且散列索引的尺寸和製作時間要差很多。而且在高度併發的條件下,
    散列索引的性能也很差。因此,目前我們不建議使用散列索引*/
4、GiST 索引不是單獨一種索引類型,而是一種架構,可以在這種架構上實現很多不同的索引策略。
        因此,可以使用 GiST 索引的特定操作符類型根據索引策略的不同而不同(操作符表)
   GiST 的意思是通用的搜索樹(Generalized Search Tree)
        它是一種平衡的,樹狀結構的訪問方法,在系統中起一個基礎的模版,
        然後可以使用它實現任意索引模式。
        B+-trees,R-trees 和許多其它的索引模式都可以用 GiST 實現。
   GiST 的一個優點是它允許一種客戶化的數據類型和合適的訪問方法一起開發,
        並且是由該數據類型範疇裏的專家,而不是數據庫專家開發

 

Performance Tips

Table of Contents

1、Using EXPLAIN and explain Analyze

2、Statistics Used by the Planner

3、Controllingthe Planner with Explicit JOIN Clauses

4、Populatinga Database

 4.1. Disable Autocommit ,Turn off autocommit and just do one commit at the end

 4.2. Use COPY to load all the rows in one command, instead of using a series of INSERT commands

 4.3. Remove Indexes, the fastest way is to create the table, bulk load the  table's data using COPY, then create any indexes needed for the table

4.4. Increasemaintenance_work_mem

 4.5. Increase checkpoint_segments

 4.6. Run ANALYZE Afterwards, Running ANALYZE (or VACUUM ANALYZE) ensures that the

       plannerhas up-to-date statistics about the table.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章