【Oracle】-【SORT AGGREGATE】-count與索引
Oracle10g:
create table t_count as select * from dba_objects;
create index t_count_i on t_count(object_id):
分別用:
select count(*) from t_count;
select count(object_id) from t_count;
select count(object_name) from t_count;
檢視是否使用索引對count查詢效能起到作用。
它們的執行計劃:
SQL> select count(*) from t_count;
Execution Plan
----------------------------------------------------------
Plan hash value: 2197880521
----------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 39 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T_COUNT | 12028 | 39 (0)| 00:00:01 |
----------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
SQL> select count(object_name) from t_count;
Execution Plan
----------------------------------------------------------
Plan hash value: 2197880521
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 19 | 39 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 19 | | |
| 2 | TABLE ACCESS FULL| T_COUNT | 10976 | 203K| 39 (0)| 00:00:01 |
------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
142 consistent gets
0 physical reads
0 redo size
423 bytes sent via SQL*Net to client
381 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;
Execution Plan
----------------------------------------------------------
Plan hash value: 3107438994
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 8 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | |
| 2 | INDEX FAST FULL SCAN| T_COUNT_I | 10976 | 54880 | 8 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
29 consistent gets
0 physical reads
0 redo size
421 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
它們的trace檔案:
select count(*)
from
t_count
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 2 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 142 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 144 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 25
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=142 pr=0 pw=0 time=1848 us)
10976 TABLE ACCESS FULL T_COUNT (cr=142 pr=0 pw=0 time=55 us)
elect count(object_id)
from
t_count
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 2 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 24 29 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 24 31 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 25
Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=29 pr=24 pw=0 time=2648 us)
10976 INDEX FAST FULL SCAN T_COUNT_I (cr=29 pr=24 pw=0 time=455 us)(object id 12404)
select count(object_name)
from
t_count
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 1 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 142 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 143 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 25
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=142 pr=0 pw=0 time=2037 us)
10976 TABLE ACCESS FULL T_COUNT (cr=142 pr=0 pw=0 time=153 us)
可以得出:
1、使用count(索引)確實可以用INDEX FAST FULL SCAN,不用TABLE ACCESS FULL。
2、注意到這裡SORT AGGREGATE,看似好像用到了排序,但count不需要排序啊?實際再看,它的COST是空的,實際沒有任何消耗。不是有sort就會排序。
SORT AGGREGATE做為sort的option之一比較特殊,它並不做sort。
SORT AGGREGATE作用於所有的data set上,用於aggregate function,例如sum, count, avg, min, max。
如果aggregate function不是作用於與所有的data set上,還是作用於不同的group上,那麼操作型別將會變為SORT (GROUP BY),這時會有sort發生。
ASKTOM也說過:
it hasn't anything to sort, it is just aggregating. The step however is called "sort aggregate" it knows there is just one row to "sort".
唯一還有點疑問的就是INDEX FAST FULL SCAN是有排序的,但這裡未顯示?
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7192724/viewspace-767157/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Oracle】排序與sort_area_sizeOracle排序
- count(0),count(1),count(*)總結與count(column)
- ORACLE的count與空值比較Oracle
- Oracle Sort JoinOracle
- SQL Server、Oracle中CASE 與COUNT合用計數SQLServerOracle
- mysql中count(1)與count(*)比較MySql
- Oracle表與索引的分析及索引重建Oracle索引
- 語系排序nls_sort與語系索引Linguistic Index排序索引NGUIIndex
- oracle 索引的建立與管理Oracle索引
- oracle dml與索引index(一)Oracle索引Index
- Oracle表與索引的分析及索引重建(轉)Oracle索引
- MongoDB:aggregate與aggregateCursorMongoDB
- SELECT COUNT(*) 索引會走 index fast full scan索引IndexAST
- oracle count null空與''空子行串的記錄嗎OracleNull
- 【原創】ORACLE 分割槽與索引Oracle索引
- [Oracle Script] Temporary Sort UsageOracle
- MongoDB aggregate效能優化與排序MongoDB優化排序
- sort_area_retained_size與sort_area_sizeAI
- [Oracle Script] check object count by userOracleObject
- 【TUNE_ORACLE】PGA_AGGREGATE_LIMIT詳解(一)PGA_AGGREGATE_TARGET的限制OracleMIT
- 【TUNE_ORACLE】PGA_AGGREGATE_LIMIT詳解(二)PGA_AGGREGATE_LIMIT的作用OracleMIT
- 【Oracle】-【ROWNUM與索引】-索引對ROWNUM檢索的影響Oracle索引
- Lucene 排序 Sort與SortField排序
- oracle實驗記錄 (sort_area_size與 cpu_time)Oracle
- count_sum_distinct與nullNull
- MySQL的COUNT語句--count(*)、 count(常量)、 count(列名)MySql
- 【TUNE_ORACLE】PGA_AGGREGATE_LIMIT詳解(三)PGA_AGGREGATE_LIMIT的大小設定OracleMIT
- 【優化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主鍵)、COUNT(ROWID)等優化
- 【Oracle】-【建立索引】-建立索引的操作原理與一些體會Oracle索引
- ORACLE索引與高效能SQL介紹Oracle索引SQL
- 索引@oracle索引技術索引Oracle
- pga_aggregate_target 相關總結 -- Oracle PGAOracle
- Tuning PGA_AGGREGATE_TARGET in Oracle 9iOracle
- Oracle 索引Oracle索引
- count(1),count(*),count(列)的區別
- 16、MySQL Case-索引key對select count(*)的影響MySql索引
- 從例項看oracle的索引監控與無效索引維護Oracle索引
- Oracle索引梳理系列(六)- Oracle索引種類之函式索引Oracle索引函式