Oracle 中count(1) 、count(*) 和count(列名) 函数的区别

https://www.cnblogs.com/dshore123/p/7878310.html

1)count(1)与count(*)比较:

1、如果你的数据表没有主键,那么count(1)比count(*)快
2、如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快
3、如果你的表只有一个字段的话那count(*)就是最快的啦
4、count(*) count(1) 两者比较。主要还是要count(1)所相对应的数据字段。
5、如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。  因为count(*),自动会优化指定到那一个字段。所以没必要去count(?),用count(*),sql会帮你完成优化的

2)count详解:

1、count(*)将返回表格中所有存在的行的总数包括值为null的行,然而count(列名)将返回表格中除去null以外的所有行的总数(有默认值的列也会被计入).
2、distinct 列名,得到的结果将是除去值为null和重复数据后的结果

3)举例演示如下:

-- 创建test表
 SQL> create table test
 (
 ename varchar2(10),
 sal number(4)
 );

-- 向test表中插入数据
SQL> insert into test values('fxe1',90);
SQL> insert into test(ename) values('fxe2');
SQL> insert into test(ename) values('fxe3');
SQL> insert into test(ename) values('fxe4');
SQL> insert into test values('fxe5',80);
SQL> insert into test values('fxe6',80);

SQL> select * from test; -- 查询test表中的所有数据
ENAME      SAL
---------- ----------
fxe1        90
fxe2
fxe3
fxe4
fxe5        80
fxe6        80
---------- ----------
27 SQL> select count(*) from test;  -- count(*):包含NULL,一共6条记录
COUNT(*)
----------
6        

SQL> select count(1) from test;  -- count(1):包含NULL,一共6条记录,和count(*)的结果一样
COUNT(1)
----------
6       

SQL> select count(sal) from test;  -- count(列名):不包含NULL,但包含重复值项,一共3条记录
COUNT(SAL)
----------
3              

SQL> select count(distinct sal) from test;  -- count(列名):不包含NULL,去重“count(distinct sal)”,一共2条记录
COUNT(DISTINCTSAL)
------------------
2              

SQL> select distinct sal from test;
SAL
----------
80
90

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值