Xavier Noria wrote:
Not that is critical for my application, but just for curiosity which
is the recommended idiom to figure out whether a table has any register?
Xavier,
You can of course use the most direct method, counting records and
comparing like this.
select count(*) > 0 from t;
But this is inefficient if your table has lots of records. To eliminate
this overhead you can use the limit clause to only return the first
record, if it exists, in a subselect, and then check if you got any
results back.
select count(*) > 0 from (select * from t limit 1);
This can be improved by removing the count aggregate function call and
the test of its output by simply checking if you got a result or not.
select (select * from t limit 1) is not null;
This only looks at the first record and does not involve initializing
and finalizing an aggregate function. If your table has many columns
there is still some overhead that can be removed if you have a primary
key column (which can't contain a null), simply return only that column
from the subselect.
select (select id from t limit 1) is not null;
I think this is a nearly optimal way to check if a table has any records.
HTH
Dennis Cote
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------