Re: order by slowing down a query by 80 times - Mailing list pgsql-performance

From Kevin Grittner
Subject Re: order by slowing down a query by 80 times
Date
Msg-id 4C2862C90200002500032BCD@gw.wicourts.gov
Whole thread Raw
In response to order by slowing down a query by 80 times  (Rajesh Kumar Mallah <mallah.rajesh@gmail.com>)
Responses Re: order by slowing down a query by 80 times  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-performance
Rajesh Kumar Mallah <mallah.rajesh@gmail.com> wrote:

> just by removing the order by co_name reduces the query time
> dramatically from  ~ 9 sec  to 63 ms. Can anyone please help.

The reason is that one query allows it to return *any* 25 rows,
while the other query requires it to find a *specific* set of 25
rows.  It happens to be faster to just grab any old set of rows than
to find particular ones.

If the actual results you need are the ones sorted by name, then
forget the other query and focus on how you can retrieve the desired
results more quickly.  One valuable piece of information would be to
know how many rows the query would return without the limit.  It's
also possible that your costing factors may need adjustment.  Or you
may need to get finer-grained statistics -- the optimizer thought it
would save time to use an index in the sequence you wanted, but it
had to scan through 2212 rows to find 25 rows which matched the
selection criteria.  It might well have been faster to use a table
scan and sort than to follow the index like that.

-Kevin

pgsql-performance by date:

Previous
From: Rajesh Kumar Mallah
Date:
Subject: Re: order by slowing down a query by 80 times
Next
From: Tom Lane
Date:
Subject: Re: order by slowing down a query by 80 times