Sql server big data如何批量update数据
2016-02-18 10:20
465 查看
原因:
要一次性update 2千万条数据,虽然update sql很简单,但是由于一次性修改太多的数据,造成数据库log满了,就会报error:
[ErrorCode: 9002, SQL State: S0004] The transaction log for database'XXXXData' is full. To find out why space in the log cannot be reused, seethe log_reuse_wait_desc column in sys.databases
所以:
因为是数据库一次性update 数据操作,因此,就想到批量的update数据。 我能想到的解决方案就是,像分页查询 sql那样,每次update 一定数量的数据。
SQL
要一次性update 2千万条数据,虽然update sql很简单,但是由于一次性修改太多的数据,造成数据库log满了,就会报error:
[ErrorCode: 9002, SQL State: S0004] The transaction log for database'XXXXData' is full. To find out why space in the log cannot be reused, seethe log_reuse_wait_desc column in sys.databases
所以:
因为是数据库一次性update 数据操作,因此,就想到批量的update数据。 我能想到的解决方案就是,像分页查询 sql那样,每次update 一定数量的数据。
SQL
DECLARE @pagesize INT, @pages INT, @offset INT, @maxresult INT select @pagesize=100000, @pages=1 -- 1 prepare data list CREATE TABLE #Tbl_Affected_ID ( id INT NOT NULL, rowNum INT NOT NULL, modified CHAR(1) DEFAULT 'N' NOT NULL ) INSERT #Tbl_Affected_ID(id, rowNum) SELECT aa.id, ROW_NUMBER() OVER(ORDER BY aa.id) AS rowNum FROM TBLxxx aa WHERE userId is null '?' -- 2 batch update WHILE exists( select 1 from #Tbl_Affected_ID where modified = 'N') BEGIN select @offset = (@pages-1) * @pagesize select @maxresult = @offset + @pagesize UPDATE TBLxxx SET userId = 'test_user' from #Tbl_Affected_ID tmp WHERE tmp.id = TBLxxx.id and rowNum between @offset and @maxresult UPDATE #Tbl_Affected_ID SET modified = 'Y' WHERE rowNum between @offset and @maxresult select @pages = @pages+1 END
相关文章推荐
- MongoDB是?
- 手机短信码使用流程
- mysql 启动不了
- oracle dump文件说明--未发生迁移
- oracle如何 查询 创建 执行存储过程及游标的作用
- mysql返回字段,当没记录时返回0的解决方法
- 测试sq3数据库,读取数据库把内容输出到csv文件,附上输出到excel方法
- mysql处理字符串的两个绝招:substring_index,concat
- Mysql 分表
- Mysql insert 性能优化
- SQL Server ->> Database Promgramming Object Security Control(数据库编程对象安全控制)
- Xcode7.2使用sqlite3数据库的方法
- Xcode7.2使用sqlite3数据库的方法
- Xcode7.2使用sqlite3数据库的方法
- oracle_nvl
- mysql 一行转多列
- mysql 查看所有存储过程
- MySQL数据类型和属性设置
- SQL Server 2008 R2 主从数据库同步
- 答:SQLServer DBA 三十问之三:有哪些操作会使用到TempDB;如果TempDB异常变大,可能的原因是什么,该如何处理