索引系列九--索引特性之有序优化distinct

--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
分享地址:https://www.cdcxhl.com/article48/pihgep.html

成都网站建设公司_创新互联,为您提供虚拟主机服务器托管用户体验App设计网站营销小程序开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司