| Diffderence between count(*) , count(1) [message #575649] |
Fri, 25 January 2013 03:23  |
 |
saipradyumn
Messages: 131 Registered: October 2011 Location: Hyderabad
|
Senior Member |
|
|
Hi Michel,
Could you please explain the difference between count(*) and count(1)
But from query prospective both are giving the same results
Thanks
SaiPrayumn
|
|
|
|
|
|
| Re: Diffderence between count(*) , count(1) [message #575658 is a reply to message #575649] |
Fri, 25 January 2013 04:40   |
 |
Michel Cadot
Messages: 54126 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
The best answer I can give is you want to count the rows, you don't want to count some 1 (why 1? why not 2? or 100?).
Now, Oracle knows many developers are dumb and so convert COUNT(1) to COUNT(*) anyway:
SQL> explain plan for select count(1) from emp;
Explained.
SQL> select * from table(dbms_xplan.display('PLAN_TABLE',null,'ALL'));
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------
Plan hash value: 3381701820
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FULL SCAN| EMP_PK | 15 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / EMP@SEL$1
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) COUNT(*)[22]
Note the column projection, Oracle use to return the result.
So, be smart and use COUNT(*).
Regards
Michel
[Updated on: Fri, 25 January 2013 04:40] Report message to a moderator
|
|
|
|
|
|
| Re: Diffderence between count(*) , count(1) [message #575977 is a reply to message #575975] |
Wed, 30 January 2013 00:53  |
 |
Michel Cadot
Messages: 54126 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
You didn't read what I said, do you?
You didn't read the link LittleFoot provided, do you?
COUNT(1) IS COUNT(*) as Oracle internally converts COUNT(1) to COUNT(*).
COUNT(1) just shows you don't understand what you write.
Regards
Michel
|
|
|
|