Why is Select 1 faster than Select count(*)?
It is better still to use EXISTS where the RDBMS supports it or an equivalent, as this will stop processing rows as soon as it finds a match.
Since Oracle doesn't support IF EXISTS in PL/SQL, CodeByMidnight's suggestion to use EXISTS would normally be done with something like
SELECT 1 INTO l_local_variable FROM dual WHERE EXISTS( SELECT 1 FROM some_table WHERE some_column = some_condition );
Oracle knows that it can stop processing the WHERE EXISTS clause as soon as one row is found, so it doesn't have to potentially count a large number of rows that match the criteria. This is less of a concern, of course, if you are checking to see whether a row with a particular key exists than if you are checking a condition involving unindexed columns or checking a condition that might result in a large number of rows being returned.
(Note: I wish I could post this as a comment on CodeByMidnight's post, but comments can't include formatted code).
UPDATE: Given the clarification the original poster made in their comment, the short, definitive answer is that a SELECT 1
or SELECT COUNT(1)
is no faster than a SELECT COUNT(*)
. Contrary to whatever coding guidelines you are looking at, COUNT(*)
is the preferred way of counting all the rows. There was an old myth that a COUNT(1)
was faster. At a minimum, that hasn't been true in any version of Oracle released in the past decade and it is unlikely that it was ever true. It was a widely held belief, however. Today, code that does a COUNT(1)
rather than a COUNT(*)
generally makes me suspect that the author is prone to believe various Oracle myths which is why I would suggest using COUNT(*)
.
I'd be suprised if select count(*) wasn't properly optimised, there is no need to load in all the columns as there will be no column related processing.