当表数据量越来越大时查询速度会下降,像课本目录一样,在表的条件字段上创建索引,查询时能够快速定位感兴趣的数据所在的位置。索引的好处主要有加速带条件的查询,删除,更新,加速JOIN操作,加速外键约束更新和删除的操作等,但是索引也不是只有好处没有坏处,创建索引时会锁表,不仅将数据写入表中,还要创建索引,因此会在一定程度上影响写的性能。

Btree索引适合处理能够按顺序存储的数据的=,<,>,<=,>=,以及等效这些操作符的其他操作如BETWEEN,IN以及IS NULL和以字符串开头的模糊查询。Btree索引要想起作用where条件必须包含第一个索引列。

测试表:

test=# create table tbl_index(a bigint,b timestamp without time zone,c varchar(12));
CREATE TABLE
test=# insert into tbl_index (a,b,c) select generate_series(1,3000000),clock_timestamp()::timestamp(0) without time zone,'got u';
INSERT 0 3000000
test=# \timing
Timing is on.

示例1.创建索引前查询

test=# select * from tbl_index where a=3000000;
a | b | c
---------+---------------------+-------
3000000 | 2016-06-29 14:54:00 | got u
(1 row) Time: 303.729 ms

创建索引:

test=# create index idx_tbl_index_a on tbl_index using btree(a);
CREATE INDEX
Time: 3743.555 ms

示例2.创建索引后查询

test=# select * from tbl_index where a=3000000;
a | b | c
---------+---------------------+-------
3000000 | 2016-06-29 14:54:00 | got u
(1 row) Time: 2.316 ms

删除索引

test=# drop index idx_tbl_index_a ;
DROP INDEX

创建组合索引

test=# create index idx_tbl_index_a_b on tbl_index using btree(a,b);
CREATE INDEX
Time: 2987.971 ms

使用explain命令输出查询计划,并使用analyze参数实际执行SQL语句。

示例3.按字段a查询

test=# explain analyze select * from tbl_index where a=3000000;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_tbl_index_a_b on tbl_index (cost=0.43..8.45 rows=1 width=22) (actual time=0.026..0.027 rows=1 loops=1)
Index Cond: (a = 3000000)
Planning time: 0.127 ms
Execution time: 0.073 ms
(4 rows) Time: 1.435 ms

示例4.按字段b查询

test=# explain analyze select * from tbl_index where b='2016-06-29 14:54:00';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..38495.00 rows=171000 width=22) (actual time=306.211..514.992 rows=172824 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on tbl_index (cost=0.00..21107.50 rows=71250 width=22) (actual time=284.671..296.463 rows=57608 loops=3)
Filter: (b = '2016-06-29 14:54:00'::timestamp without time zone)
Rows Removed by Filter: 942392
Planning time: 0.191 ms
Execution time: 524.130 ms
(8 rows) Time: 525.623 ms

示例5.组合字段查询 a and b

test=# explain analyze select * from tbl_index where a=3000000 and b='2016-06-29 14:54:00';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_tbl_index_a_b on tbl_index (cost=0.43..8.45 rows=1 width=22) (actual time=0.031..0.033 rows=1 loops=1)
Index Cond: ((a = 3000000) AND (b = '2016-06-29 14:54:00'::timestamp without time zone))
Planning time: 0.169 ms
Execution time: 0.075 ms
(4 rows) Time: 1.173 ms

示例6.组合字段查询 a or b

test=# explain analyze select * from tbl_index where a=3000000 or b='2016-06-29 14:54:00';
QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------
-------------
Bitmap Heap Scan on tbl_index (cost=34034.37..41369.38 rows=171001 width=22) (actual time=130.579..145.787 rows=172824 loops=1)
Recheck Cond: ((a = 3000000) OR (b = '2016-06-29 14:54:00'::timestamp without time zone))
Heap Blocks: exact=276
-> BitmapOr (cost=34034.37..34034.37 rows=171001 width=0) (actual time=130.480..130.480 rows=0 loops=1)
-> Bitmap Index Scan on idx_tbl_index_a_b (cost=0.00..4.44 rows=1 width=0) (actual time=0.026..0.026 rows=1 loops=1)
Index Cond: (a = 3000000)
-> Bitmap Index Scan on idx_tbl_index_a_b (cost=0.00..33944.43 rows=171000 width=0) (actual time=130.452..130.452 rows=172
824 loops=1)
Index Cond: (b = '2016-06-29 14:54:00'::timestamp without time zone)
Planning time: 0.215 ms
Execution time: 153.074 ms
(10 rows) Time: 154.065 ms

表中的索引实际是btree(a,b),从以上示例中可以看出,只有where条件包含索引的第一个字段,查询才会进行索引扫描,否则将进行全表扫描。示例5和示例6比较可知,组合索引字段间使用and和or(测试例使用pg9.6,记得低版本pg组合索引使用or查询会进行全表扫描,这个不知道是不是记错了)虽然都是索引扫描,但是and组合要比or组合查询速度更快。

删除组合索引,然后分别在a和b字段上创建索引

test=# drop index idx_tbl_index_a_b ;
DROP INDEX
Time: 36.017 ms
test=# create index idx_tbl_index_a on tbl_index using btree (a);
CREATE INDEX
Time: 2277.276 ms
test=# create index idx_tbl_index_b on tbl_index using btree (b);
CREATE INDEX
Time: 2278.055 ms

分别使用a and b和a or b进行查询

test=# explain analyze select * from tbl_index where a=3000000 and b='2016-06-29 14:54:00';
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_tbl_index_a on tbl_index (cost=0.43..8.45 rows=1 width=22) (actual time=0.099..0.100 rows=1 loops=1)
Index Cond: (a = 3000000)
Filter: (b = '2016-06-29 14:54:00'::timestamp without time zone)
Planning time: 0.779 ms
Execution time: 0.137 ms
(5 rows) Time: 2.154 ms
test=# explain analyze select * from tbl_index where a=3000000 or b='2016-06-29 14:54:00';
QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------
--------
Bitmap Heap Scan on tbl_index (cost=1840.87..9175.88 rows=171001 width=22) (actual time=29.026..48.537 rows=172824 loops=1)
Recheck Cond: ((a = 3000000) OR (b = '2016-06-29 14:54:00'::timestamp without time zone))
Heap Blocks: exact=276
-> BitmapOr (cost=1840.87..1840.87 rows=171001 width=0) (actual time=28.968..28.968 rows=0 loops=1)
-> Bitmap Index Scan on idx_tbl_index_a (cost=0.00..4.44 rows=1 width=0) (actual time=0.022..0.022 rows=1 loops=1)
Index Cond: (a = 3000000)
-> Bitmap Index Scan on idx_tbl_index_b (cost=0.00..1750.93 rows=171000 width=0) (actual time=28.943..28.943 rows=172824 l
oops=1)
Index Cond: (b = '2016-06-29 14:54:00'::timestamp without time zone)
Planning time: 0.142 ms
Execution time: 57.446 ms
(10 rows) Time: 58.151 ms

结果显示分别在a和b字段上创建索引与在(a,b)组合字段上创建索引相比,and查询性能下降,但是or的性能可以提升。

PS:主键和唯一键会自动创建Btree索引,无需另外单独再为主键和唯一键创建索引。

postgresql----Btree索引的更多相关文章

  1. PostgreSQL内核分析——BTree索引

    文中附图参考至<PostgreSQL数据库内核分析> (一)概念描述 B+树是一种索引数据结构,其一个特征在于非叶子节点用于描述索引,而叶子节点指向具体的数据存储位置.在PostgreSQ ...

  2. PostGreSQL不同索引类型(btree & hash)的性能问题

    在关系型数据库调优中,查询语句涉及到的索引类型是不得不考虑的一个问题.不同的类型的索引可能会适用不同类型的业务场景.这里我们所说的索引类型指的是访问方法(Access Method),至于从其他维度区 ...

  3. 浅谈PostgreSQL的索引

    1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id i ...

  4. mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)

    没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...

  5. (转)浅谈PostgreSQL的索引

    1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id i ...

  6. 0103MySQL中的B-tree索引 USINGWHERE和USING INDEX同时出现

    转自博客http://www.amogoo.com/article/4 前提1,为了与时俱进,文中数据库环境为MySQL5.6版本2,为了通用,更为了避免造数据的痛苦,文中所涉及表.数据,均来自于My ...

  7. Btree 索引

    Btree 索引 索引是帮助数据库高效获取数据的一种数据结构,通过提取句子主干,就可以得到索引的本质. m-way查找树 如果想了解Btree,需要首先了解m-way数据结构. m-way查找树是是一 ...

  8. B-Tree索引在sqlserver和mysql中的应用

    在谈论数据库性能优化的时候,通常都会提到“索引”,但很多人其实并没有真正理解索引,也没有搞清楚索引为什么就能加快检索速度,以至于在实践中并不能很好的应用索引.事实上,索引是一种廉价而且十分有效的优化手 ...

  9. MySQL的btree索引和hash索引的区别

    Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...

  10. MySQL索引类型 btree索引和hash索引的区别

    来源一 Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 ...

随机推荐

  1. HBase在搜狐内容推荐引擎系统中的应用

    转自:http://www.aboutyun.com/thread-7297-1-1.html Facebook放弃Cassandra之后,对HBase 0.89版本进行了大量稳定性优化,使它真正成为 ...

  2. 关于B/S和C/S模式

    B/S注重的是 1.服务想玩游戏的时候,就出现想看电影的时候,就出现不用下载客户端太麻烦看个电影还要下载一大堆东西,不看了 2.维护升级软件不用一台一台地重新更新,百度一直在更新,但是我们感觉不到 3 ...

  3. c++ 用构造函数

    1.c++ 用构造函数创建一个对象时,对象里的属性应该是全新的不存在把之前的属性留下的问题.之所以debug里会存在上一个变量的信息,应该是debug里的一个bug,与程序无光.

  4. BUILD_BUG_ON

    BUILD_BUG_ON() 在编译时调用,可以提前发现错误,这里利用了一些不常用的特性,当数组个数元素为负时会发生编译器错误,对于位域宽度而言,其为负数时也会发生编译器错误. #define BUI ...

  5. linux shell判断脚本执行时传入了多少个参数,判断脚本参数个数,参数数量

    需求描述: 在写shell脚本的时候,会有这样的需求,脚本在执行的过程中是需要传入参数的,如果没有参数传递给脚本, 就会给出相应的脚本使用方法或者例子,告知,脚本后面应该加什么样的参数.所以呢,这里要 ...

  6. Windows中目录及文件路径太长无法删除的解决方法

    用windows自带的命令解决  win7以上的系统有 robocopy 命令 http://www.jianshu.com/p/95a269951a1b 导致目录太深的原因就是用node中的node ...

  7. 配置Java的jdk环境变量

    1.classpath E:\Java\jdk1..0_20\jre\lib\rt.jar;.;E:\Tomcat\lib; 2.JAVA_HOME E:\Java\jdk1..0_20; 3.Pat ...

  8. Java精选笔记_其他IO流(ObjectInputStream、DataInputStream、PrintStream、标准输入输出流)

    其他IO流 ObjectInputStream和ObjectOutputStream 如果希望永久将对象转为字节数据写入到硬盘上,即对象序列化,可以使用ObjectOutputStream(对象输出流 ...

  9. ios - UILabel全属性

    label是大家在开发过程中使用频率很高的一个用来显示文本信息的控件,但是她所有的属性你都了解吗,下面让我们来 重新认识一下label! 1.创建 CGRect rect = CGRectMake(, ...

  10. Linux nmon 命令

    nmon 是一个资源监控工具,能够监控 Linux 系统资源( cpu,memory,network,disks )的使用情况,常见用法如下: [root@localhost ~]$ yum inst ...