python实现批量化查询耗时SQL
今天发现最近多了一些耗时SQL的查询,例如我去数据库一张千万级表查询一天的数据需要耗时20分钟,我总共需要查询一年的数据,我总不能一条一条的手动执行吧,这样也太伤身体,属实难崩啊。就算将这些SQL都弄好放到navicat里面执行,一个窗口最多只能展示20个结果,根本不够用,所以只能另想他法。于是我就计划用python程序解决这个问题,将每次查询的结果保存到一个CSV文件里面,这样我就能等它查询之后,一键复制就行,真实老婆婆吃豆腐------放120个心,哈哈哈哈哈,下面开始上程序!!!
import csv
from pymysql import *
import time
from datetime import datetime, timedelta
conn = connect(host='xxxxxx',
port=3306,
user='xxxxxx',
password='xxxxxx',
database='xxxxxx',
charset='utf8mb4')
sites = ["bw_web", "bw_app"]
data_list = []
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 7, 22)
current_date = start_date
while current_date <= end_date:
date = current_date.strftime("%Y-%m-%d")
print(date)
rows_list = [date]
for site in sites:
cs = conn.cursor() # 获取光标
sql = f"SELECT count(1) as pv, count( DISTINCT ( token_id ) ) as uv FROM msg2024{current_date.month} WHERE server_day = '{date}' AND track_event = '0' AND site_id = '{site}'; \n"
start_time = time.time()
cs.execute(sql)
rows = cs.fetchall()
# 记录结束时间
end_time = time.time()
# 计算执行时间
execution_time = end_time - start_time
conn.commit()
pv_value = rows[0][0]
uv_value = rows[0][1]
print(f"======>track库共花费{execution_time:.6f}秒执行完毕,{sql},pv为{pv_value},uv为{uv_value}")
rows_list.append(pv_value)
rows_list.append(uv_value)
data_list.append(rows_list)
current_date += timedelta(days=1)
print(data_list)
with open('result.csv', 'w', newline='', encoding='utf-8-sig') as file:
writer = csv.writer(file)
csv_title = ['date', 'track-web-pv', 'track-web-uv', 'track-app-pv', 'track-app-uv']
writer.writerow(csv_title)
writer.writerows(data_list)
写在最后
编程精选网(www.codehuber.com),程序员的终身学习网站已上线!
如果这篇【文章】有帮助到你,希望可以给【JavaGPT】点个赞👍,创作不易,如果有对【后端技术 】、【前端领域 】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【JavaGPT】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💝💝💝!