ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 聚簇因子

聚簇因子

原创 Linux操作系统 作者:tyoodz 时间:2011-11-09 21:17:50 0 删除 编辑

聚簇因子的值表示了表中行的有序程度,这个值与块数接近时,说明表有序程度很好,得到了很好的组织。在这种情况下,同一个叶子块中的索引条目可能
指向同一个数据块上的行。聚簇因子与行值相近时,表的次序是非常随机的,同一个叶子块上的索引条目不太可能指向同一个数据块上的行。

下面先建立两张表,一张建立索引的列非常有序,另一张表建立索引的列非常无序,就是tom书上的例子。

SQL> create table colocated ( x int, y varchar2(80) );

表已创建。

SQL> begin
  2      for i in 1 .. 100000
  3      loop
  4          insert into colocated(x,y)
  5          values (i, rpad(dbms_random.random,75,'*') );
  6      end loop;
  7  end;
  8  /

PL/SQL 过程已成功完成。

SQL> alter table colocated
  2  add constraint colocated_pk
  3  primary key(x);

表已更改。

SQL> begin
  2  dbms_stats.gather_table_stats( user, 'COLOCATED', cascade=>true );
  3  end;
  4  /

PL/SQL 过程已成功完成。

上面是有序表

SQL> create table disorganized
  2  as
  3  select x,y
  4    from colocated
  5   order by y;

表已创建。

SQL> alter table disorganized
  2  add constraint disorganized_pk
  3  primary key (x);

表已更改。

SQL> begin
  2  dbms_stats.gather_table_stats( user, 'DISORGANIZED', cascade=>true );
  3  end;
  4  /

PL/SQL 过程已成功完成。

这个是无序表

通过user_indexes可以查看聚簇因子的值

SQL> select a.index_name,
  2         b.num_rows,
  3         b.blocks,
  4         a.clustering_factor
  5    from user_indexes a, user_tables b
  6  where index_name in ('COLOCATED_PK', 'DISORGANIZED_PK' )
  7    and a.table_name = b.table_name
  8  /


INDEX_NAME                       NUM_ROWS     BLOCKS CLUSTERING_FACTOR
------------------------------ ---------- ---------- -----------------
COLOCATED_PK                       100000       1252              1190
DISORGANIZED_PK                    100000       1219             99926


在查看两张表块的情况

SQL> select table_name, blocks
  2  from user_tables
  3  where table_name in ( 'COLOCATED', 'DISORGANIZED' );

TABLE_NAME                                                       BLOCKS
------------------------------------------------------------ ----------
COLOCATED                                                          1252
DISORGANIZED                                                       1219


可以看出有序表colocated聚簇因子值非常接近blocks值,而无序表的值非常接近表的行数100000行。

聚簇因子也可以看作通过索引读整个表时对表的逻辑I/O。


SQL> alter session set sql_trace=true;

会话已更改。

SQL> select count(y) from
  2  (select /*+ index(colocated colocated_pk)*/ * from colocated);

  COUNT(Y)
----------
    100000

  1
SQL> select count(y) from
  2  (select /*+ index(disorganized disorganized_pk)*/ * from disorganized)
  3  /

  COUNT(Y)
----------
    100000

下面是TKPROF的内容:

select count(y) from
(select /*+ index(colocated colocated_pk)*/ * from colocated)

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.01       0.04          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.15       0.14          0       1399          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.17       0.18          0       1399          0           1



Rows     Row Source Operation
-------  ---------------------------------------------------
      1  SORT AGGREGATE (cr=1399 pr=0 pw=0 time=147829 us)
 100000   TABLE ACCESS BY INDEX ROWID COLOCATED (cr=1399 pr=0 pw=0 time=1200067 us)
 100000    INDEX FULL SCAN COLOCATED_PK (cr=209 pr=0 pw=0 time=400045 us)(object id 54173)
 
 
可以得出对表执行的逻辑io为1399-209=1190,与该表的聚簇因子值相等。


********************************************************************************

select count(y) from
(select /*+ index(disorganized disorganized_pk)*/ * from disorganized)

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.37       0.38          0     100135          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.37       0.38          0     100135          0           1


Rows     Row Source Operation
-------  ---------------------------------------------------
      1  SORT AGGREGATE (cr=100135 pr=0 pw=0 time=380757 us)
 100000   TABLE ACCESS BY INDEX ROWID DISORGANIZED (cr=100135 pr=0 pw=0 time=1500061 us)
 100000    INDEX FULL SCAN DISORGANIZED_PK (cr=209 pr=0 pw=0 time=500041 us)(object id 54175)
 
 可以得出对表执行的逻辑io为100135-209=99926,与该表的聚簇因子值相等。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25361369/viewspace-710518/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: session_cached_cursors
请登录后发表评论 登录
全部评论

注册时间:2011-05-16

  • 博文量
    55
  • 访问量
    87644