溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

索引系列五--索引特性之存列值優(yōu)化sum avg

發(fā)布時(shí)間:2020-07-31 11:17:06 來源:網(wǎng)絡(luò) 閱讀:568 作者:1415699306 欄目:關(guān)系型數(shù)據(jù)庫(kù)

SUM/AVG的優(yōu)化

drop table t purge;

create table t as select * from dba_objects;

create index idx1_object_id on t(object_id);

set autotrace on

set linesize 1000

set timing on 


select sum(object_id) from t; 

執(zhí)行計(jì)劃

----------------------------------------------------------------------------------------

| Id  | Operation             | Name           | Rows  | Bytes | Cost (%CPU)| Time     |

----------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |                |     1 |    13 |    49   (0)| 00:00:01 |

|   1 |  SORT AGGREGATE       |                |     1 |    13 |            |          |

|   2 |   INDEX FAST FULL SCAN| IDX1_OBJECT_ID | 92407 |  1173K|    49   (0)| 00:00:01 |

----------------------------------------------------------------------------------------

統(tǒng)計(jì)信息

----------------------------------------------------------

          0  recursive calls

          0  db block gets

        170  consistent gets

          0  physical reads

          0  redo size

        432  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

          

--比較一下假如不走索引的代價(jià),體會(huì)一下這個(gè)索引的重要性

select /*+full(t)*/ sum(object_id) from t;  

SUM(OBJECT_ID)

--------------

  2732093100         

執(zhí)行計(jì)劃

---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    13 |   292   (1)| 00:00:04 |

|   1 |  SORT AGGREGATE    |      |     1 |    13 |            |          |

|   2 |   TABLE ACCESS FULL| T    | 92407 |  1173K|   292   (1)| 00:00:04 |

---------------------------------------------------------------------------

統(tǒng)計(jì)信息

----------------------------------------------------------

          0  recursive calls

          0  db block gets

       1047  consistent gets

          0  physical reads

          0  redo size

        432  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     

          

--起來類似的比如AVG,和SUM是一樣的,如下:

select avg(object_id) from t; 

AVG(OBJECT_ID)

--------------

  37365.5338

執(zhí)行計(jì)劃

----------------------------------------------------------------------------------------

| Id  | Operation             | Name           | Rows  | Bytes | Cost (%CPU)| Time     |

----------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |                |     1 |    13 |    49   (0)| 00:00:01 |

|   1 |  SORT AGGREGATE       |                |     1 |    13 |            |          |

|   2 |   INDEX FAST FULL SCAN| IDX1_OBJECT_ID | 92407 |  1173K|    49   (0)| 00:00:01 |

----------------------------------------------------------------------------------------

統(tǒng)計(jì)信息

----------------------------------------------------------

          0  recursive calls

          0  db block gets

        170  consistent gets

          0  physical reads

          0  redo size

        448  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


--不知大家注意到?jīng)],這里的試驗(yàn)已經(jīng)告訴我們了,OBJECT_ID列是否為空,也不影響SUM/AVG等聚合的結(jié)果。          


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI