我在 Postgres 解释中测试了更多参数,我运行了两个非常简单的查询,它们返回了完全不同的成本。
查询1:
select * from reservation limit 1;
解释查询 1:
Limit (cost=10000000000.00..10000000000.12 rows=1 width=1585) (actual time=0.007..0.007 rows=1 loops=1)
Output: -- lot of cols here
Buffers: shared hit=1
-> Seq Scan on public.reservation (cost=10000000000.00..10000269615.83 rows=2235283 width=1585) (actual time=0.006..0.006 rows=1 loops=1)
Output: -- lot of cols here
Buffers: shared hit=1
Settings: effective_cache_size = '177252896kB', enable_seqscan = 'off', enable_sort = 'off', from_collapse_limit = '20', join_collapse_limit = '20', random_page_cost = '1.5', work_mem = '10000kB'
Planning Time: 0.341 ms
Execution Time: 0.028 ms
查询2:
select * from reservation order by reservationid limit 1;
reservationid 是一个序列号。
解释查询 2:
Output: -- lot of cols here
Buffers: shared hit=4
-> Index Scan using pk_reservation on public.reservation (cost=0.43..306073.95 rows=2235283 width=1585) (actual time=0.026..0.026 rows=1 loops=1)
Output: -- lot of cols here
Buffers: shared hit=4
Settings: effective_cache_size = '177252896kB', enable_seqscan = 'off', enable_sort = 'off', from_collapse_limit = '20', join_collapse_limit = '20', random_page_cost = '1.5', work_mem = '10000kB'
Planning Time: 0.114 ms
Execution Time: 0.043 ms
正如我们所见,时间非常相似,但成本却截然不同。我知道访问索引比从磁盘读取要快得多,但即使使用 1 的限制,差异这么大是否正常?
我想死元组可以解决如此高成本的问题。
版本: x86_64-pc-linux-gnu 上的 PostgreSQL 12.12,由 x86_64-pc-linux-gnu-gcc (GCC) 7.4.0 编译,64 位
正如Frank Heikens在评论中所回答的,我报告的问题是由于设置
enable_seqscan = 'off'
造成的,这将seq_scan的值设置为无限,以防止规划器使用此选项来找到恢复数据的最佳路径。