pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: Rajesh Kumar Mallah <mallah.rajesh@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: order by slowing down a query by 80 times
Date: Mon, 28 Jun 2010 10:17:29 -0400
Message-ID: <26902.1277734649@sss.pgh.pa.us> (raw)
In-Reply-To: <4C2862C90200002500032BCD@gw.wicourts.gov>
References: <AANLkTim6WXLK8iJuwZbzJjCuPr9LPau1OX7vxwYDFdkN@mail.gmail.com>
	<4C2862C90200002500032BCD@gw.wicourts.gov>

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> writes:
> 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.

I'm guessing that most of the cost is in repeated evaluations of the
filter clause
	(co_name_vec)::tsvector @@ to_tsquery('manufacturer'::text)

There are two extremely expensive functions involved there (cast to
tsvector and to_tsquery) and they're being done over again at what
I suspect is practically every table row.  The unordered query is
so much faster because it stops after having evaluated the text
search clause just a few times.

The way to make this go faster is to set up the actually recommended
infrastructure for full text search, namely create an index on
(co_name_vec)::tsvector (either directly or using an auxiliary tsvector
column).  If you don't want to maintain such an index, fine, but don't
expect full text search queries to be quick.

			regards, tom lane



view thread (9+ messages)  latest in thread

Message-ID: <26902.1277734649@sss.pgh.pa.us>
Permalink:  ../26902.1277734649@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/26902.1277734649@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, Kevin.Grittner@wicourts.gov, mallah.rajesh@gmail.com
  Subject: Re: order by slowing down a query by 80 times
  In-Reply-To: <26902.1277734649@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox