The difference between count (1), COUNT (*), and count (column name) functions in Oracle

Source: Internet
Author: User

1) count (1) compared to COUNT (*):

1. If your data table does not have a primary key, then count (1) is faster than COUNT (*)
2, if there is a primary key, then the primary key (Union primary key) as the count of the condition is also faster than COUNT (*)
3, if your table has only one field, then count (*) is the fastest
4, COUNT (*) count (1) compares the two. The data field that corresponds to count (1) is mostly still.
5, 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 is no need to count (?), with Count (*), SQL will help you to complete the optimized

2) Count Detailed:

1. Count (*) will return the total number of rows in the table that contain null, but count (column name) returns the total number of all rows except null in the table (columns with default values will also be counted).
2, distinct column name, the result will be to remove the value of NULL and duplicate data after the result


3) Examples are shown below:

1Sql> Create TableTest2 (3Enamevarchar2(Ten),4Sal Number(4)5  );6 7 The table is created. 8 9  TenSql> Insert  intoTestValues('Fxe1', -); Onehas been created1line.  A   -Sql> Insert  intoTest (ENAME)Values('Fxe2'); -has been created1line.  the  -Sql> Insert  intoTest (ENAME)Values('fxe3'); -has been created1line.  -  +Sql> Insert  intoTest (ENAME)Values('fxe4'); -has been created1line.  +  ASql> Insert  intoTestValues('fxe5', the); athas been created1line.  -  -Sql> Insert  intoTestValues('Fxe6', the); -has been created1line.  -  -  inSql> Select *  fromtest; - ename SAL to ---------- ---------- +Fxe1 - - Fxe2 the fxe3 * fxe4 $Fxe5 thePanax NotoginsengFxe6 the -  the  +Sql> Select Count(*) fromTest--Count (*): contains null, altogether 6 records A COUNT(*) the ---------- + 6         -  $Sql> Select Count(1) fromTest--count (1): Contains null, altogether 6 records, and the result of Count (*) $ COUNT(1) - ---------- - 6        the  -Sql> Select Count(SAL) fromTest--count (column name): does not contain NULL, but contains duplicate value entries, altogether 3 recordsWuyi COUNT(SAL) the ---------- - 3               Wu   -Sql> Select Count(distinctSal fromTest--count (column name): does not contain NULL, go to "count (distinct sal)", altogether 2 records About COUNT(distinctsal) $ ------------------ - 2               -  -Sql> Select distinctSal fromtest; A SAL + ---------- the  the -  -

Go to: http://blog.csdn.net/szstephenzhou/article/details/8446481

The difference between count (1), COUNT (*), and count (column name) functions 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.