pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
To: Hemapriya <priyam_1121@yahoo.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Query with Max, Order by is very slow.......
Date: Wed, 7 Apr 2004 23:23:17 +0100 (BST)
Message-ID: <Pine.LNX.4.58.0404072321340.3239@localhost.localdomain> (raw)
In-Reply-To: <20040407210354.79531.qmail@web21322.mail.yahoo.com>
References: <20040407210354.79531.qmail@web21322.mail.yahoo.com>

On Wed, 7 Apr 2004, Hemapriya wrote:

> Hi,
>
> we have table having 23 million rows.
> This is the table structure.
> Table Request:
>
>   Column   |            Type             | Modifiers
> -----------+-----------------------------+-----------
>  origindb  | character(1)                | not null
>  uid       | integer                     | not null
>  rtype     | integer                     |
>  senderid  | integer                     |
>  destaddr  | character varying(15)       |
>  opid      | integer                     |
>  devmodel  | integer                     |
>  ikind     | integer                     |
>  itemid    | character varying(10)       |
>  tranfk    | integer                     |
>  enteredon | timestamp without time zone |
>  status    | integer                     |
>  accountid | integer                     |
> Indexes:
>     "request_pkey" primary key, btree (origindb, uid)
>
> I do max Query like this
>
> select max(uid) from request where originDB=1;
>
> it took around 20 min to return the result..  Since
> max, count functions do the full table scan, i tried
> the workaround given..
>
> select uid from request where originDB=1 order by uid
> desc limit 1;
>
> this query runs forever.. i tried even without where
> condition..no result..

You really want an index on origindb and uid - the order by ... desc
limit 1 workaround is only quick if there's an index on the order by
field, and and where clause is faster if it can use an index to speed up
the query. I would say you might want an index on both of them together,
a joint index.

-- 

Sam Barnett-Cormack
Software Developer                           |  Student of Physics & Maths
UK Mirror Service (http://www.mirror.ac.uk)  |  Lancaster University



view thread (4+ messages)  latest in thread

Message-ID: <Pine.LNX.4.58.0404072321340.3239@localhost.localdomain>
Permalink:  ../Pine.LNX.4.58.0404072321340.3239@localhost.localdomain/
Also on:    postgresql.org/message-id/Pine.LNX.4.58.0404072321340.3239@localhost.localdomain

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: s.barnett-cormack@lancaster.ac.uk, priyam_1121@yahoo.com
  Subject: Re: Query with Max, Order by is very slow.......
  In-Reply-To: <Pine.LNX.4.58.0404072321340.3239@localhost.localdomain>
  References: <20040407210354.79531.qmail@web21322.mail.yahoo.com> <Pine.LNX.4.58.0404072321340.3239@localhost.localdomain>

* 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