我从互联网资源中了解到,当偏移量增加时,查询速度会变慢。但在我的情况下,我认为它太慢了。我正在使用 postgres 9.3
。
这是查询语句(id
是主键):
select * from test_table offset 3900000 limit 100;
它大约需要 10秒
返回数据。我认为这太慢了。表中有大约 4百万
条记录。数据库的总大小为 23GB
。
机器配置:
RAM: 12 GB
CPU: 2.30 GHz
Core: 10
我已更改的postgresql.conf
文件中的一些值如下。其他值为默认值。
shared_buffers = 2048MB
temp_buffers = 512MB
work_mem = 1024MB
maintenance_work_mem = 256MB
dynamic_shared_memory_type = posix
default_statistics_target = 10000
autovacuum = on
enable_seqscan = off ## its not making any effect as I can see from Analyze doing seq-scan
除此之外,我还尝试通过更改
random_page_cost = 2.0
和cpu_index_tuple_cost = 0.0005
的值,但结果仍然相同。查询的
Explain(analyze,buffers)
结果如下:"Limit (cost=10000443876.02..10000443887.40 rows=100 width=1034) (actual time=12793.975..12794.292 rows=100 loops=1)"
" Buffers: shared hit=26820 read=378984"
" -> Seq Scan on test_table (cost=10000000000.00..10000467477.70 rows=4107370 width=1034) (actual time=0.008..9036.776 rows=3900100 loops=1)"
" Buffers: shared hit=26820 read=378984"
"Planning time: 0.136 ms"
"Execution time: 12794.461 ms"
全球的人们如何处理Postgres中的这个问题?任何替代方案对我都有帮助。
更新:添加order by id
(也尝试了其他索引列),以下是解释:
"Limit (cost=506165.06..506178.04 rows=100 width=1034) (actual time=15691.132..15691.494 rows=100 loops=1)"
" Buffers: shared hit=110813 read=415344"
" -> Index Scan using test_table_pkey on test_table (cost=0.43..533078.74 rows=4107370 width=1034) (actual time=38.264..11535.005 rows=3900100 loops=1)"
" Buffers: shared hit=110813 read=415344"
"Planning time: 0.219 ms"
"Execution time: 15691.660 ms"
work_mem = 1024MB
可能太高了,default_statistics_target = 10000
对于一般用途来说也太高了。关闭autovacuum
是不必要且危险的。你已经关闭autovacuum
多长时间了? - wildplasser