Re: ORDER BY ... LIMIT.. performance

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "john cartmell" <john(dot)cartmell(at)mediaburst(dot)co(dot)uk>, <pgsql-performance(at)postgresql(dot)org>
Subject: Re: ORDER BY ... LIMIT.. performance
Date: 2002-12-05 22:21:18
Message-ID: web-2023168@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

John,

> I am not sure whether this is a know problem but we discovered this
> the
> other day.
> We are using PostgreSQL 7.2.1 on Redhat 7.3.

First of all, there are a few bug-fixes between 7.2.1 and 7.2.3. One
relates to backups, and another to security. So you should upgrade to
7.2.3 immediately -- no init or restore from backup required (not
version 7.3, which has some significant changes).

> The table has about over a million rows (~1.4).
>
> The query concerned is of the form
>
> SELECT *
> FROM tblCompany
> WHERE lower(companyname) like 'company%'
> ORDER BY companyname
> LIMIT 20,0
>
> There is a functional index lower(companyname) for the like clause.
>
> Without the LIMIT clause the query takes approximately 3-5 seconds to
> return.
> If total number of rows returned without the LIMIT clause is greater
> than 20 records, then the above query also takes th same amount of
> time.
> But if the the total number of rows is 20 or less then the time taken
> for the above query to return goes up to 20-30 seconds. Has anyone
> else
> come across this. We have managed to get round it by performing a
> count
> first and only performing the LIMIT if there are enough rows but
> surely
> the query should be able to do this itself!

This seems very odd. Please do the following:

1) Post an EXPLAIN ANALYZE statement for the above query, with limit,
that returns in 3-5 seconds.
2) Post an EXPLAIN ANALYZE for a query that returns slowly (20-30
seconds).

Thanks!

-Josh

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Bruce Momjian 2002-12-05 22:42:49 Re: Filesystem optimisation for postgresql tables and WAL
Previous Message Jochem van Dieten 2002-12-05 22:05:57 Re: Is a better way to have the same result of this query?