【发布时间】:2019-08-28 23:52:33
【问题描述】:
我有一个具有以下结构和索引的表:
Table "public.client_data"
Column | Type | Modifiers
-------------------------+---------+-----------
account_id | text | not_null
client_id | text | not null
client_type | text | not null
creation_time | bigint | not null
last_modified_time | bigint | not null
Indexes:
"client_data_pkey" PRIMARY KEY, btree (account_id, client_id)
"client_data_last_modified_time_index" btree (last_modified_time)
我需要从这个表中找到最旧的记录 - 为此我使用了以下查询:
SELECT last_modified_time FROM client_data ORDER BY last_modified_time ASC LIMIT 1;
但是,在 AWS Aurora Postgres 9.6 中的 db.r4.2xlarge RDS 实例中,此表上大约 6100 万行的查询运行速度非常慢(90-100 分钟),没有其他并发查询在运行。
但是,将查询更改为使用 DESC 会立即完成。可能是什么问题呢?我期待,因为我有一个 last_modified_time 的索引,仅查询由该列排序并应用了限制的列将涉及一个仅索引查询,该查询应在索引中的第一个条目之后停止。
这里是解释分析的输出:
EXPLAIN ANALYZE SELECT last_modified_time FROM client_data ORDER BY last_modified_time ASC LIMIT 1;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.57..2.31 rows=1 width=8) (actual time=6297823.287..6297823.287 rows=1 loops=1)
-> Index Only Scan using client_data_last_modified_time_index on client_data (cost=0.57..1049731749.38 rows=606590292 width=8) (actual time=6297823.287..6297823.287 rows=1 loops=1)
Heap Fetches: 26575013
Planning time: 0.078 ms
Execution time: 6297823.306 ms
DESC 版本的查询结果相同
EXPLAIN ANALYZE SELECT last_modified_time FROM client_data ORDER BY last_modified_time DESC LIMIT 1;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.57..2.32 rows=1 width=8) (actual time=2.265..2.266 rows=1 loops=1)
-> Index Only Scan Backward using client_data_last_modified_time_index on client_data (cost=0.57..1066049674.69 rows=611336085 width=8) (actual time=2.264..2.264 rows=1 loops=1)
Heap Fetches: 9
Planning time: 0.095 ms
Execution time: 2.278 ms
任何指针?
【问题讨论】:
-
看起来 postgres 经常访问堆,可能是为了检查旧行的可见性。 autovacuum 是否跟上此表中的更改?
-
不确定,但自动吸尘器最近运行过,可能是这样。
-
是否有可能有很多 first 相同的 last_modified_time(与
ASC),但只有少数 last last_modified_time(与 @987654329 @)?
标签: database postgresql query-optimization rdbms