I have read from Internet resources that a query will be slow when the offset increases. But in my case I think its much too slow. I am using postgres 9.3
.
Here is the query (id
is primary key):
select * from test_table offset 3900000 limit 100;
It returns the data in around 10 seconds
. And I think its much too slow. I have around 4 million
records in the table. Overall size of the database is 23GB
.
Machine configuration:
RAM: 12 GB
CPU: 2.30 GHz
Core: 10
A few values from postgresql.conf
file which I have changed are as below. Others are default.
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
Apart from these, I also have tried by changing the values of random_page_cost = 2.0
and cpu_index_tuple_cost = 0.0005
and the result is the same.
Explain (analyze, buffers)
result over the query is as below:
"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"
How do people around the world negotiates with this problem with Postgres? Any alternate solution will be helpful for me as well.
UPDATE:: Adding order by id
(tried with other indexed column as well) and here is the explain:
"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"