将数据库中的所有Sequences统一增加10万

[复制链接]
查看11 | 回复9 | 2009-2-20 09:44:20 | 显示全部楼层 |阅读模式
今天PM通知我将所有的SEQ增减10W,研究了一下,才明白怎么回事(本人比较笨^_^)。
第一步:将SEQ的步长增加10W (把冰箱门打开)
第二步:执行nextval (把大象转进去)
第三步:将SEQ的步长修改到本来步长 (把冰箱门带上)
select 'alter sequence ' || t1.sequence_name || ' increment by 100000 nocache;' from user_sequences t1
union all
select 'select ' || t2.sequence_name || '.nextval from dual;' from user_sequences t2
union all
select 'alter sequence ' || t3.sequence_name || ' increment by 2 nocache;' from user_sequences t3;
简简单单三步走,搞定所有问题,嘿嘿……
------------------------------------------------------------------------------

本人写这篇东东只是为了学习提高,根据2L和4L的朋友的提议,本人将原有操作修改了一下,这样就可以做到动态了,而不必理会SEQ的原先步长。
declare
v_sql varchar2(500);
begin
for addCurrValue in (select s.sequence_name seqName,

s.increment_byseqIncrement

from user_sequences s) loop
/* 将SEQ的步长修改修改为100000 */
v_sql := 'alter sequence ' || addCurrValue.seqName || ' increment by 100000 nocache;';
execute immediate v_sql;
/* 获取SEQ的下一个值 */
v_sql := 'select ' || addCurrValue.seqName || '.nextval from dual;';
execute immediate v_sql;
/* 将SEQ的步长修改回原先的步长 */
v_sql := 'alter sequence ' || addCurrValue.seqName || ' increment by ' || addCurrValue.seqIncrement || ' nocache;';
execute immediate v_sql;
commit;
end loop;
end;
[ 本帖最后由 wjychl 于 2010-12-20 11:33 编辑 ]
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
简单?
你怎么知道原来的sequence 都是increment by 2 nocache?
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
还有更好的方法么?
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
declare
v_sql varchar2(500);
begin
for addCurrValue in (select s.sequence_name seqName,

s.increment_byseqIncrement

from user_sequences
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
这种变更一定要测试充分
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
nocache的sequence 性能差
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
原帖由 atgc 于 2010-12-20 13:14 发表
nocache的sequence 性能差


为什么呢?烦请详解,谢谢。
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
原帖由 wjychl 于 2010-12-20 19:20 发表

为什么呢?烦请详解,谢谢。

因为不带CACHE的序列每取一次都会提交一个自治事务,提交就会有那些写日志等操作。
可以看USER_SEQUENCES里面的CACHE_SIZE。
楼主方法挺不错的,以前我也在这里给人推荐过这个方法。要注意你跑脚本的时候别人没有在使用这个序列。
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
原帖由 wjychl 于 2010-12-20 19:20 发表

为什么呢?烦请详解,谢谢。

没有cache值,每次nextval都会去update 系统表,sequence本身也是表
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
cache ...会导致断号
当然 nocache也会断号。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行