gpt4 book ai didi

sql - 分区表上的 Postgres 查询比非分区表慢 2 倍

转载 作者:行者123 更新时间:2023-11-29 11:48:59 24 4
gpt4 key购买 nike

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

  1. 在普通表上(24 毫秒)
    解释分析 select * from tbl_original where device_info_id = 5;

  2. 在启用分区的表上(49 毫秒)
    解释分析 select * from tbl_partitioned where device_info_id = 5;

以下是 tbl_originalEXPLAIN ANALYZE 命令的输出:

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

以下是 tbl_partitionedEXPLAIN ANALYZE 命令的输出

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

看起来分区查询中最昂贵的操作是索引扫描,占用6251.14 个单位。但是,考虑到分区表与原始表相比的大小,此索引扫描应该非常快。不确定我们是否遗漏了任何明显的东西!

我们将不胜感激对优化查询/分区表的任何帮助。

注意:分区表是使用以下内容创建的:

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

constraint_exclusion 设置为 partition

最佳答案

尝试获取更多解释数据,例如:

explain (ANALYZE, TIMING, COSTS, BUFFERS, VERBOSE) select * from tbl_original where device_info_id = 5;

特别要注意输出中的“命中”,例如:

Buffers: shared hit=4 read=224

Read=xxx 表示必须从磁盘读取一个 block 。 Hit= 表示它来自 RAM(共享缓冲区)。您的更多数据可能在共享缓冲区中——性能在很大程度上取决于此。

关于sql - 分区表上的 Postgres 查询比非分区表慢 2 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53589578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com