在分区表上进行Postgres查询的速度比未分区表慢2倍

时间:2018-12-03 07:55:19

标签: sql postgresql query-performance database-partitioning postgresql-9.6

我们有一个具有400万条记录的表,并为该表创建了分区,假设选择查询在启用分区的表上会更快。但是,启用分区的表的选择速度是原来的2倍!!

  1. 在普通表上( 24毫秒
    explain analyse select * from tbl_original where device_info_id = 5;

  2. 在启用分区的表上( 49毫秒
    explain analyse select * from tbl_partitioned where device_info_id = 5;

以下是EXPLAIN ANALYZE的{​​{1}}命令的输出:

tbl_original

以下是QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Bitmap Heap Scan on tbl_original (cost=61.19..9515.02 rows=2679 width=379) (actual time=0.297..13.008 rows=3369 loops=1) Recheck Cond: (device_info_id = 5) Heap Blocks: exact=554 -> Bitmap Index Scan on idx_tbl_original (cost=0.00..60.52 rows=2679 width=0) (actual time=0.232..0.232 rows=3369 loops=1) Index Cond: (device_info_id = 5) Planning time: 0.087 ms Execution time: 24.890 ms 的{​​{1}}命令的输出

EXPLAIN ANALYZE

分区查询中最昂贵的操作似乎是 Index Scan ,采用 6251.14 个单位。但是,考虑到分区表与原始表相比的大小,该索引扫描应该非常快。不知道我们是否在这里遗漏了任何明显的东西!

在优化查询/分区表方面的任何帮助将不胜感激。

注意:分区表是使用以下方法创建的:

tbl_partitioned

表的大小为:

QUERY PLAN                                                                                                                                                 
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Append  (cost=0.00..6251.14 rows=3697 width=404) (actual time=0.034..36.635 rows=3369 loops=1)                                                             
  ->  Seq Scan on tbl_partitioned  (cost=0.00..0.00 rows=1 width=1069) (actual time=0.006..0.006 rows=0 loops=1)                                           
        Filter: (device_info_id = 5)                                                                                                                       
  ->  Index Scan using idx_tbl_partitioned_p1 on tbl_partitioned_p1  (cost=0.42..6251.14 rows=3696 width=404) (actual time=0.017..12.922 rows=3369 loops=1)
        Index Cond: (device_info_id = 5)                                                                                                                   
Planning time: 0.184 ms                                                                                                                                    
Execution time: 49.129 ms                                                                                                                                  

CREATE TABLE tbl_partitioned (LIKE tbl_original); CREATE TABLE tbl_partitioned_p1 ( CONSTRAINT pk_tbl_partitioned_p1 PRIMARY KEY (id), CONSTRAINT ck_tbl_partitioned_p1 CHECK ( device_info_id < 10 ) ) INHERITS (tbl_partitioned); CREATE INDEX idx_tbl_partitioned_p1 ON tbl_partitioned_p1 (device_info_id); CREATE INDEX idx_tbl_partitioned ON tbl_partitioned (device_info_id); INSERT INTO tbl_partitioned_p1 SELECT * from tbl_original where device_info_id < 10; 设置为select count(*) from tbl_partitioned; -- 413696 rows select count(*) from tbl_original; -- 4417025 rows select count(*) from tbl_original where device_info_id = 5; -- 3369 rows

1 个答案:

答案 0 :(得分:0)

尝试获取更多像这样的解释数据:

  

说明(分析,时序,成本,缓冲,细化),选择*从   tbl_original,其中device_info_id = 5;

尤其要注意ouptut中的“匹配”:

  

缓冲区:共享命中= 4读取= 224

Read = xxx意味着必须从磁盘读取一个块。 Hit =表示它来自RAM(共享缓冲区)。您的更多数据可能位于共享缓冲区中-性能非常依赖于此。