首页 > 解决方案 > PostgreSQL 11 更高的分区表规划时间

问题描述

我希望在 PostgreSQL 11 中对我的表之一(即 TransactionLog)进行范围分区。
在评估未分区表和分区表之间的查询性能时,我在规划时间上得到了巨大的差异。分区表中的计划时间非常长。
同样,当我通过直接在查询中指定分区名称进行查询时,与我在查询中基于分区表(父表)名称进行查询时相比,计划时间要少得多0.081 毫秒,其中计划时间为6.231 毫秒(示例如下)。

让我知道如何提高分区表的查询性能。

以下是架构

CREATE TABLE TransactionLog (
    txid character varying(36) NOT NULL,
    txnDetails character varying(64),
    loggingtime timestamp(6) without time zone DEFAULT LOCALTIMESTAMP,
) PARTITION BY RANGE(loggingtime);

CREATE TABLE IF NOT EXISTS TransactionLog_20200223 PARTITION OF TransactionLog FOR VALUES FROM ('2020-02-23') TO ('2020-02-24');
CREATE UNIQUE INDEX TransactionLog_20200223_UnqTxId ON TransactionLog_20200223 (txnid);

以下是我直接在分区上查询时的分析结果。计划时间 ~ 0.080 ms(平均 10 次执行)

postgres=> EXPLAIN (ANALYZE,VERBOSE,COSTS,BUFFERS,TIMING,SUMMARY) select txnDetails FROM mra_part.TransactionLog_20200223 WHERE txnid = 'febd139d-1b7f-4564-a004-1b3474e51756';
                                                                             QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Index Scan using TransactionLog_20200223_UnqTxId on TransactionLog_20200223 (cost=0.57..4.61 rows=1 width=10) (actual time=0.039..0.040 rows=1 loops=1)
   Output: txnDetails
   Index Cond: ((TransactionLog_20200223.txnid)::text = 'febd139d-1b7f-4564-a004-1b3474e51756'::text)
   Buffers: shared hit=5
 **Planning Time: 0.081 ms**
 Execution Time: 0.056 ms
(6 rows)

以下是我通过父表查询时的解释分析结果。计划时间 ~ 6.198 ms(平均 10 次执行)

postgres=> EXPLAIN (ANALYZE,VERBOSE,COSTS,BUFFERS,TIMING,SUMMARY)  select txnDetails FROM mtdauthlog WHERE txnid = 'febd139d-1b7f-4564-a004-1b3474e51756' AND loggingtime >= '2020-02-23'::timestamp without time zone AND loggingtime < '2020-02-24'::timestamp without time zone;
                                                                                              QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Append  (cost=0.57..4.62 rows=1 width=10) (actual time=0.036..0.037 rows=1 loops=1)
   Buffers: shared hit=5
   ->  Index Scan using TransactionLog_20200223_UnqTxId on TransactionLog_20200223  (cost=0.57..4.61 rows=1 width=10) (actual time=0.035..0.036 rows=1 loops=1)
         Output: TransactionLog_20200223.txnDetails
         Index Cond: ((TransactionLog_20200223.txnid)::text = 'febd139d-1b7f-4564-a004-1b3474e51756'::text)
         Filter: ((TransactionLog_20200223.loggingtime >= '2020-02-23 00:00:00'::timestamp without time zone) AND (TransactionLog_20200223.loggingtime < '2020-02-24 00:00:00'::timestamp without time zone))
         Buffers: shared hit=5
 **Planning Time: 6.231 ms**
 Execution Time: 0.076 ms
(9 rows)


PostgreSQL 版本:x86_64-pc-linux-gnu 上的 PostgreSQL 11.7,由 gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) 编译,64 位

标签: postgresqlpartitioningplanning

解决方案


推荐阅读