count(*)和sum(1)的效率(一)

2014-11-24 13:26:28 · 作者: · 浏览: 4
count(*)和sum(1)的效率
count(*)和sum(*)的结果有时候是一样的,所以有时候开放在写存储过程的时候会用到sum当count使用
这样有不有问题呢?我们来讨论下这2个的效率。
[sql]
SQL> oradebug event 10046 trace name context forever,level 12;
Statement processed.
SQL> select count(*) from business.PRPLCERTIFYIMGTEMPBAK;
COUNT(*)
----------
103287157
Elapsed: 00:00:10.01
SQL> select sum(1) from business.PRPLCERTIFYIMGTEMPBAK;
SUM(1)
----------
103287157
Elapsed: 00:00:13.27
SQL> oradebug event 10046 trace name context off;
Statement processed.
SQL> oradebug tracefile_name
从10046的信息,我们很容易就发现。sum的效率不足体现在cpu上,I/O完全一样。
[sql]
select count(*)
from
business.PRPLCERTIFYIMGTEMPBAK
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.02 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 9.98 9.75 203828 203874 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 9.98 9.77 203828 203874 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=203874 pr=203828 pw=0 time=9750091 us)
103287157 INDEX FAST FULL SCAN IDX_SERIALNO (cr=203874 pr=203828 pw=0 time=309861672 us)(object id 155232)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
db file scattered read 12791 0.00 0.65
SQL*Net message from client 2 3.47 3.47
********************************************************************************
select sum(1)
from
business.PRPLCERTIFYIMGTEMPBAK
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 13.26 12.95 203828 203874 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 13.26 12.95 203828 203874 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=203874 pr=203828 pw=0 time=12957830 us)
103287157 INDEX FAST FULL SCAN IDX_SERIALNO (cr=203874 pr=203828 pw=0 time=206574428 us)(object id 155232)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2