8、实战聚簇因子随DML变化的情形
a、创建演示环境
scott@SYBO2SZ> @cr_big_tb 1000000 -->创建一张百万记录的表
Table created.
scott@SYBO2SZ> @idx_stat -->查看表与索引相关信息(CF为14489,TB_BLKS为14652)
Enter value for input_table_name: BIG_TABLE
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- ------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 2088 1000000 1 1 14489 20130422 12:27:43 14652 999712
b、模拟DML操作
--创建一个临时表来存储将要从表big_table删除的记录
scott@SYBO2SZ> create table big_table_tmp nologging as select * from big_table where id>=10000 and id<=200000;
scott@SYBO2SZ> delete from big_table nologging where id>=10000 and id<=200000; -->从表big_table删除一些记录
scott@SYBO2SZ> commit;
-->查看表与索引相关信息(从下面的查询结果可知,删除记录并不使得CF发生变化)
scott@SYBO2SZ> @idx_stat
Enter value for input_table_name: BIG_TABLE
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- -------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 2088 1000000 1 1 14489 20130422 12:27:43 14652 999712
scott@SYBO2SZ> exec dbms_stats.gather_table_stats('SCOTT','BIG_TABLE',cascade=>true); -->收集统计信息
scott@SYBO2SZ> @idx_stat -->查看表与索引相关信息(在收集统计信息后,删除记录后CF为11732,TB_BLKS依然为14652)
Enter value for input_table_name: BIG_TABLE --(TB_BLKS块数未发生变化是因为空闲空间没有释放,需要shrink)
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- ------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 1692 809999 1 1 11732 20130422 12:31:45 14652 808497
-->接下来将删除的数据插入到big_table以模拟表上新增数据,分两次插入,以使得id变得无序
scott@SYBO2SZ> insert into big_table nologging select * from big_table_tmp where id>=150000 and id<=200000
2 order by object_name;
scott@SYBO2SZ> insert into big_table nologging select * from big_table_tmp where id>=10000 and id<150000
2 order by object_name;
scott@SYBO2SZ> exec dbms_stats.gather_table_stats('SCOTT','BIG_TABLE',cascade=>true); -->收集统计信息
scott@SYBO2SZ> @idx_stat -->查看表与索引相关信息(此时CF的值由原来的14489增大到114256,呈数量级变化)
Enter value for input_table_name: BIG_TABLE
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- ------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 2088 1000000 1 1 114256 20130422 12:33:31 14652 998513
--下面尝试move table是否对CF有向影响
scott@SYBO2SZ> alter table big_table move;
scott@SYBO2SZ> @idx_stat -->查看表与索引相关信息(move table之后,无任何变化)
Enter value for input_table_name: BIG_TABLE
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- -------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 2088 1000000 1 1 114256 20130422 12:33:31 14652 998513
-->尝试收集统计信息后,在看CF的变化
-->下面的错误表明,move之后,索引失效
scott@SYBO2SZ> exec dbms_stats.gather_table_stats('SCOTT','BIG_TABLE',cascade=>true);
BEGIN dbms_stats.gather_table_stats('SCOTT','BIG_TABLE',cascade=>true); END;
*
ERROR at line 1:
ORA-20000: index "SCOTT"."BIG_TABLE_PK" or partition of such index is in unusable state
ORA-06512: at "SYS.DBMS_STATS", line 13182
ORA-06512: at "SYS.DBMS_STATS", line 13202
ORA-06512: at line 1
scott@SYBO2SZ> alter index big_table_pk rebuild nologging; ---->重建索引
scott@SYBO2SZ> exec dbms_stats.gather_table_stats('SCOTT','BIG_TABLE',cascade=>true); -->再次收集统计信息
PL/SQL procedure successfully completed.
scott@SYBO2SZ> @idx_stat -->重建索引后,CF的值反而增大了
Enter value for input_table_name: BIG_TABLE
Enter value for owner: SCOTT
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- ------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_PK 2088 1000000 1 1 118384 20130422 12:36:31 14649 999427
c、重建big_table
-->下面通过重建big_table来缩小CF的值,新的表名为big_table_tmp
scott@SYBO2SZ> drop table big_table_tmp purge; --->删除之前的临时表
scott@SYBO2SZ> create table big_table_tmp nologging as select * from big_table order by id;
scott@SYBO2SZ> create unique index big_table_tmp_pk on big_table_tmp(id);
scott@SYBO2SZ> alter table big_table_tmp add constraint big_table_tmp_pk primary key(id) using index big_table_tmp_pk;
scott@SYBO2SZ> exec dbms_stats.gather_table_stats('SCOTT','BIG_TABLE_TMP',cascade=>true);
scott@SYBO2SZ> @idx_stat --->表big_table_tmp上的CF值(14486)小于原始的CF值(14489)
Enter value for input_table_name: big_table_tmp
Enter value for owner: scott
AVG LEAF BLKS AVG DATA BLKS
BLEV IDX_NAME LEAF_BLKS DST_KEYS PER KEY PER KEY CLUST_FACT LAST_ANALYZED TB_BLKS TB_ROWS
---- ---------------- ---------- ---------- ------------- ------------- ---------- ------------------ ---------- ----------
2 BIG_TABLE_TMP_PK 2088 1000000 1 1 14486 20130422 12:38:37 14649 995891
d、比较不同的CF对查询性能的影响
-->下面来基于表big_table与big_table_tmp来比较一下不同的CF对查询的影响
scott@SYBO2SZ> set autot trace;
scott@SYBO2SZ> select * from big_table where id between 10000 and 15000;
5001 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3747652938
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5001 | 478K| 606 (0)| 00:00:08 |
| 1 | TABLE ACCESS BY INDEX ROWID| BIG_TABLE | 5001 | 478K| 606 (0)| 00:00:08 |
|* 2 | INDEX RANGE SCAN | BIG_TABLE_PK | 5001 | | 13 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID">=10000 AND "ID"<=15000)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2993 consistent gets
531 physical reads
116 redo size
287976 bytes sent via SQL*Net to client
4155 bytes received via SQL*Net from client
335 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
5001 rows processed
--原始表上的查询的cost为606, consistent gets与physical reads分别为2993,531
scott@SYBO2SZ> select * from big_table_tmp where id between 10000 and 15000;
5001 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1127920103
------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4982 | 476K| 86 (0)| 00:00:02 |
| 1 | TABLE ACCESS BY INDEX ROWID| BIG_TABLE_TMP | 4982 | 476K| 86 (0)| 00:00:02 |
|* 2 | INDEX RANGE SCAN | BIG_TABLE_TMP_PK | 4982 | | 13 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID">=10000 AND "ID"<=15000)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
750 consistent gets
76 physical reads
0 redo size
287976 bytes sent via SQL*Net to client
4155 bytes received via SQL*Net from client
335 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
5001 rows processed
--新创建的表的cost 为86, consistent gets与physical reads分别为750,76. 呈数量级低低于原表的开销
-->可以将原始表big_table删除,然后将big_table_tmp重命名为big_table
--> alter table big_table_tmp rename to big_table; (不再演示)
-->注上面的create table as ..方式并不适合用于生产环境的真实操作,因为表上的一些属性会被忽略掉.
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
