mysql_binlog_summary

import heapq


def get_table_summary(dump_sql_path):
    print("正在解析数据文件{0}".format(dump_sql_path))
    file_handler = open(dump_sql_path)
    file_line = file_handler.readline()
    table_dict = dict()
    while file_line:
        if file_line.find("Table_map: ") > 0:
            start_index = file_line.index("Table_map:") + len("Table_map: ")
            end_index = file_line.index("mapped to", start_index)
            table_name = file_line[start_index:end_index].strip()
            if table_name in table_dict.keys():
                table_dict[table_name] += 1
            else:
                table_dict[table_name] = 1
            file_line = file_handler.readline()
    print("##=============按照表访问排序====================##")
    sorted_table_dict = sorted(table_dict.items(), key=lambda x: int(x[1]), reverse=True)
    for item in sorted_table_dict:
        print("{0}:{1}".format(item[0], item[1]))
    print("##=============================================##")


def get_binlog_time_summary(dump_sql_path):
    print("正在解析数据文件{0}".format(dump_sql_path))
    file_handler = open(dump_sql_path)
    file_line = file_handler.readline()
    datetime_dict = dict()
    while file_line:
        if file_line.find("Table_map: ") > 0:
            start_index = 1
            end_index = file_line.index("server id", start_index)
            current_time = file_line[start_index:end_index].strip()
            if current_time in datetime_dict.keys():
                datetime_dict[current_time] += 1
            else:
                datetime_dict[current_time] = 1
            file_line = file_handler.readline()
    print("##=============按照表时间访问排序====================##")
    sorted_time_dict = sorted(datetime_dict.items(), key=lambda x: x[0], reverse=True)
    for item in sorted_time_dict:
        print("{0}:{1}".format(item[0], item[1]))
    print("##=============================================##")


def get_exec_time_summary(dump_sql_path):
    print("正在解析数据文件{0}".format(dump_sql_path))
    file_handler = open(dump_sql_path)
    file_line = file_handler.readline()
    datetime_dict = dict()
    while file_line:
        if file_line.find("exec_time=") > 0:
            start_index = 1
            end_index = file_line.index("server id", start_index)
            current_time = file_line[start_index:end_index].strip()
            start_index = file_line.index("exec_time=") + len("exec_time=")
            end_index = file_line.index("exec_time=", start_index)
            exec_time = file_line[start_index:end_index].strip()
            if current_time in datetime_dict.keys():
                datetime_dict[current_time] += 1
            else:
                datetime_dict[current_time] = 1
            file_line = file_handler.readline()
    print("##=============按照表时间访问排序====================##")
    sorted_time_dict = sorted(datetime_dict.items(), key=lambda x: x[0], reverse=True)
    for item in sorted_time_dict:
        print("{0}:{1}".format(item[0], item[1]))
    print("##=============================================##")


if __name__ == '__main__':
    sql_path = "d:\\006416_table.sql"
    get_table_summary(dump_sql_path=sql_path)
    get_binlog_time_summary(dump_sql_path=sql_path)

  

posted on 2018-06-14 14:58  笑东风  阅读(119)  评论(0编辑  收藏  举报

导航