首页 > Oracle【诡异】的效率问题

Oracle【诡异】的效率问题

下面这两段SQL,本意是用循环造数据用作压力测试用的,脚本的效率不高,大家不要吐槽。
在执行了这两句后,能够造出一百万左右的数据。
后来测试完了,把fcs_cls_app表清空,只留几条数据或者没有数据的时候,增删改查这个表就会变得非常的慢,哪怕几条数据都会要很长时间,几乎一分钟吧,十分的诡异。
我后来把fcs_cls_app这个表drop掉重新创建,就好了,增删改查都是正常的速度。
请问这会是什么原因呢?
本来没有考虑过这个脚本引起的问题的,然后今天有个同事也需要造数据做压测,我就把我写的这个脚本发给他改一下用了,然后出现了和我一样的情况:测试完了,表里面数据只有6条都要查询很长的时间!
(操作都是在PLSQL11客户端上进行的,但是很明显的能够感觉到系统运行时与这个表相关的操作也会变得很慢)

create table test as select * from fcs_cls_app;  
--造数据
declare
 i integer;
 my_num integer; 
begin
  i:=1;
  my_num:=1;
  for i in 1..20 loop
    delete from test;
    insert into test select * from fcs_cls_app;
    update test t set t.appsheetserialno = t.appsheetserialno + my_num;
    insert into fcs_cls_app select * from test;
    my_num:= my_num*2;
    commit;
  end loop;
end;

首先说一点,这个问题和redolog无关,redolog是oralce运行机制的一部分,必须使用,最多是测试环境归档日志没开。

表插入大批量数据再删除后,查询速度慢的问题,关键是使用delete语句删除记录后,表占用的空间没有释放,在oracle中的说法就是HWM保持不变,因此查询的时候即使只有几条记录,如果全表扫描的话仍会花费大量的时间,如果是临时表需要定期清理数据,使用truncate table更合适一些。

参考的测试案例:
http://www.eygle.com/archives/2011/04/delete_truncate_hwm.html

oracle官方对truncate操作的说明:

Use the TRUNCATE TABLE statement to remove all rows from a table. By default, Oracle Database also performs the following tasks:

Deallocates all space used by the removed rows except that specified
by the MINEXTENTS storage parameter

Sets the NEXT storage parameter to the size of the last extent removed
from the segment by the truncation process

生产环境如果无法使用truncate,可以使用下面的方法降低HWM

方法一:

alter table table_name move

完成后需要重新创建索引

方法二:

alter table table_name enable row movement;
alter table table_name shrink space;

检查下Oracle的redolog是不是开着


对这个表收集一下统计信息就好了

【热门文章】
【热门文章】