0
点赞
收藏
分享

微信扫一扫

mysql游标分段遍历统计数量

Spinach菠菜 2022-01-15 阅读 96

小白写个小代码。

因为数据库有的表数据量很大,例如几千万,每条语句的查询时长有限制,不然会影响别人使用此数据库的速度,所以建议使用mysql游标分段遍历统计数量,设置合适的分段范围,尽量减少查询时间,不影响别人。

# -*- coding:utf-8 -*-
"""
作者:孙敏
日期:2022年01月15日
"""
import pymysql
from tqdm import tqdm

#填写对应的mysql连接信息
host = 'localhost'
port = 3306
user = 'root'
password = '1234'
database = 'sunmin'
#连接mysql数据库
db =pymysql.connect(host=host,port=port,user=user,password=password,
                         database=database,charset='utf8')
#使用游标
cursor = db.cursor()


#使用sql语句对id进行分段查询database对应数据库中的表,将查询结果直接打印
def total_count(start,end,step):
    allnumber = [] #为求和函数做准备:先定义一个空列表,后续往此列表内赋值,然后使用sum函数求和
    for i in tqdm(range(start,end,step),'进度'):#代表id范围和步长
        section = list((i,i+step-1))
        cursor.execute(sql,section)#执行单个sql语句
        first_record = cursor.fetchone()#取出查询语句的第一个元素
        allnumber.append(int(first_record[0]))
    totalcount = sum(allnumber)
    print('sql查询所得总数量:'+str(totalcount))



#使用sql语句对id进行分段查询database对应数据库中的表,将查询结果生成excel表格
def output_as_a_excel_table(path,start,end,step):
    xlsx_list = []
    for i in tqdm(range(start,end,step),'进度'):#代表id范围和步长
        section = list((i,i+step-1))
        cursor.execute(sql,section)#执行单个sql语句
        first_record = cursor.fetchone()#取出查询语句的第一个元素
        y = []#定义一个列表,主要使输出结果的xlsx_list列表的元素还是列表
        b=str(section)
        y.append(b)#为xlsx_list列表元素的子列表添加第一个元素
        c=str(first_record[0])
        y.append(c)#为xlsx_list列表元素的子列表添加第二个元素
        xlsx_list.append(y)#为xlsx_list列表添加子列表元素
    result = open(path, 'w', encoding='gbk')
    # 参数'w'代表往指定表格写入数据,会先将表格中原本的内容清空
    # 若把参数’w'修改为‘a+',即可实现在原本内容的基础上,增加新写入的内容
    result.write('id区间\t对应数量\n')
    for m in range(len(xlsx_list)):
        for n in range(len(xlsx_list[m])):
            result.write(str(xlsx_list[m][n]))
            result.write('\t')  # '\t'表示每写入一个元素后,会移动到同行的下一个单元格
        result.write('\n')# 换行操作
    result.close()


if __name__ == '__main__':
    #填写对应变量的具体值和sql语句
    start = 1
    end = 29334
    step = 500
    path = 'D:\python\测试1.xls'
    sql = "select count(id) from customer where id between %s and %s"
    # 调用函数
    total_count(start,end,step)
    output_as_a_excel_table(path,start,end,step)

cursor.close()#关闭游标
db.close()#关闭数据库连接

 

 

举报

相关推荐

0 条评论