X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D7D8A329D27 for ; Tue, 28 Sep 2004 15:24:32 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53746-07 for ; Tue, 28 Sep 2004 14:24:22 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 13776329EAE for ; Tue, 28 Sep 2004 15:24:25 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id i8SEOGfj014452; Tue, 28 Sep 2004 10:24:16 -0400 (EDT) To: Martin Marques Cc: pgsql-sql@postgresql.org Subject: Re: Best way to know if there is a row In-reply-to: <200409280955.23773.martin@bugs.unl.edu.ar> References: <200409280955.23773.martin@bugs.unl.edu.ar> Comments: In-reply-to Martin Marques message dated "Tue, 28 Sep 2004 09:55:23 -0300" Date: Tue, 28 Sep 2004 10:24:16 -0400 Message-ID: <14451.1096381456@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/247 X-Sequence-Number: 19093 Martin Marques 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