Re: Takes too long to fetch the data from database

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: soni de <soni(dot)de(at)gmail(dot)com>
Cc: Dave Dutcher <dave(at)tridecap(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Takes too long to fetch the data from database
Date: 2006-05-09 07:38:55
Message-ID: 20060509073855.GA11659@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, May 09, 2006 at 09:24:15 +0530,
soni de <soni(dot)de(at)gmail(dot)com> wrote:
>
> EXPLAIN
> pdb=# EXPLAIN ANALYZE select * from wan where kname = 'pluto' order by stime
> limit 50;
> NOTICE: QUERY PLAN:
>
> Limit (cost=3515.32..3515.32 rows=50 width=95) (actual time=
> 230492.69..230493.07 rows=50 loops=1)
> -> Sort (cost=3515.32..3515.32 rows=208 width=95) (actual time=
> 230492.68..230493.00 rows=51 loops=1)
> -> Seq Scan on wan (cost=0.00..3507.32 rows=208 width=95) (actual
> time=0.44..229217.38 rows=18306 loops=1)
> Total runtime: 230631.62 msec

Unless you have an index on (kname, stime) the query is going to need to
find the records with a value for kname of 'pluto' and then get the most
recent 50 of them. It looks like there are enough estimated records
with kname = 'pluto', that a sequential scan is being prefered.
Creating an extra index will slow down inserts somewhat, but will speed
up queries like the above significantly, so may be worthwhile for you.
I think later versions of Postgres are smarter, but for sure in 7.2
you will need to write the query like:
SELECT *
FROM wan
WHERE kname = 'pluto'
ORDER BY kname DESC, stime DESC
LIMIT 50
;

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message PFC 2006-05-09 08:38:17 Big IN() clauses etc : feature proposal
Previous Message soni de 2006-05-09 03:54:15 Re: Takes too long to fetch the data from database