Re: Why does a simple query not use an obvious index?

From: Pierre-Frédéric Caillaud <lists(at)boutiquenumerique(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Why does a simple query not use an obvious index?
Date: 2004-08-30 19:32:28
Message-ID: opsdj8ke1rcq72hf@musicbox
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


Another primary key trick :

If you insert records with a serial primary key, and rarely delete them
or update the timestamp, you can use the primary key to compute an
approximate number of rows.

a := SELECT pkey FROM table WHERE timestamp() > threshold ORDER BY
timestamp ASC LIMIT 1;
b := SELECT pkey FROM table WHERE ORDER BY pkey DESC LIMIT 1;

(b-a) is an approximate count.

Performance is great because you only fetch two rows. Index scan is
guaranteed (LIMIT 1). On the downside, you get an approximation, and this
only works for tables where timestamp is a date of INSERT, timestamp
worrelated wiht pkey) not when timestamp is a date of UPDATE (uncorrelated
with pkey).

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Pierre-Frédéric Caillaud 2004-08-30 19:39:17 Re: seqscan instead of index scan
Previous Message Pierre-Frédéric Caillaud 2004-08-30 19:21:26 Re: Why does a simple query not use an obvious index?