The difference between count (1) and COUNT (*) in Oracle

Source: Internet
Author: User

Count1) with Count (*) Comparison: If your data table does not have a primary key, COUNT (1) than COUNT (*Fast If there is a primary key, then the primary key (the Federated primary key) as the count condition is also more than count (*) to be quick if your table has only one field then count (*) is the fastest. COUNT (*) Count (1) Compare the two. The main or count (1) corresponds to the data field. If Count (1) is a clustered index, ID, that must be count (1) fast.       But the difference is very small. Because Count (*), automatically optimizes the specified field to that one. So there's no need to count (?), with Count (*), SQL will help you to complete the count of optimizations: count (*The total number of rows in the returned table includes null rows, but count (column name) returns the total number of all rows except null in the table (columns with default values will also be counted). Distinct the column name, the result will be the result of dropping the value null and repeating the data--------------------------------------------------------------------------------------------------------------- -examples are shown below: SQL>CREATE TABLE Test2 (      3Ename VARCHAR2 (Ten),      4Sal Number (4)      5 );            The table is created. SQL> INSERT INTO Test values ('Fxe1', -); has been created1line. SQL> INSERT into Test (ename) VALUES ('Fxe2'); has been created1line. SQL> INSERT into Test (ename) VALUES ('fxe3'); has been created1line. SQL> INSERT into Test (ename) VALUES ('fxe4'); has been created1line. SQL> INSERT INTO Test values ('fxe5', the); has been created1line. SQL> INSERT INTO Test values ('Fxe6', the); has been created1line. SQL>Select* fromtest; Ename SAL---------- ----------Fxe1 -fxe2 fxe3 fxe4 fxe5 theFxe6 theSQL>SelectCOUNT (*) fromtest; COUNT (*)      ----------6SQL>SelectCount (Sal) fromtest; COUNT (SAL)----------3SQL>SelectCOUNT (distinct Sal) fromtest; COUNT (distinctsal)------------------2SQL>SelectDistinct Sal fromtest; SAL---------- the       -               

  1. Count (1) and Count (*) Comparison:
  2. If your data table does not have a primary key, then Count(1) is faster than count(*)
  3. If there is a primary key, then the primary key (the Federated primary key) as the count condition is also faster than count(*)
  4. If your table has only one field, then count(*) is the quickest.
  5. Count (*)  Count (1) comparison of the two.    The data field that corresponds to count (1) is mostly still.
  6. If count(1) is a clustered index, ID, that must be count(1) fast.   But the difference is very small.
  7. Because count(*), automatically optimizes the specified field to that one. So there is no need to count(?), with count(*), SQL will help you to complete the optimized
  8. Count Detailed:
  9. Count (*) The total number of rows that are returned in the table will include a value of NULL the line, however Count (column name) removes the returned table NULL  The total number of all rows except those with default values will also be counted.
  10. distinct column name, the resulting result will be the drop value of NULL and repeat the data after the results
  11. ---------------------------------------------------------------------------------------------------------- ------  
  12. Examples are shown below:
  13. Sql> Create table test
  14. 2 (
  15. 3 ename VARCHAR2 (10),
  16. 4 sal Number (4)
  17. 5);
  18. The table is created.
  19. sql> Insert into test values(' fxe1 '   , 90);
  20. 1 rows have been created.
  21. sql> Insert into test (ename) values(' Fxe2 ')  ;
  22. 1 rows have been created.
  23. sql> Insert into test (ename) values('  Fxe3 ');
  24. 1 rows have been created.
  25. sql> Insert into test (ename) values(' fxe4 ');
  26. 1 rows have been created.
  27. sql> Insert into test values(' fxe5 '   , 80);
  28. 1 rows have been created.
  29. sql> Insert into test values(' fxe6 ', 80);
  30. 1 rows have been created.
  31. sql> Select * from test;
  32. Ename SAL
  33. ---------- ----------   
  34. FXE1 90
  35. Fxe2
  36. Fxe3
  37. Fxe4
  38. Fxe5 80
  39. Fxe6 80
  40. sql> Select count(*) from test;
  41. COUNT (*)  
  42. ----------   
  43. 6
  44. sql> Select count(sal) from test;
  45. COUNT (SAL)
  46. ----------   
  47. 3
  48. sql> Select count(distinct sal) from    test;
  49. COUNT (distinctsal)
  50. ------------------   
  51. 2
  52. sql> Select distinct sal from test;
  53. SAL
  54. ----------   
  55. 80
  56. 90

The difference between count (1) and COUNT (*) in Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.