ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-06-01 14:02:29  阅读:146  来源: 互联网

标签:10004 Index rows .. 分区 id2 t1 索引 SQL


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

1、准备数据

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、本地索引的性能

没有提供分区条件时

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)

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

提供分区条件时

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、全局索引的性能

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 解析与执行时间都比本地索引的情景快很多。

 

 

标签:10004,Index,rows,..,分区,id2,t1,索引,SQL
来源: https://www.cnblogs.com/kingbase/p/16334014.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有