PostgreSQL在分区表上的规划时间更长

时间:2020-02-23 09:24:42

标签: postgresql partitioning planning

我希望在PostgreSQL 11中对我的表之一(即TransactionLog)进行范围分区。
在评估未分区表和分区表之间的查询性能时,我在计划时间上获得了巨大的不同。分区表中的计划时间非常长。
类似地,当我在查询中直接指定分区名称进行查询时,与基于计划中的分区表(父表)名称进行查询时,规划时间相比 0.081 ms 要少得多6.231 ms (下面的示例)。

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

以下是架构

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毫秒(平均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毫秒(平均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位

0 个答案:

没有答案