【发布时间】:2021-07-25 17:00:46
【问题描述】:
我有一个复杂的查询,它在下面被大大简化,在“aarch64-unknown-linux-gnu 上的 PostgreSQL 11.9 上运行,由 aarch64-unknown-linux-gnu-gcc (GCC) 7.4.0, 64-bit 编译",在 AWS Aurora Serverless 2xlarge 服务器(8 核,64GB RAM)上运行。
我有以下...
mv_journey,一个具有约 5.5 亿行的物化视图,其中包含有关具有起点和目的地的旅程的信息,以及有关这些信息的一些度量(旅程花费了多长时间等),用列定义 @ 987654321@ 和 from_region 标识起点,to_id 和 to_region 标识目的地。
place_from 和 place_to,在 CTE 的初始步骤中从函数 fn_location_get 计算得出,包含 id 和 region (分别映射到from_id、from_region 和to_id、to_region)。这些还包含来自该区域的汇总级别,例如country、continent。通常这些返回约 100 到 20,000 行。
稍后在 CTE 中,我使用 place_from 和 place_to 过滤 550M mv_journey 行,并使用 group by 创建基于旅程的汇总报告,例如从一个国家到另一个国家。
简化的查询是这样的。
WITH place_from AS (
select *
from fn_location_get(...)
), place_to AS (
select *
from fn_location_get(...)
)
select [many dimension columns...]
, [a few aggregated measure columns]
from mv_journey j
inner join place_from o on j.from_id = o.id
and j.from_region = o.region
inner join place_to d on j.from_id = d.id
and j.from_region = d.region
where service_type_id = ?
group by [many dimension columns...]
我在 mv_journey 上有索引
CREATE INDEX idx_mv_journey_from ON mv_journey (from_id, from_region);
CREATE INDEX idx_mv_journey_to ON mv_journey (to_id, to_region);
当我运行查询(使用 SET LOCAL work_mem = '2048MB' 调用快速排序)时,place_from (92) 中有少量行,place_to (~18,000) 中有大量行,查询运行时间约为 25以下查询计划的秒数(其中包括 CTE 中生成 place_from 和 place_to 的步骤)。
"GroupAggregate (cost=530108.64..530129.64 rows=30 width=686) (actual time=13097.187..25408.707 rows=92 loops=1)"
" Group Key: [many dimension columns...]"
" CTE place_from"
" -> Function Scan on fn_location_get (cost=0.25..10.25 rows=1000 width=396) (actual time=34.275..34.331 rows=92 loops=1)"
" CTE place_to"
" -> Function Scan on fn_location_get (cost=0.25..10.25 rows=1000 width=396) (actual time=96.287..97.428 rows=18085 loops=1)"
" -> Sort (cost=530088.14..530088.22 rows=30 width=622) (actual time=12935.329..13295.468 rows=1871349 loops=1)"
" Sort Key: [many dimension columns...]"
" Sort Method: quicksort Memory: 826782kB"
" -> Merge Join (cost=529643.68..530087.41 rows=30 width=622) (actual time=4708.780..6021.449 rows=1871349 loops=1)"
" Merge Cond: ((j.to_id = d.id) AND (j.to_region = d.region))"
" -> Sort (cost=529573.85..529719.16 rows=58124 width=340) (actual time=4583.265..4788.625 rows=1878801 loops=1)"
" Sort Key: j.to_id, j.to_region"
" Sort Method: quicksort Memory: 623260kB"
" -> Nested Loop (cost=0.57..524974.25 rows=58124 width=340) (actual time=34.324..3079.815 rows=1878801 loops=1)"
" -> CTE Scan on place_from o (cost=0.00..20.00 rows=1000 width=320) (actual time=34.277..34.432 rows=92 loops=1)"
" -> Index Scan using idx_mv_journey_from on mv_journey j (cost=0.57..524.37 rows=58 width=60) (actual time=0.018..30.022 rows=20422 loops=92)"
" Index Cond: ((from_id = o.id) AND (from_region = o.region))"
" Filter: (service_type_id = 'ALL'::text)"
" Rows Removed by Filter: 81687"
" -> Sort (cost=69.83..72.33 rows=1000 width=320) (actual time=125.505..223.780 rows=1871350 loops=1)"
" Sort Key: d.id, d.region"
" Sort Method: quicksort Memory: 3329kB"
" -> CTE Scan on place_to d (cost=0.00..20.00 rows=1000 width=320) (actual time=96.292..103.677 rows=18085 loops=1)"
"Planning Time: 0.546 ms"
"Execution Time: 25501.827 ms"
问题是,当我交换 from/to 中的位置时,即place_from (~18,000) 中的大量行和place_to (92) 中的少量行,查询将永远进行。顺便说一句,mv_journey 预计在两种情况下匹配的行数相同 - 一个方向的预期记录不会多于另一方向。
如果没有运行几个小时并且 PGAdmin 4 失去与服务器的连接,我还没有完成第二个查询。因此,我什至无法对其进行EXPLAIN ANALYZE。不过我有EXPLAIN:
"GroupAggregate (cost=474135.40..474152.90 rows=25 width=686)"
" Group Key: [many dimension columns...]"
" CTE place_from"
" -> Function Scan on fn_location_get (cost=0.25..10.25 rows=1000 width=396)"
" CTE place_to"
" -> Function Scan on fn_location_get (cost=0.25..10.25 rows=1000 width=396)"
" -> Sort (cost=474114.90..474114.96 rows=25 width=622)"
" Sort Key: [many dimension columns...]"
" -> Merge Join (cost=473720.23..474114.31 rows=25 width=622)"
" Merge Cond: ((j.to_id = d.id) AND (j.to_region = d.region))## Heading ##"
" -> Sort (cost=473650.40..473779.18 rows=51511 width=340)"
" Sort Key: j.to_id, j.to_region"
" -> Nested Loop (cost=0.57..469619.00 rows=51511 width=340)"
" -> CTE Scan on place_from o (cost=0.00..20.00 rows=1000 width=320)"
" -> Index Scan using idx_mv_journey_from on mv_journey j (cost=0.57..469.08 rows=52 width=60)"
" Index Cond: ((from_id = o.id) AND (from_region = o.region))"
" Filter: (service_type_id = 'ALL'::text)"
" -> Sort (cost=69.83..72.33 rows=1000 width=320)"
" Sort Key: d.id, d.region"
" -> CTE Scan on place_to d (cost=0.00..20.00 rows=1000 width=320)"
我的假设是,如果我在 from/to 的两侧都有等效的索引,那么 Postgres 将使用镜像相反的查询计划,对源进行合并连接,并使用 idx_mv_journey_to 进行嵌套循环连接目的地。
但看起来查询规划器的行数估计值在两个查询中都存在偏差。尽管如此,第一个查询表现如此出色似乎只是运气。
我尝试了以下方法,但都没有成功
- 交换内部连接语句,使目标连接在前
ALTER TABLE mv_journey ALTER COLUMN to_id SET STATISTICS 1000; ANALYZE mv_journeyALTER TABLE mv_journey ALTER COLUMN from_id SET STATISTICS 1000; ANALYZE mv_journey
我猜计划是在 CTE 执行开始之前完成的?这就是为什么它不知道创建place_from 和place_to 集的fn_location_get 调用会产生什么?
fn_location_get 是一个复杂的函数,它有自己的递归 CTE,我不想将它的逻辑从函数中引入到这个 CTE 中。
摆脱这种混乱的最好方法是什么?
【问题讨论】:
标签: postgresql performance query-optimization common-table-expression