From: Tom Lane <tgl@sss.pgh.pa.us>
To: Poul Møller Hansen <freebsd@pbnet.dk>
Cc: Richard Huxton <dev@archonet.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Optimizing query
Date: Mon, 15 Aug 2005 09:46:07 -0400
Message-ID: <25298.1124113567@sss.pgh.pa.us> (raw)
In-Reply-To: <43006480.4000409@pbnet.dk>
References: <43005482.7020105@pbnet.dk>
<43005CBD.5070703@archonet.com>
<43006480.4000409@pbnet.dk>
=?ISO-8859-1?Q?Poul_M=F8ller_Hansen?= <freebsd@pbnet.dk> writes:
> explain analyze SELECT * FROM my.table WHERE node = '10' ORDER BY node,
> id DESC LIMIT 1
> QUERY
> PLAN
> --------------------------------------------------------------------------------------------------------------------------------------------
> Limit (cost=22638.36..22638.36 rows=1 width=246) (actual
> time=3.001..3.007 rows=1 loops=1)
> -> Sort (cost=22638.36..22652.59 rows=5691 width=246) (actual
> time=2.984..2.984 rows=1 loops=1)
> Sort Key: node, id
> -> Index Scan using node_date on table (cost=0.00..21898.65
> rows=5691 width=246) (actual time=0.077..1.852 rows=62 loops=1)
> Index Cond: ((node)::text = '10'::text)
> Total runtime: 3.127 ms
You're not there yet: you want what Richard said, namely
explain analyze SELECT * FROM my.table WHERE node = '10' ORDER BY node DESC,
id DESC LIMIT 1
There shouldn't be any Sort in the plan, just the indexscan and Limit.
The plan above is going to suck if there are a lot of rows with node = '10'.
regards, tom lane
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, freebsd@pbnet.dk, dev@archonet.com
Subject: Re: Optimizing query
In-Reply-To: <25298.1124113567@sss.pgh.pa.us>
* 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