当表数据量愈来愈大时查询速度会降低,在表的条件字段上使用索引,快速定位到可能知足条件的记录,不须要遍历全部记录。html
create table t(id int, info text); insert into t select generate_series(1,10000),'lottu'||generate_series(1,10000); create table t1 as select * from t; create table t2 as select * from t; create index ind_t2_id on t2(id);
lottu=# analyze t1; ANALYZE lottu=# analyze t2; ANALYZE # 没有索引 lottu=# explain (analyze,buffers,verbose) select * from t1 where id < 10; QUERY PLAN ----------------------------------------------------------------------------------------------------- Seq Scan on lottu.t1 (cost=0.00..180.00 rows=9 width=13) (actual time=0.073..5.650 rows=9 loops=1) Output: id, info Filter: (t1.id < 10) Rows Removed by Filter: 9991 Buffers: shared hit=55 Planning time: 25.904 ms Execution time: 5.741 ms (7 rows) # 有索引 lottu=# explain (analyze,verbose,buffers) select * from t2 where id < 10; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Index Scan using ind_t2_id on lottu.t2 (cost=0.29..8.44 rows=9 width=13) (actual time=0.008..0.014 rows=9 loops=1) Output: id, info Index Cond: (t2.id < 10) Buffers: shared hit=3 Planning time: 0.400 ms Execution time: 0.052 ms (6 rows)
#在这个案例中:执行同一条SQL。t2有索引的执行数据是0.052 ms;t1没有索引的是:5.741 ms; sql
索引自己就是有序的。数据库
#没有索引 lottu=# explain (analyze,verbose,buffers) select * from t1 where id > 2 order by id; QUERY PLAN ----------------------------------------------------------------------------------------------------------------- Sort (cost=844.31..869.31 rows=9999 width=13) (actual time=8.737..11.995 rows=9998 loops=1) Output: id, info Sort Key: t1.id Sort Method: quicksort Memory: 853kB Buffers: shared hit=55 -> Seq Scan on lottu.t1 (cost=0.00..180.00 rows=9999 width=13) (actual time=0.038..5.133 rows=9998 loops=1) Output: id, info Filter: (t1.id > 2) Rows Removed by Filter: 2 Buffers: shared hit=55 Planning time: 0.116 ms Execution time: 15.205 ms (12 rows) #有索引 lottu=# explain (analyze,verbose,buffers) select * from t2 where id > 2 order by id; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- Index Scan using ind_t2_id on lottu.t2 (cost=0.29..353.27 rows=9999 width=13) (actual time=0.030..5.304 rows=9998 loops=1) Output: id, info Index Cond: (t2.id > 2) Buffers: shared hit=84 Planning time: 0.295 ms Execution time: 7.027 ms (6 rows)
#在这个案例中:执行同一条SQL。express
索引的扫描方式有3种性能优化
先查索引找到匹配记录的ctid,再经过ctid查堆表并发
先查索引找到匹配记录的ctid集合,把ctid经过bitmap作集合运算和排序后再查堆表oracle
若是索引字段中包含了全部返回字段,对可见性映射 (vm)中全为可见的数据块,不查堆表直接返回索引中的值。oop
这里谈谈Indexscan扫描方式和Indexonlyscan扫描方式
对这两种扫描方式区别;借用oracle中索引扫描方式来说;Indexscan扫描方式会产生回表读。根据上面解释来讲;Indexscan扫描方式:查完索引以后还须要查表。 Indexonlyscan扫描方式只须要查索引。也就是说:Indexonlyscan扫描方式要优于Indexscan扫描方式?咱们来看看post
现有表t;在字段id上面建来ind_t_id索引 1. t表没有VM文件。 lottu=# \d+ t Table "lottu.t" Column | Type | Modifiers | Storage | Stats target | Description --------+---------+-----------+----------+--------------+------------- id | integer | | plain | | info | text | | extended | | Indexes: "ind_t_id" btree (id) lottu=# explain (analyze,buffers,verbose) select id from t where id < 10; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Only Scan using ind_t_id on lottu.t (cost=0.29..8.44 rows=9 width=4) (actual time=0.009..0.015 rows=9 loops=1) Output: id Index Cond: (t.id < 10) Heap Fetches: 9 Buffers: shared hit=3 Planning time: 0.177 ms Execution time: 0.050 ms (7 rows) #人为更改执行计划 lottu=# set enable_indexonlyscan = off; SET lottu=# explain (analyze,buffers,verbose) select id from t where id < 10; QUERY PLAN ------------------------------------------------------------------------------------------------------------------ Index Scan using ind_t_id on lottu.t (cost=0.29..8.44 rows=9 width=4) (actual time=0.008..0.014 rows=9 loops=1) Output: id Index Cond: (t.id < 10) Buffers: shared hit=3 Planning time: 0.188 ms Execution time: 0.050 ms (6 rows) # 能够发现二者几乎没有差别;惟一不一样的是Indexonlyscan扫描方式存在扫描的Heap Fetches时间。 这个时间是不在Execution time里面的。 2. t表有VM文件 lottu=# delete from t where id >200 and id < 500; DELETE 299 lottu=# vacuum t; VACUUM lottu=# analyze t; ANALYZE lottu=# explain (analyze,buffers,verbose) select id from t where id < 10; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Only Scan using ind_t_id on lottu.t (cost=0.29..4.44 rows=9 width=4) (actual time=0.008..0.012 rows=9 loops=1) Output: id Index Cond: (t.id < 10) Heap Fetches: 0 Buffers: shared hit=3 Planning time: 0.174 ms Execution time: 0.048 ms (7 rows) lottu=# set enable_indexonlyscan = off; SET lottu=# explain (analyze,buffers,verbose) select id from t where id < 10; QUERY PLAN ------------------------------------------------------------------------------------------------------------------ Index Scan using ind_t_id on lottu.t (cost=0.29..8.44 rows=9 width=4) (actual time=0.012..0.022 rows=9 loops=1) Output: id Index Cond: (t.id < 10) Buffers: shared hit=3 Planning time: 0.179 ms Execution time: 0.077 ms (6 rows)
总结:性能
知识点1:
知识点2:
人为选择执行计划。可设置enable_xxx参数有
参考文献
PostgreSQL 支持索引类型有: B-tree, Hash, GiST, SP-GiST, GIN and BRIN。
建立索引语法:
lottu=# \h create index Command: CREATE INDEX Description: define a new index Syntax: CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] name ] ON table_name [ USING method ] ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ) [ WITH ( storage_parameter = value [, ... ] ) ] [ TABLESPACE tablespace_name ] [ WHERE predicate ] 接下来咱们以t表为例。 1. 关键字【UNIQUE】 #建立惟一索引;主键就是一种惟一索引 CREATE UNIQUE INDEX ind_t_id_1 on t (id); 2. 关键字【CONCURRENTLY】 # 这是并发建立索引。跟oracle的online建立索引做用是同样的。建立索引过程当中;不会阻塞表更新,插入,删除操做。固然建立的时间就会很漫长。 CREATE INDEX CONCURRENTLY ind_t_id_2 on t (id); 3. 关键字【IF NOT EXISTS】 #用该命令是用于确认索引名是否存在。若存在;也不会报错。 CREATE INDEX IF NOT EXISTS ind_t_id_3 on t (id); 4. 关键字【USING】 # 建立哪一种类型的索引。 默认是B-tree。 CREATE INDEX ind_t_id_4 on t using btree (id); 5 关键字【[ ASC | DESC ] [ NULLS { FIRST | LAST]】 # 建立索引是采用降序仍是升序。 若字段存在null值,是把null值放在前面仍是最后:例如采用降序,null放在前面。 CREATE INDEX ind_t_id_5 on t (id desc nulls first) 6. 关键字【WITH ( storage_parameter = value)】 #索引的填充因子设为。例如建立索引的填充因子设为75 CREATE INDEX ind_t_id_6 on t (id) with (fillfactor = 75); 7. 关键字【TABLESPACE】 #是把索引建立在哪一个表空间。 CREATE INDEX ind_t_id_7 on t (id) TABLESPACE tsp_lottu; 8. 关键字【WHERE】 #只在本身感兴趣的那部分数据上建立索引,而不是对每一行数据都建立索引,此种方式建立索引就须要使用WHERE条件了。 CREATE INDEX ind_t_id_8 on t (id) WHERE id < 1000;
修改索引语法
lottu=# \h alter index Command: ALTER INDEX Description: change the definition of an index Syntax: #把索引从新命名 ALTER INDEX [ IF EXISTS ] name RENAME TO new_name #把索引迁移表空间 ALTER INDEX [ IF EXISTS ] name SET TABLESPACE tablespace_name #把索引重设置填充因子 ALTER INDEX [ IF EXISTS ] name SET ( storage_parameter = value [, ... ] ) #把索引的填充因子设置为默认值 ALTER INDEX [ IF EXISTS ] name RESET ( storage_parameter [, ... ] ) #把表空间TSP1中索引迁移到新表空间 ALTER INDEX ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ] SET TABLESPACE new_tablespace [ NOWAIT ]
删除索引语法
lottu=# \h drop index Command: DROP INDEX Description: remove an index Syntax: DROP INDEX [ CONCURRENTLY ] [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
索引能带来加快对表中记录的查询,排序,以及惟一约束的做用。索引也是有代价
select pg_size_pretty(pg_relation_size('ind_t_id'));
--经过pg_stat_user_indexes.idx_scan可检查利用索引进行扫描的次数;这样能够确认那些索引能够清理掉。 select idx_scan from pg_stat_user_indexes where indexrelname = 'ind_t_id';
--若是一个表通过频繁更新以后,索引性能很差;须要重建索引。 lottu=# select pg_size_pretty(pg_relation_size('ind_t_id_1')); pg_size_pretty ---------------- 2200 kB (1 row) lottu=# delete from t where id > 1000; DELETE 99000 lottu=# analyze t; ANALYZE lottu=# select pg_size_pretty(pg_relation_size('ind_t_id_1')); pg_size_pretty ---------------- 2200 kB lottu=# insert into t select generate_series(2000,100000),'lottu'; INSERT 0 98001 lottu=# select pg_size_pretty(pg_relation_size('ind_t_id_1')); pg_size_pretty ---------------- 4336 kB (1 row) lottu=# vacuum full t; VACUUM lottu=# select pg_size_pretty(pg_relation_size('ind_t_id_1')); pg_size_pretty ---------------- 2176 kB 重建方法: 1. reindex:reindex不支持并行重建【CONCURRENTLY】;索引会锁表;会进行阻塞。 2. vacuum full; 对表进行重构;索引也会重建;一样也会锁表。 3. 建立一个新索引(索引名不一样);再删除旧索引。