想要比较这三者的性能,当然做实验是最精准的,用事实说话。
先准备数据:
Microsoft Windows [版本 5.2.3790]
(C) 版权所有 1985-2003 Microsoft Corp.
C:/Documents and Settings/Administrator>sqlplus sys/manager as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期二 4月 7 16:06:50 2015
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 – Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> drop table t;
表已删除。
SQL> create table t as select * from dba_objects;
表已创建。
SQL> update t set object_id =rownum ;
已更新72749行。
SQL> set timing on
SQL> set autotrace on
SQL>
下面实验:
SQL> select count(*) from t;
COUNT(*)
———-
72749
已用时间: 00: 00: 00.03
执行计划
———————————————————-
Plan hash value: 2966233522
——————————————————————-
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————-
| 0 | SELECT STATEMENT | | 1 | 283 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 85379 | 283 (1)| 00:00:04 |
——————————————————————-
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
153 recursive calls
0 db block gets
1134 consistent gets
0 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(object_id) from t;
COUNT(OBJECT_ID)
—————-
72749
已用时间: 00: 00: 00.03
执行计划
———————————————————-
Plan hash value: 2966233522
—————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
—————————————————————————
| 0 | SELECT STATEMENT | | 1 | 13 | 283 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| T | 85379 | 1083K| 283 (1)| 00:00:04 |
—————————————————————————
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
27 recursive calls
0 db block gets
1123 consistent gets
0 physical reads
0 redo size
433 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(1) from t;
COUNT(1)
———-
72749
已用时间: 00: 00: 00.06
执行计划
———————————————————-
Plan hash value: 2966233522
——————————————————————-
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————-
| 0 | SELECT STATEMENT | | 1 | 283 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 85379 | 283 (1)| 00:00:04 |
——————————————————————-
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
4 recursive calls
0 db block gets
1121 consistent gets
0 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
现在可以看出,在这个普通的表中,三种性能count方式都相同。
下面创建索引
SQL> create index idx_object_id on t(object_id);
索引已创建。
再次测试
已用时间: 00: 00: 00.24
SQL> select count(*) from t;
COUNT(*)
———-
72749
已用时间: 00: 00: 00.02
执行计划
———————————————————-
Plan hash value: 2966233522
——————————————————————-
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————-
| 0 | SELECT STATEMENT | | 1 | 283 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 85379 | 283 (1)| 00:00:04 |
——————————————————————-
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
5 recursive calls
0 db block gets
1122 consistent gets
0 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(object_id) from t;
COUNT(OBJECT_ID)
—————-
72749
已用时间: 00: 00: 00.04
执行计划
———————————————————-
Plan hash value: 1131838604
——————————————————————————
——-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| T
e |
——————————————————————————
——-
| 0 | SELECT STATEMENT | | 1 | 13 | 46 (0)| 0
00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
| 2 | INDEX FAST FULL SCAN| IDX_OBJECT_ID | 85379 | 1083K| 46 (0)| 0
00:01 |
——————————————————————————
——-
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
4 recursive calls
0 db block gets
251 consistent gets
161 physical reads
0 redo size
433 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(1) from t;
COUNT(1)
———-
72749
已用时间: 00: 00: 00.02
执行计划
———————————————————-
Plan hash value: 2966233522
——————————————————————-
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————-
| 0 | SELECT STATEMENT | | 1 | 283 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 85379 | 283 (1)| 00:00:04 |
——————————————————————-
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
4 recursive calls
0 db block gets
1122 consistent gets
0 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
现在可以看出,创建索引后,count(idx_column)性能显著提高,其余两个不变。
现在删除索引,添加主键:
SQL> drop index idx_object_id;
索引已删除。
已用时间: 00: 00: 00.27
SQL> alter table t add constraint pk_object_id primary key(object_id);
表已更改。
已用时间: 00: 00: 00.63
SQL> select count(*) from t;
COUNT(*)
———-
72749
已用时间: 00: 00: 00.08
执行计划
———————————————————-
Plan hash value: 1265209789
——————————————————————————
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————————
| 0 | SELECT STATEMENT | | 1 | 43 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_OBJECT_ID | 85379 | 43 (0)| 00:00:01 |
——————————————————————————
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
145 recursive calls
0 db block gets
259 consistent gets
151 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(object_id) from t;
COUNT(OBJECT_ID)
—————-
72749
已用时间: 00: 00: 00.18
执行计划
———————————————————-
Plan hash value: 1265209789
——————————————————————————
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————————
| 0 | SELECT STATEMENT | | 1 | 43 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_OBJECT_ID | 85379 | 43 (0)| 00:00:01 |
——————————————————————————
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
4 recursive calls
0 db block gets
241 consistent gets
0 physical reads
0 redo size
433 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(1) from t;
COUNT(1)
———-
72749
已用时间: 00: 00: 00.02
执行计划
———————————————————-
Plan hash value: 1265209789
——————————————————————————
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
——————————————————————————
| 0 | SELECT STATEMENT | | 1 | 43 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_OBJECT_ID | 85379 | 43 (0)| 00:00:01 |
——————————————————————————
Note
—–
– dynamic sampling used for this statement (level=2)
统计信息
———————————————————-
4 recursive calls
0 db block gets
241 consistent gets
0 physical reads
0 redo size
425 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
三种count方式性能都提高。
结论:
无索引无主键的表,count()的性能都相同,都需要全表扫描
当有索引时,对索引列count()性能最好,由于count(*),count(1)需要计算空值,因此count(idx_column)只用访问索引就可以,但是count(*),count(1)还是需要全表扫描
当有主键时,count()三种方式行性能都有显著提高,都去扫描主键,因为主键不能为空,count(*),count(1)就可以直接去扫描主键了
另外,其实对表的不同的列count,性能是不同的,字段越往后,性能越差,我就不做试验了。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/7237.html