Re: Rules, Windows and ORDER BY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jason Dusek <jason(dot)dusek(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Rules, Windows and ORDER BY
Date: 2012-08-23 13:51:07
Message-ID: 10799.1345729867@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jason Dusek <jason(dot)dusek(at)gmail(dot)com> writes:
> I have a simple table of keys and values which periodically
> receives updated values. It's desirable to keep older values
> but, most of the time, we query only for the latest value of a
> particular key.

> CREATE TABLE kv
> ( k bytea NOT NULL,
> at timestamptz NOT NULL,
> realm bytea NOT NULL,
> v bytea NOT NULL );
> CREATE INDEX ON kv USING hash(k);
> CREATE INDEX ON kv (t);
> CREATE INDEX ON kv USING hash(realm);

> SELECT * FROM kv WHERE k = $1 AND realm = $2 ORDER BY at DESC LIMIT 1;

If you want to make that fast, an index on (k,realm,at) would help.
Those indexes that you did create are next to useless for this,
and furthermore hash indexes are quite unsafe for production.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martin French 2012-08-23 13:51:19 Re: At what point does a big table start becoming too big?
Previous Message Loughrey, Hugh 2012-08-23 13:50:39 Re: Check PostgreSQL status using MS-DOS bat file?