I have the following code:
SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING COUNT(*) > 1;
Is there any difference to the results or performance if I replace the COUNT(*) with COUNT(‘x’)?
(This question is related to a previous one)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To say that
SELECT COUNT(*) vs COUNT(1)results in your DBMS returning ‘columns’ is pure bunk. That may have been the case long, long ago but any self-respecting query optimizer will choose some fast method to count the rows in the table – there is NO performance difference betweenSELECT COUNT(*), COUNT(1), COUNT('this is a silly conversation')Moreover,
SELECT(1) vs SELECT(*)will NOT have any difference in INDEX usage — most DBMS will actually optimizeSELECT( n ) into SELECT(*)anyway. See the ASK TOM: Oracle has been optimizingSELECT(n) into SELECT(*)for the better part of a decade, if not longer: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1156151916789One thing to note – if you are using COUNT(col) (don’t!) and col is marked NULL, then it will actually have to count the number of occurrences in the table (either via index scan, histogram, etc. if they exist, or a full table scan otherwise).
Bottom line: if what you want is the count of rows in a table, use COUNT(*)