Re: ORDER BY, LIMIT and indexes - Mailing list pgsql-performance

From Ivan Voras
Subject Re: ORDER BY, LIMIT and indexes
Date
Msg-id CAF-QHFUQaXW8E-n2okPJ8vbsKWCQZDYYYkK-37Du1YkZhc4Zkg@mail.gmail.com
Whole thread Raw
In response to Re: ORDER BY, LIMIT and indexes  (Ivan Voras <ivoras@freebsd.org>)
Responses Re: ORDER BY, LIMIT and indexes  (Claudio Freire <klaussfreire@gmail.com>)
Re: ORDER BY, LIMIT and indexes  (Mark Kirkwood <mark.kirkwood@catalyst.net.nz>)
Re: ORDER BY, LIMIT and indexes  (Sergey Konoplev <gray.ru@gmail.com>)
List pgsql-performance
Here are two more unexpected results. Same test table (1 mil. records,
"id" is SERIAL PRIMARY KEY, PostgreSQL 9.1, VACUUM ANALYZE performed
before the experiments):

ivoras=# explain analyze select * from lt where id > 900000 limit 10;
                                                   QUERY PLAN
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..1.71 rows=10 width=9) (actual
time=142.669..142.680 rows=10 loops=1)
   ->  Seq Scan on lt  (cost=0.00..17402.00 rows=101630 width=9)
(actual time=142.665..142.672 rows=10 loops=1)
         Filter: (id > 900000)
 Total runtime: 142.735 ms
(4 rows)

Note the Seq Scan.

ivoras=# explain analyze select * from lt where id > 900000;
                                                          QUERY PLAN

-------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on lt  (cost=1683.97..7856.35 rows=101630 width=9)
(actual time=38.462..85.780 rows=100000 loops=1)
   Recheck Cond: (id > 900000)
   ->  Bitmap Index Scan on lt_pkey  (cost=0.00..1658.56 rows=101630
width=0) (actual time=38.310..38.310 rows=100000 loops=1)
         Index Cond: (id > 900000)
 Total runtime: 115.674 ms
(5 rows)

This somewhat explains the above case - we are simply fetching 100,000
records here, and it's slow enough even with the index scan, so
planner skips the index in the former case. BUT, if it did use the
index, it would have been expectedly fast:

ivoras=# set enable_seqscan to off;
SET
ivoras=# explain analyze select * from lt where id > 900000 limit 10;
                                                         QUERY PLAN

----------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..1.74 rows=10 width=9) (actual time=0.081..0.112
rows=10 loops=1)
   ->  Index Scan using lt_pkey on lt  (cost=0.00..17644.17
rows=101630 width=9) (actual time=0.078..0.100 rows=10 loops=1)
         Index Cond: (id > 900000)
 Total runtime: 0.175 ms
(4 rows)

It looks like the problem is in the difference between what the
planner expects and what the Filter or Index operations deliver:
(cost=0.00..17402.00 rows=101630 width=9) (actual
time=142.665..142.672 rows=10 loops=1).


pgsql-performance by date:

Previous
From: Ivan Voras
Date:
Subject: Re: ORDER BY, LIMIT and indexes
Next
From: Claudio Freire
Date:
Subject: Re: ORDER BY, LIMIT and indexes