溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

索引系列九--索引特性之有序優(yōu)化distinct

發(fā)布時間:2020-07-14 11:43:12 來源:網絡 閱讀:416 作者:1415699306 欄目:關系型數據庫

--DISTINCT測試前的準備

drop table t purge;

create table t as select * from dba_objects;

update t set object_id=rownum;

alter table T modify OBJECT_ID not null;

update t set object_id=2;

update t set object_id=3 where rownum<=25000;

commit;





/*

在oracle10g的R2環(huán)境之后,DISTINCT由于其 HASH UNIQUE的算法導致其不會產生排序,其調整的

ALTER SESSION SET "_gby_hash_aggregation_enabled" = FALSE

*/

set linesize 1000

set autotrace traceonly


select  distinct object_id from t ;

執(zhí)行計劃

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

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

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

|   0 | SELECT STATEMENT   |      | 88780 |  1127K|       |   717   (1)| 00:00:09 |

|   1 |  HASH UNIQUE       |      | 88780 |  1127K|  1752K|   717   (1)| 00:00:09 |

|   2 |   TABLE ACCESS FULL| T    | 88780 |  1127K|       |   292   (1)| 00:00:04 |

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

統計信息

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

          0  recursive calls

          0  db block gets

       1047  consistent gets

          0  physical reads

          0  redo size

        462  bytes sent via SQL*Net to client

        416  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          2  rows processed



/*不過雖然沒有排序,通過觀察TempSpc可知distinct消耗PGA內存進行HASH UNIQUE運算,

接下來看看建了索引后的情況,TempSpc關鍵字立即消失,COST也立即下降許多,具體如下*/


--為T表的object_id列建索引

create index idx_t_object_id on t(object_id);

set linesize 1000

set autotrace traceonly


select  /*+index(t)*/ distinct object_id from t ;

執(zhí)行計劃

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

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

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

|   0 | SELECT STATEMENT   |                 | 88780 |  1127K|   582   (1)| 00:00:07 |

|   1 |  SORT UNIQUE NOSORT|                 | 88780 |  1127K|   582   (1)| 00:00:07 |

|   2 |   INDEX FULL SCAN  | IDX_T_OBJECT_ID | 88780 |  1127K|   158   (1)| 00:00:02 |

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

統計信息

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

          0  recursive calls

          0  db block gets

        145  consistent gets

          0  physical reads

          0  redo size

        462  bytes sent via SQL*Net to client

        416  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          2  rows processed

 

 


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI