--DISTINCT測試前的準備
成都創新互聯專注于企業全網營銷推廣、網站重做改版、麻城網站定制設計、自適應品牌網站建設、H5開發、成都商城網站開發、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為麻城等各大城市提供網站開發制作服務。
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環境之后,DISTINCT由于其 HASH UNIQUE的算法導致其不會產生排序,其調整的
ALTER SESSION SET "_gby_hash_aggregation_enabled" = FALSE
*/
set linesize 1000
set autotrace traceonly
select distinct object_id from t ;
執行計劃
-----------------------------------------------------------------------------------
| 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 ;
執行計劃
--------------------------------------------------------------------------------------
| 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
名稱欄目:索引系列九--索引特性之有序優化distinct
分享地址:http://m.kartarina.com/article48/pihgep.html
成都網站建設公司_創新互聯,為您提供虛擬主機、服務器托管、用戶體驗、App設計、網站營銷、小程序開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯