微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/postgresql/196855.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐