七、管理大的transactions
有些操作是大批量地修改数据,log增长速度十分快,如:
◆大量数据修改
◆删除一个表的所有记录
◆基于子查询的数据插入
◆批量数据拷贝
下面讲述怎样使用这些transaction 使log 不至溢满:
大量数据修改
例:
1>;update large_tab set col_1=0 2>;go |
若这个表很大,则此update动作在未完成之前就可能使log满,引起1105错误log full)而且执行这种大的transaction所产生的exclusive table loc,阻止其他用户在update期间修 改这个表,这可能引起死锁。为避免这些情况,我们可以把这个大的transaction分成几个小 的transactions,并执行dump transaction 动作。
上述例子可以分成两个或多个小transactions.
例如:
1>;update large_tab set col1=0 2>;where col2 3>;go 1>;dump transaction database_name with truncate_only 2>;go 1>;update large_tab set col1=0 2>;where col2>;=x 3>;go 1>;dump transaction database_name with truncate_only 2>;go |
若这个transaction 需要备份到介质上,则不用with truncate_only 选项。若执行dump transaction with truncate_only,应该先做dump database 命令。
删除一个表的所有记录
例:
1>;delete table large_tab 2>;go |
同样,把整个table的记录都删除,要记很多log,我们可以用truncate table命令代替上 述语句完成相同功能。
1>;truncate table large_tab 2>;go |
这样,表中记录都删除了,而使用truncate table 命令,log只记录空间回收情况,而不 是记录删除表中每一行的操作。
基于子查询的数据插入
例:
1>;insert new_tab select col1,col2 from large_tab 2>;go |
同样的方法,对这个大的transaction,我们应该处理为几个小的transactions。
1>;Insert new_tab 2>;select col1,col2 from large_tab where col1<=y 3>;go 1>;dump transaction database_name with truncate_only 2>;go 1>;insert new_tab 2>;select col1,col2 from large_tab where col1>;y 3>;go 1>;dump database database_name with truncate_only 2>;go |
同样,若想保存log到介质上,则dump transaction 后不加with truncate_only 选项。若 执行dump transaction with truncate_only,应该先做dump database 动作。
批量数据拷贝
在使用bcp把数据拷入数据库时,我们可以把这个大的transaction变成几个小的 transactions处理,避免log剧增。
开放trunc log on chkpt 选项
1>;use master 2>;go 1>;sp_dboption database_name,trunc,true 2>;go 1>;use database_name 2>;go 1>;checkpoint 2>;go bcp... -b 100 (on unix) bcp... /batch_size=100(on vms) |
关闭trunc log on chkpt选项,并dump database。
在这个例子中,一个批执行100行拷贝。也可以将bcp输入文件分成两或多个分开的文件, 在每个文件执行后做dump transaction 来避免log 满。
若bcp使用快速方式无索引,无triggers),这样操作不记log,换句话说,log 只记载 空间分配情况。在这种情况下,要先做dump database为恢复数据库用)。若log太小,可置 trunc log on chkpt 选项,这样在每次checkpoint后清除log。