From: Pierre-Frédéric Caillaud <lists@boutiquenumerique.com>
To: pgsql-performance@postgresql.org
Subject: Re: Why does a simple query not use an obvious index?
Date: Mon, 30 Aug 2004 21:32:28 +0200
Message-ID: <opsdj8ke1rcq72hf@musicbox> (raw)
In-Reply-To: <16093.1093817023@sss.pgh.pa.us>
References: <ENEIKKAEJOFHOIHMMOIDIEBMJCAA.jack.kerkhof@guest-tek.com>
<1093804128.5493.22.camel@localhost.localdomain>
<87fz65k5sq.fsf@stark.xeocode.com>
<16093.1093817023@sss.pgh.pa.us>
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).
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: lists@boutiquenumerique.com
Subject: Re: Why does a simple query not use an obvious index?
In-Reply-To: <opsdj8ke1rcq72hf@musicbox>
* 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