【发布时间】:2017-08-15 20:02:43
【问题描述】:
我有一个简单但相当大的表“日志”,它包含三列:user_id、day、hours。
user_id character varying(36) COLLATE pg_catalog."default" NOT NULL,
day timestamp without time zone,
hours double precision
所有列都有索引。
问题是针对“day”字段的聚合工作非常缓慢。例如,简单的查询需要很长时间才能完成。
select min(day) from log where user_id = 'ab056f5a-390b-41d7-ba56-897c14b679bf'
分析表明 Postgres 会进行全面扫描,过滤与 user_id = 'ab056f5a-390b-41d7-ba56-897c14b679bf' 无关的条目,这绝对是违反直觉的
[
{
"Execution Time": 146502.05,
"Planning Time": 0.893,
"Plan": {
"Startup Cost": 789.02,
"Actual Rows": 1,
"Plans": [
{
"Startup Cost": 0.44,
"Actual Rows": 1,
"Plans": [
{
"Index Cond": "(log.day IS NOT NULL)",
"Startup Cost": 0.44,
"Scan Direction": "Forward",
"Plan Width": 8,
"Rows Removed by Index Recheck": 0,
"Actual Rows": 1,
"Node Type": "Index Scan",
"Total Cost": 1395792.54,
"Plan Rows": 1770,
"Relation Name": "log",
"Alias": "log",
"Parallel Aware": false,
"Actual Total Time": 146502.015,
"Output": [
"log.day"
],
"Parent Relationship": "Outer",
"Actual Startup Time": 146502.015,
"Schema": "public",
"Filter": "((log.user_id)::text = 'ab056f5a-390b-41d7-ba56-897c14b679bf'::text)",
"Actual Loops": 1,
"Rows Removed by Filter": 12665610,
"Index Name": "index_log_day"
}
],
"Node Type": "Limit",
"Plan Rows": 1,
"Parallel Aware": false,
"Actual Total Time": 146502.016,
"Output": [
"log.day"
],
"Parent Relationship": "InitPlan",
"Actual Startup Time": 146502.016,
"Plan Width": 8,
"Subplan Name": "InitPlan 1 (returns $0)",
"Actual Loops": 1,
"Total Cost": 789.02
}
],
"Node Type": "Result",
"Plan Rows": 1,
"Parallel Aware": false,
"Actual Total Time": 146502.019,
"Output": [
"$0"
],
"Actual Startup Time": 146502.019,
"Plan Width": 8,
"Actual Loops": 1,
"Total Cost": 789.03
},
"Triggers": []
}
]
更奇怪的是,几乎相似的查询完美地工作。
select min(hours) from log where user_id = 'ab056f5a-390b-41d7-ba56-897c14b679bf'
Postgres 首先选择 user_id = 'ab056f5a-390b-41d7-ba56-897c14b679bf' 的条目,然后在其中汇总明显正确的条目。
[
{
"Execution Time": 5.989,
"Planning Time": 1.186,
"Plan": {
"Partial Mode": "Simple",
"Startup Cost": 6842.66,
"Actual Rows": 1,
"Plans": [
{
"Startup Cost": 66.28,
"Plan Width": 8,
"Rows Removed by Index Recheck": 0,
"Actual Rows": 745,
"Plans": [
{
"Startup Cost": 0,
"Plan Width": 0,
"Actual Rows": 745,
"Node Type": "Bitmap Index Scan",
"Index Cond": "((log.user_id)::text = 'ab056f5a-390b-41d7-ba56-897c14b679bf'::text)",
"Plan Rows": 1770,
"Parallel Aware": false,
"Actual Total Time": 0.25,
"Parent Relationship": "Outer",
"Actual Startup Time": 0.25,
"Total Cost": 65.84,
"Actual Loops": 1,
"Index Name": "index_log_user_id"
}
],
"Recheck Cond": "((log.user_id)::text = 'ab056f5a-390b-41d7-ba56-897c14b679bf'::text)",
"Exact Heap Blocks": 742,
"Node Type": "Bitmap Heap Scan",
"Plan Rows": 1770,
"Relation Name": "log",
"Alias": "log",
"Parallel Aware": false,
"Actual Total Time": 5.793,
"Output": [
"day",
"hours",
"user_id"
],
"Lossy Heap Blocks": 0,
"Parent Relationship": "Outer",
"Actual Startup Time": 0.357,
"Total Cost": 6838.23,
"Actual Loops": 1,
"Schema": "public"
}
],
"Node Type": "Aggregate",
"Strategy": "Plain",
"Plan Rows": 1,
"Parallel Aware": false,
"Actual Total Time": 5.946,
"Output": [
"min(hours)"
],
"Actual Startup Time": 5.946,
"Plan Width": 8,
"Actual Loops": 1,
"Total Cost": 6842.67
},
"Triggers": []
}
]
有两种可能的解决方法:
1) 将查询重写为:
select user_id, min(day) from log where user_id = 'ac43a155-4fbb-49eb-a670-02c307eb3d4f' group by user_id
2) 像 finding MAX(db_timestamp) query 中建议的那样引入对索引
它们可能看起来不错,但我认为这两种方式都可以解决问题(第一个甚至是 hack)。从逻辑上讲,如果 Postgres 可以为“小时”选择合适的计划,它必须为“天”选择合适的计划,但事实并非如此。所以它看起来像是在时间戳字段聚合期间发生的 Postgres 错误,但我承认我可能会错过一些东西。有人可以告诉我是否可以在不使用 WA 的情况下在这里完成某些事情,或者这确实是 Postgres 错误,我必须报告它?
UPD:我已将此作为一个错误报告给 PostgreSQL 错误邮件列表。如果被接受,我会通知大家。
【问题讨论】:
-
您是否收集了表格的统计数据?
-
我有统计收集的默认设置,相信应该自动收集。所以我需要对统计做一些明确的事情吗?
-
顺便说一句:{user_id,day} 对我来说似乎是候选键。
标签: postgresql sql-execution-plan sql-tuning postgresql-9.6 sql-timestamp