-------行链接
/***重建跨多个数据块的数据行
生产环境中,数据变更后会导致很多数据行会存储在多个数据块中。从而导致IO使用频率增高并进一步导致并查询速度变慢。
平时维护中需要DBA重建跨多个数据块的数据行使其每一行数据都尽可能位于一个数据块中.
***/
-------------------------
--1.验证是否有行链接方法
-------------------------
create tablespace test
datafile 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\test01.DBF'
size 5000m
autoextend on
next 100m maxsize unlimited
extent management local autoallocate
segment space management auto;
create user ocpyang identified by ocpyang
default tablespace test
temporary tablespace temp
;
grant dba to ocpyang;
alter user ocpyang
quota unlimited on test;
create table t2
(
sid int not null ,
sname nvarchar2(100)
)
tablespace test;
--循环导入数据
declare
maxrecords constant int:=300000;
i int :=1;
begin
for i in 1..maxrecords loop
insert into t2 values(i,'this is a test for ocpyang today!');
end loop;
dbms_output.put_line(' 成功录入数据! ');
commit;
end;
/
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t2 select * from t2;
col segment_name for a10
col segment_type for a10
select segment_name,segment_type,bytes/1024/1024 as MB from user_segments where segment_name='T2';
SEGMENT_NA SEGMENT_TY MB
---------- ---------- ----------
T2 TABLE 808
select count(*) from t2;
COUNT(*)
----------
9600000
SQL> delete from t2 where sid<=50000;
已删除50000行。
SQL> commit;
提交完成。
update t2 set sname='this is a test about row chain! welcome to ocpyang blog,thank you!' where sid<100000;
-------------------------
--2.验证是否有行链接
-------------------------
-----方法1:段顾问
--(1).查看顾问建议
1).创建一个任务
2).为这个任务分配一个对象
3).设置任务数
4).执行这个任务
begin
declare
my_task_id number;
obj_id number;
my_task_name varchar2(100);
my_task_desc varchar2(200);
begin
my_task_name :='ocpyang';
my_task_desc :='手动段顾问建议';
--step1:
dbms_advisor.create_task
(
advisor_name =>'Segment Advisor',
task_id =>my_task_id,
task_name =>my_task_name,
task_desc =>my_task_desc
);
--step2:
dbms_advisor.create_object
(
task_name =>my_task_name,
object_type =>'TABLE',
attr1 =>'OCPYANG', --用户名
attr2 =>'T2', --表名
attr3 =>NULL,
attr4 =>NULL,
attr5 =>NULL,
object_id =>obj_id
);
--step3:
dbms_advisor.set_task_parameter
(
task_name =>my_task_name,
parameter =>'recommend_all',
value =>'TRUE'
);
--step4:
dbms_advisor.execute_task(my_task_name);
end;
end;
/
--删除
exec DBMS_ADVISOR.DELETE_TASK('ocpyang');
---查询1:获取顾问建议
SELECT
'Task Name : ' || f.task_name || chr(10) ||
'Segment Name : ' || o.attr2 || chr(10) ||
'Segment Type : ' || o.type || chr(10) ||
'Partition Name : ' || o.attr3 || chr(10) ||
'Message : ' || f.message || chr(10) ||
'More Info : ' || f.more_info TASK_ADVICE
FROM dba_advisor_findings f
,dba_advisor_objects o
WHERE o.task_id = f.task_id
AND o.object_id = f.object_id
AND f.task_name like 'ocpyang'
ORDER BY f.task_name;
TASK_ADVICE
--------------------------------------------------------------------------------
Task Name : ocpyang
Segment Name : T2
Segment Type : TABLE
Partition Name :
Message : 此对象中的空闲空间小于 10MB。
More Info : 分配空间:3145728: 已用空间:2025670: 可回收空间:1120058:
Task Name : ocpyang
Segment Name : T2
Segment Type : TABLE
Partition Name :
TASK_ADVICE
--------------------------------------------------------------------------------
Message :通过重新组织,可以删除该对象具有的行链接(参考)
---(2).解决方法:移动表(移动表后需要重建所有索引)
alter table t2 move;
验证索引状态:
select owner,index_name,status from dba_indexes where table_name='T2';
alter index pk_t2 rebuild;
----------方法2:analyze table
---(1).分析表
@?\RDBMS\ADMIN\utlchain.sql;
analyze table ocpyang.t2 list chained rows;
select count(*) from chained_rows where table_name='T2';
COUNT(*)
---------
664586
---(2).解决方法:
create table t22
as
select * from ocpyang.t2
where rowid in(select head_rowid from chained_rows where table_name='T2');
delete from ocpyang.t2
where rowid in (select head_rowid from chained_rows where table_name='T2');
insert into ocpyang.t2 select * from t22;
------重新分析查看是否还有行链接
truncate table chained_rows;
analyze table ocpyang.t2 list chained rows;
--如果返回0就说明解决了行链接.
select count(*) from chained_rows where table_name='T2';
COUNT(*)
---------
0
drop table t22;