Postgresql 提供了对于分区表 global index 的支持。global index 不仅提供了对于唯一索引功能的改进(无需包含分区键),而且在性能上相比非global index (local index)有很大的提升(无法提供分区条件情况下)。以下举例说明二者在性能方面的差异。

1、准备数据

1
2
3
create table t1(id1 integer,id2 integer,name text) partition by hash(id1) partitions 200;
 
insert into t1 select generate_series(1,10000000),generate_series(1,10000000),repeat('a',500);

2、本地索引的性能

没有提供分区条件时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
create index ind_t1_id2 on t1(id2);
 
test=# \di+ ind_t1_id2
                                List of relations
 Schema |    Name    |       Type        | Owner  | Table |  Size   | Description
--------+------------+-------------------+--------+-------+---------+-------------
 public | ind_t1_id2 | partitioned index | system | t1    | 0 bytes |
(1 row)
 
test=# explain analyze select from t1 where id2=10004;
                                                           QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
 Append  (cost=0.29..1662.50 rows=200 width=512) (actual time=1.324..3.249 rows=1 loops=1)
   ->  Index Scan using t1_p0_id2_idx on t1_p0  (cost=0.29..8.31 rows=1 width=512) (actual time=0.054..0.055 rows=0 loops=1)
         Index Cond: (id2 = 10004)
   ->  Index Scan using t1_p1_id2_idx on t1_p1  (cost=0.29..8.31 rows=1 width=512) (actual time=0.065..0.065 rows=0 loops=1)
         Index Cond: (id2 = 10004)
   ......
   ->  Index Scan using t1_p198_id2_idx on t1_p198  (cost=0.29..8.31 rows=1 width=512) (actual time=0.031..0.031 rows=0 loops=1)
         Index Cond: (id2 = 10004)
   ->  Index Scan using t1_p199_id2_idx on t1_p199  (cost=0.29..8.31 rows=1 width=512) (actual time=0.025..0.025 rows=0 loops=1)
         Index Cond: (id2 = 10004)
 Planning Time: 39.262 ms
 Execution Time: 5.673 ms
(403 rows)

使用非全局索引,并且没有提供分区条件情况下,优化器需要读取所有索引分区及表分区的统计数据,才能确定最优的执行计划。对于数据访问,同样需要访问所有分区的索引(即使该分区没有所需要的数据)。

提供分区条件时

1
2
3
4
5
6
7
8
9
test=# explain analyze select from t1 where id2=10004 and id1=10004;
                                                       QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
 Index Scan using t1_p71_id2_idx on t1_p71  (cost=0.29..8.31 rows=1 width=512) (actual time=0.045..0.046 rows=1 loops=1)
   Index Cond: (id2 = 10004)
   Filter: (id1 = 10004)
 Planning Time: 0.346 ms
 Execution Time: 0.064 ms
(5 rows)

在提供分区条件情况下,只需要访问单个索引分区及表分区的统计数据,因此,所需的语句的解析时间更少。

3、全局索引的性能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
create unique index ind_t1_id2 on t1(id2) global;
 
test=# \di+ ind_t1_id2
                             List of relations
 Schema |    Name    |     Type     | Owner  | Table |  Size  | Description
--------+------------+--------------+--------+-------+--------+-------------
 public | ind_t1_id2 | global index | system | t1    | 215 MB |
(1 row)
 
 
test=# explain analyze select from t1 where id2=10004;
                                                        QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
 Global Index Scan using ind_t1_id2 on t1  (cost=0.38..8.39 rows=200 width=512) (actual time=0.136..0.137 rows=1 loops=1)
   Index Cond: (id2 = 10004)
 Planning Time: 9.896 ms
 Execution Time: 0.264 ms
(4 rows)

  

可以SQL 解析与执行时间都比本地索引的情景快很多。

4、DDL操作不会影响全局索引

Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes。

(1)、创建测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create table t1_part(id integer,name text,status char(1))
partition by list(status)
(
  partition p_0 values ('0'),
  partition p_1 values ('1'),
  partition p_default values (default)
);
 
 
insert into t1_part select generate_series(1,100000),repeat('a',500),'0';
insert into t1_part select generate_series(100001,200000),repeat('a',500),'1';
insert into t1_part select generate_series(200001,300000),repeat('a',500),'2';
create unique index ind_t1_part_2 on t1_part(id) global;
analyze t1_part;
 
set enable_globalindexscan = on;

(2)略

truncate partition 不会导致全局索引失效。

Postgresql 对于delete操作,只是在 tuple 上做了个标记,而索引不会进行操作。在通过索引访问tuple时,如果发现数据已经被 Deleted ,也不会报错。因此,对于truncate ,实际就相当于记录被delete。可以看到,在truncate 之后,索引的占用空间没有发生变动,而在 vacuum full ,索引尺寸小了很多。

 

Postgresql 全局索引与分区索引对于SQL性能影响的比较及DDL操作后分区全局索引是否会失效的更多相关文章

  1. 全局索引与分区索引对于SQL性能影响的比较

    KingbaseES 提供了对于分区表 global index 的支持.global index 不仅提供了对于唯一索引功能的改进(无需包含分区键),而且在性能上相比非global index (l ...

  2. SQL Server创建复合索引时,复合索引列顺序对查询的性能影响

    说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因:一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗?二来觉得,索引是个非常大的话题,很难概括 ...

  3. KingbaseES 全局索引是否因为DDL操作而变为Unusable ?

    前言 Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes.KingbaseES 同样支持全局索引.那么,如果对分区表进行DDL操作,那 ...

  4. Oracle DB SQL 性能分析器

    • 确定使用SQL 性能分析器的优点 • 描述SQL 性能分析器工作流阶段 • 使用SQL 性能分析器确定数据库更改所带来的性能改进 SQL 性能分析器:概览 • 11g 的新增功能 • 目标用户:D ...

  5. SQL 性能优化 总结

    SQL 性能优化 总结 (1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving ...

  6. SQL SERVER大话存储结构(2)_非聚集索引如何查找到行记录

              如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持!      1 行记录如何存储     这里引入两个 ...

  7. SQL Server调优系列进阶篇(如何维护数据库索引)

    前言 上一篇我们研究了如何利用索引在数据库里面调优,简要的介绍了索引的原理,更重要的分析了如何选择索引以及索引的利弊项,有兴趣的可以点击查看. 本篇延续上一篇的内容,继续分析索引这块,侧重索引项的日常 ...

  8. SQL Server调优系列玩转篇三(利用索引提示(Hint)引导语句最大优化运行)

    前言 本篇继续玩转模块的内容,关于索引在SQL Server的位置无须多言,本篇将分析如何利用Hint引导语句充分利用索引进行运行,同样,还是希望扎实掌握前面一系列的内容,才进入本模块的内容分析. 闲 ...

  9. 索引 使用use index优化sql查询

    好博客:MySQL http://webnoties.blog.163.com/blog/#m=0&t=1&c=fks_08407108108708107008508508609508 ...

  10. mysql索引合并:一条sql可以使用多个索引

    前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...

随机推荐

  1. 【每日一题】【递归实现、自下而上、优化】-2022年1月12日-NC68 跳台阶

    描述一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果). 数据范围:0 \leq n \leq 400≤n≤40要求:时间复杂度: ...

  2. 互联网最全cka真题解析-2022

    1.CKA真题解析kubectl自动补全及帮助信息1.配置kubectl自动补全apt install bash-completion source <(kubectl completion b ...

  3. v-if v-for同时使用 解决eslint报错问题

    <template v-for="sec in item.goods"> <div v-if="item.showDetail" class= ...

  4. [数学建模]层次分析法AHP

    评价类问题. 问题: ① 评价的目标? ② 可选的方案? ③ 评价的指标? 分层 目标层.准则层.方案层 层次分析法可分为四个步骤建立: 第一步:标度确定和构造判断矩阵: 第二步:特征向量,特征根计算 ...

  5. 在CentOS8中安装gitlab

    安装 docker 及 docker-compose centos8 更新源 cd /etc/yum.repos.d/ sed -i 's/mirrorlist/#mirrorlist/g' /etc ...

  6. 8000字详解Thread Pool Executor

    摘要:Java是如何实现和管理线程池的? 本文分享自华为云社区<JUC线程池: ThreadPoolExecutor详解>,作者:龙哥手记 . 带着大厂的面试问题去理解 提示 请带着这些问 ...

  7. React报错之React.Children.only expected to receive single React element child

    总览 当我们把多个子元素传递给一个只期望有一个React子元素的组件时,会产生"React.Children.only expected to receive single React el ...

  8. 【rabbitmq】单独配置某一个消费者手动ack,其他消费者自动ack

    前言:博主才疏学浅,此方案仅供参考,如有更优方案请大佬评论区告知,十分感谢✿✿ヽ(°▽°)ノ✿ 问题背景:同一个服务中存在多个不同业务的rabbitmq的消费者,其中一个推送业务的消费者需要加死信队列 ...

  9. java下载网络文件的N种方式

    java下载网络文件的N种方式 通过java api下载网络文件的方法有很多,主要方式有以下几种: 1.使用 common-io库下载文件,需要引入commons-io-2.6.jar public ...

  10. NoClassDefFoundError的两种情况

    ClassNotFoundException vs. NoClassDefFoundError ClassNotFoundException 关于ClassNotFoundException发生的原因 ...