日期:2014-05-16 浏览次数:20464 次
这样有不有问题呢?我们来讨论下这2个的效率。
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
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 0.00 0.00 db file scattered read 12791 0.00 0.63 SQL*Net message from client 2 10.56 10.56
所以开发童鞋们。在遇到类似的时候尽量使用count,而不用sum。