分区表的规划时间在 postgres 11 中花费了更多时间

问题描述

我有不到 200 个分区(每日分区),每个分区有 5M+ 记录。

当我通过直接分区传递一天数据时,我看到估计计划为 0.01 毫秒,但在使用父表时为 190 毫秒(太多)。唯一观察到的区别是在计划中追加。

我们能否在 postgres 11 中消除 Append 或减少 修剪 时间?

查询

explain (ANALYZE,VERBOSE,COSTS,BUFFERS,TIMING,SUMMARY) select 1 from test WHERE date1 >'2021-01-27 13:41:26' and date1<'2021-01-27 21:41:26' and own=123 and mob=123454234

----------------------------plan-----------

Append (cost=0.12..4.19 rows=1 width=4) (actual time=0.018..0.018 rows=0 loops=1) 
  Buffers: shared hit=1 
  -> Index Only Scan using test_20210127_pkey on test_20210127 (cost=0.12..4.17 rows=1 width=4) (actual time=0.017..0.017 rows=0 loops=1) 
     Output: 1 
     Index Cond: ((test_20210127.date1 > '2021-01-27 13:41:26'::timestamp without time zone) AND (test_20210127.date1 < '2021-01-27 21:41:26'::timestamp without time zone) AND (test_20210127.own = 123) AND (test_20210127.mob = 123454234)) 
     Heap Fetches: 0 
     Buffers: shared hit=1 
Planning Time: 190.440 ms 
Execution Time: 0.093 ms

------------截取的表结构----

CREATE TABLE public.test
(
    own integer NOT NULL,mob bigint NOT NULL,date1 timestamp without time zone NOT NULL,ver integer NOT NULL,c5
    ...
    c100
    CONSTRAINT test_pkey PRIMARY KEY (date1,own,mob,ver)
        USING INDEX TABLESPACE tb_1
) PARTITION BY RANGE (date1) 
WITH (
    OIDS = FALSE
)
TABLESPACE tb_1;
 

-- Partitions sql

CREATE TABLE public.test_20211003 PARTITION OF public.test
    FOR VALUES FROM ('2020-10-03 00:00:00') TO ('2020-10-04 00:00:00');

CREATE TABLE public.test_201004 PARTITION OF public.test
    FOR VALUES FROM ('2020-10-04 00:00:00') TO ('2020-10-05 00:00:00');

  ........6 months partitions

解决方法

您可以升级到更高版本的 PostgreSQL,因为 v12 中的性能有所改进。

但是如果查询执行时间很短,计划时间总是占主导地位。您可以测试准备好的语句,但我怀疑运行时分区修剪会快得多。

从本质上讲,最差的查询性能是您为获得丢弃旧数据的简单方法而付出的预期代价。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...