您的位置:首页 > 编程语言 > C语言/C++

c++ odbc方式连接mysql产生预处理问题的跟踪

2017-05-31 18:21 721 查看
     在程序中如果产生预处理没有关闭,通过mysql二进制日志是很难跟踪的,要跟踪mysql预处理未关闭的情况,可以打开普通日志,在使用python进行数据分析,这样虽然也不能准确定位哪有问题,但是可以排除很多种情况,可以参考一下这个分析脚本

# -*- coding: utf-8 -*

# -*- coding: utf-8 -*

import time
import os
file = open("general.log")
# TIME_STR = "170401 "
TIME_STR = "1704"
PREPARE = "Prepare"
CLOSE_STMT = "Close stmt"

list_sql_once = []
count_prepare = 0
count_stmt = 0
count_line = 0

temp_count_prepare = []
temp_count_stmt = []
temp_total_prepare = 0
temp_total_stmt = 0

dict_count = {}
temp_get_sql = []
GET_SQL = 3952278

while 1:
line = file.readline()
count_line = count_line + 1
list_temp = []

# 统计 第 3*9988行 到 3 *10000
#     if count_line < 1*1500 :
#         continue
#     if count_line > 150*10000:
#         break

#     print "count_line ========================================================",count_line
if not line or count_line > 1000*10000:
print "count_line ========================================================",count_line
break

# 找到 prepare 的位置
index_prepare = line.find(PREPARE);
index_close_stmt = line.find(CLOSE_STMT);
# 获取字典value的值 如果没有则为0
if index_prepare != -1:
# 把prepare前面的进程号设置为int值 作为key
get_int_str = line[0:index_prepare]
try:
key_int = int(get_int_str)
except Exception as e:
get_int_str.rstrip()
list_temp = get_int_str.split("\t")
# 获取进程号 作为key值
key_int = int(list_temp[1])
if not dict_count.has_key(key_int) :
# 如果没有 这个键值{线程号:计数量}对消息,则创建键值对
dict_count[key_int] = 1
else:
# 如果有 则 加1
value_int = dict_count[key_int]
value_int = value_int + 1
dict_count[key_int] = value_int
if key_int == GET_SQL:
# 获取 pre 对应的一行sql
temp_get_sql.append(line)

if index_close_stmt != -1:
# 把 close_stmt 前面的进程号设置为int值 作为key
get_int_str = line[0:index_close_stmt]
try:
key_int = int(get_int_str)
except Exception as e:
get_int_str.rstrip()
list_temp = get_int_str.split("\t")
key_int = int(list_temp[1])

if not dict_count.has_key(key_int):
dict_count[key_int] = -1
print "--------------------here"
else:
#             print "key_int==", key_int
value_int = dict_count[key_int]
value_int = value_int - 1
dict_count[key_int] = value_int
if key_int == GET_SQL:
temp_get_sql.append(line)

for ele in dict_count:
if dict_count[ele] != 0:
print "key==",ele,"value==", dict_count[ele]
print "-----------------------------------------------------------------"

try:
fout = open("test1.txt", "w")
except IOError:
print "Error: open file failed."

count_ele = 0

for ele in  temp_get_sql:
line = str(ele) + "\n"
fout.write(line)

fout.close()

# print "count_ele == ", count_ele
only_ele_list = []
try:
fout2 = open("test2.txt", "w")
except IOError:
print "Error: open file failed."

for ele in  temp_get_sql:
if ele not in only_ele_list:
only_ele_list.append(ele)
# 把查询到的不重复sql存放到 文本文件中
for ele in only_ele_list:
line = str(ele) + "\n"
fout2.write(line)
fout2.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: