12

In Oracle, when querying for row existence, why is Select 1 fast than Select count(*)?

  • 1
    without knowing which RDBMS engine you're using there's no way to answer correctly. Different engines behave differently – Glen Sep 10 '09 at 15:53
  • 2
    Do you mean "why is select count(1) faster than Select count(*)"? – Tony Andrews Sep 10 '09 at 16:34
  • I mean "Select 1". I'm looking at some old, offline coding standards documentation where it is claimed that "Select 1" is faster than "Select count(*)", and a preferred way to query for row existence. The documentation does not provide a technical explanation for why this is a "performance enhancing" technique. When I searched around on the net, I found things like AskTom threads and debates...but I didn't see a clear, definitive answer. –  Sep 10 '09 at 17:04
  • I updated my answer. The short answer is that there is no difference between COUNT(*) and COUNT(1). – Justin Cave Sep 10 '09 at 20:37
  • 5
    Which question can you answer fastest. (a) Is there someone called "Smith" in the phone book? (b) How many called Smith are there in the phone book? – WW. Sep 11 '09 at 05:35

8 Answers8

16

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.

MartW
  • 12,348
  • 3
  • 44
  • 68
  • 5
    +1 We should only use COUNT() we need to know the actual number of records involved. – APC Sep 10 '09 at 15:58
14

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(*).

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
2

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.

cjk
  • 45,739
  • 9
  • 81
  • 112
1

http://www.dbasupport.com/forums/archive/index.php/t-28741.html

For Oracle at least.

Vinen
  • 51
  • 5
0

Because a star takes all cols into the count, "1" is a native datatype.

In MySQL "SELECT COUNT(name_of_the_primary_key)" should be as fast as your SELECT 1. Its the index that counts. A count() on an index should be quite fast ;)

Mario Mueller
  • 1,450
  • 2
  • 13
  • 16
0

I don't think this is true for Oracle. http://justoracle.blogspot.com/2006/12/count-vs-count1.html

But, in some databases the reason is because '*' has to visit the tables meta-data. This tends to add an un-needed overhead. Where as 1 is just a literal.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
0

All other things being equal, "select 1 from my_table" will return the first result quicker than "select count(*) from my_table", but if you retrieve all the results from the query, the count(*) one will be quicker because it involves much less data (1 integer, as opposed to 1 integer per each row in the table).

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
0

If you are using postgresql, than count(1) is infact slower than count(*)

Per: https://www.citusdata.com/blog/2016/10/12/count-performance/ :

A note about count(1) vs count(*). One might think that count(1) would be faster because count(*) appears to consult the data for a whole row. However the opposite is true. The star symbol is meaningless here, unlike its use in SELECT *. PostgreSQL parses The expression count(*) as a special case taking no arguments. (Historically the expression ought to have been defined as count().) On the other hand count(1) takes an argument and PostgreSQL has to check at every row to see that its argument, 1, is indeed still not NULL.

Maxim
  • 725
  • 1
  • 8
  • 24