Thread: Fetch a single record

Fetch a single record

From
david_shadovitz@xontech.com (David Shadovitz)
Date:
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.

Any other ideas?  Please reply to me personally (david@shadovitz.com)
as well as to the list.

Thanks.
-David


Re: Fetch a single record

From
Peter Eisentraut
Date:
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.



Re: Fetch a single record

From
Paul Thomas
Date:
On 05/12/2003 21:51 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.
> [snip]

Have you also tried  SELECT * from mytable limit 1

If you genuinely don't care what the record is (I assume you're justing 
testing that table is not empty?) then this might be the way to go. My 
rather limited knowledge of PG internals leads me to believe that this 
will generally cause just one page being read from disk (I'm assuming the 
99% case of no mega-sized text/bytea/whatever columns here). I'd be 
interested to know just how far off the mark by understanding is...

-- 
Paul Thomas
+------------------------------+---------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for the Smaller 
Business |
| Computer Consultants         | 
http://www.thomas-micro-systems-ltd.co.uk   |
+------------------------------+---------------------------------------------+


Re: Fetch a single record

From
greg@turnstep.com
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> I'm looking for the fastest way to fetch a single record from a table.
> I don't care what record it is.
That's a strange request. If you really don't care what comes back, you
don't even need to query a table:
SELECT 1;
If you perhaps want the column names, query the system tables.
Otherwise the LIMIT 1 should be very fast, especially if you have no
ordering. Avoid the "SELECT *" if you don't need all the columns
for a little more speed.
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200312091943
-----BEGIN PGP SIGNATURE-----
iD8DBQE/1mynvJuQZxSWSsgRAk0HAKDKTHglcodYw2G9j5Il60e96Vv/xwCfcZ6p
ffIBwsqFtqW0UABYttqzT3U=
=JV2a
-----END PGP SIGNATURE-----