X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 0EF8AD1BB71 for ; Tue, 9 Dec 2003 20:26:54 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 11642-09 for ; Tue, 9 Dec 2003 16:26:23 -0400 (AST) Received: from curie.credativ.org (credativ.com [217.160.209.18]) by svr1.postgresql.org (Postfix) with ESMTP id 56E53D1CCCB for ; Tue, 9 Dec 2003 16:21:40 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by curie.credativ.org (Postfix) with ESMTP id 468A8562C8; Tue, 9 Dec 2003 21:21:41 +0100 (CET) Received: from 192.168.2.126 (dsl-213-023-254-243.arcor-ip.net [213.23.254.243]) (using TLSv1 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by curie.credativ.org (Postfix) with ESMTP id A49B056271; Tue, 9 Dec 2003 21:21:39 +0100 (CET) From: Peter Eisentraut To: david_shadovitz@xontech.com (David Shadovitz), pgsql-sql@postgresql.org Subject: Re: Fetch a single record Date: Tue, 9 Dec 2003 21:21:37 +0100 User-Agent: KMail/1.5.4 References: <8b37e2e5.0312051351.4045b4e@posting.google.com> In-Reply-To: <8b37e2e5.0312051351.4045b4e@posting.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200312092121.37830.peter_e@gmx.net> X-Virus-Scanned: by AMaViS X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/72 X-Sequence-Number: 16238 David Shadovitz wrote: > I'm looking for the fastest way to fetch a single record from a > table. I don't care what record it is. > > Here are two techniques I've explored: > > 1. Use LIMIT > SELECT * FROM myTable LIMIT 1 > > 2. Get a valid OID and then get the record. > SELECT MIN(oid) AS anOID FROM myTable > SELECT * FROM myTable WHERE oid = anOID > > The 1st technique is slow. (I think PostgreSQL fetches all records > and then returns just one.) The 2nd is faster, but not fast enough. This is hard to believe. The first technique should be the fasted, but might suffer from a bad plan. Please make sure that you have run ANALYZE, and then post EXPLAIN ANALYZE output.