Python003-测试辅助示例应用数据库更新语句创建

时间:2022-02-16 12:44:20

上周同事又问一个问题:表 C_Application 中数据量较大,需要批量更新 load_start_time 的时间为 '1900-01-01 18:43:49' 为初始值,以一定时间间隔且每次更新数据量为2000笔的时间设定。

如何进行快速的数据更新操作(其初始手动单批次更新,更新效率比较低;后找开发也未提供给其比较好的方案)?

其实,此种问题的解决并不难,因为目标明确。可以通过最笨的分布式更新(多人单批次同步更新)、Excel拼sql更新等等,相信大家也可以想出很多的方法。如下提供两种基础解决参考(均为未完成的,或者说是有问题的),感兴趣的可以自行完善一下。

第一种:通过脚本语言,拼接sql语句,采用人工或自动更新数据库。python不完善脚本如下所示:

 #!/usr/bin/env python
# -*- coding: UTF-8 -*- import time
import DbsUtil if __name__ == '__main__': TIME_FORMAT_STYLE='%Y-%m-%d %X' time_start = '1900-01-01 18:43:49'
time_end = '2016-12-21 00:00:00' conn_dict = {"server": "127.0.0.1,1433", "db_name": "pythonMssql", "user_name": "ffp", "pwd": "ffp123"} time_stamp_start = time.mktime(time.strptime(time_start, TIME_FORMAT_STYLE))
time_stamp_end = time.mktime(time.strptime(time_end, TIME_FORMAT_STYLE)) timestamp_step = 60 update_time = time.strftime(TIME_FORMAT_STYLE, time.localtime(time_stamp_start)) row_num_start = 1
row_num_step = 2000
row_num_end = 2000 ms = DbsUtil.MSSQL(host="127.0.0.1",user="ffp",pwd="ffp123",db="pythonMssql")
resList = ms.ExecQuery("SELECT count(*) as counts_up from C_Application")
print resList
CYCLE_MAXa = int(resList[][]) / row_num_step + 1
print CYCLE_MAXa
CYCLE_MAX = int(1874826 / row_num_step + 1) print CYCLE_MAX
CYCLE_MIN = 0 while CYCLE_MIN < CYCLE_MAX:
row_num_start = 1 + CYCLE_MIN * row_num_step
row_num_end = (CYCLE_MIN + 1) * row_num_step
update_time = time.strftime(TIME_FORMAT_STYLE, time.localtime(time_stamp_start + CYCLE_MIN * timestamp_step)) update_sql = "update C_Application set load_start_time = '" + update_time + "' where AppKey in ((select AppKey from (select row_number() over (order by AppKey) as rowId, AppKey from C_Application where Load_Date = '2016-12-21 00:00:00' and load_start_time = '1900-01-01 18:43:49') as t where rowId between " + str(row_num_start) + " and " + str(row_num_end) + "))" print update_sql
CYCLE_MIN = CYCLE_MIN + 1

第二种:通过mssql中的存储过程也可以实现需求。不完善代码如下所示,可自行完善!

 create proc page_update(
@TableName varchar(50), -- 表名
@ReFieldsStr varchar(200), -- 字段名(全部字段为*)
@OrderString varchar(200), -- 排序字段(必须!支持多字段不用加order by)
@WhereString varchar(500) = N'', -- 条件语句(不用加where)
@PageSize int, -- 每页多少条记录
@PageIndex int = 1 , -- 指定当前为第几页
@TotalRecord int output -- 返回执行结果总记录数
)
as
begin
-- 处理开始点和结束点
Declare @StartRecord int;
Declare @EndRecord int;
Declare @TotalCountSql nvarchar(500);
Declare @SqlString nvarchar(2000); SET @StartRecord = (@PageIndex-1)*@PageSize + 1
SET @EndRecord = @StartRecord + @PageSize - 1
-- 总记录数语句
SET @TotalCountSql = N'select @TotalRecord = count(*) from ' + @TableName;
-- 查询语句
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName; IF (@WhereString != '' or @WhereString != null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END -- 返回总记录数
EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output; -- 执行主语句
SET @SqlString ='select ' + @ReFieldsStr + ' from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord));
print @SqlString
END --调用分页存储过程 page_update
--exec page_update 'C_Application','AppKey','AppKey',"Load_Date = '2016-12-21 00:00:00' and load_start_time = '1900-01-01 18:43:49'",2000,1,0;
--exec page_update 'C_Application','*','AppKey',"Load_Date = '2016-12-21 00:00:00' and load_start_time = '1900-01-01 18:43:49'",2000,2,0; --
--declare @totalCount int
--exec page_update 'C_Application','*','AppKey',"Load_Date = '2016-12-21 00:00:00' and load_start_time = '1900-01-01 18:43:49'",1000,2,@totalCount
--select @totalCount as totalCount GO

PS:如上两种仅仅提供了两种实现的思想基础和方法,感兴趣的童鞋,可自行将其完善,需要自行创建测试验证数据。完善后可恢复哦 ^_^

其实,在日常的工作生活中,我们经常会遇到各种各样的问题,需要我们去解决,只是每个人面对问题时的处理方式不同。但是,从根本上来讲,解决问题的方法途径有很多种,关键是你能否快速的利用你已经掌握的知识,结合现有的可用资源,进行灵活的知识变现和资源整合,以更快更好的解决问题。技术仅仅是手段,思想方法才是核心!