From: Tom Lane <tgl@sss.pgh.pa.us>
To: Martin Marques <martin@bugs.unl.edu.ar>
Cc: pgsql-sql@postgresql.org
Subject: Re: Best way to know if there is a row
Date: Tue, 28 Sep 2004 10:24:16 -0400
Message-ID: <14451.1096381456@sss.pgh.pa.us> (raw)
In-Reply-To: <200409280955.23773.martin@bugs.unl.edu.ar>
References: <200409280955.23773.martin@bugs.unl.edu.ar>
Martin Marques <martin@bugs.unl.edu.ar> writes:
> I need to know if there is at least one row in the relation that comes from a
> determinated query. Which is the best way to do this:
> (a) Build the query with "SELECT * ..." and after executing the query see if
> numRows()>0
> (b) Build the query with "SELECT count(*) ...", fetch the row and see if
> count>0
Either of these implies computing the entire query result set, which is
much more computation than you want. Instead do
SELECT * .... LIMIT 1
and then see if you got a row or not. Aside from not computing useless
rows, the LIMIT will bias the optimizer in favor of fast-start plans.
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-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, martin@bugs.unl.edu.ar
Subject: Re: Best way to know if there is a row
In-Reply-To: <14451.1096381456@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