通過Python進行MySQL表信息統計

導讀 在上一篇文章中簡單的介紹了使用python-mysql-replication 來解析 MySQL binlog來完成實時統計的業務,當然,在現實的業務中不可能用的那麼簡單的。
囉哩八說

今天的目的不是介紹真實的業務場景如何使用 python-mysql-replication,而是推出一枚<MySQL表信息統計>小工具(筆者通過python-mysql-replication實現的)

工具鏈接: https://github.com/daiguadaidai/mysql_binlog_stat

在之前我也是使用 @吳炳錫 大神的工具, 因爲在了的時候感覺顯示的有點生澀因此在他的基礎上修改了一些重新展示了, 可是在用的時候還是不盡人意。

具體看之前筆者的文章: http://www.ttlsa.com/mysql/mysql-fields-when-time-split/

大神的工具: https://github.com/wubx/mysql-binlog-statistic

筆者重新展示的工具: https://github.com/daiguadaidai/mysql-binlog-statistic

二話不說直接展示如何使用
1、查看幫助
[root@centos7 tmp]# python mysql_binlog_stat.py --help
usage: mysql_binlog_stat.py [-h] [--host HOST] [--port PORT]
                            [--username USERNAME] [--password PASSWORD]
                            [--log-file binlog-file-name]
                            [--log-pos binlog-file-pos]
                            [--server-id server-id] [--slave-uuid slave-uuid]
                            [--blocking False/True] [--start-time start-time]
                            [--sorted-by insert/update/delete]
 
Description: The script parse MySQL binlog and statistic column.
 
optional arguments:
  -h, --help            show this help message and exit
  --host HOST           Connect MySQL host
  --port PORT           Connect MySQL port
  --username USERNAME   Connect MySQL username
  --password PASSWORD   Connect MySQL password
  --log-file binlog-file-name
                        Specify a binlog name
  --log-pos binlog-file-pos
                        Specify a binlog file pos
  --server-id server-id
                        Specify a slave server server-id
  --slave-uuid slave-uuid
                        Specify a slave server uuid
  --blocking False/True
                        Specify is bloking and parse, default False
  --start-time start-time
                        Specify is start parse timestamp, default None,
                        example: 2016-11-01 00:00:00
  --sorted-by insert/update/delete
                        Specify show statistic sort by, default: insert

主要參數介紹:

--log-file: binlog 文件名稱

--log-pos: binlog 文件位置(從哪個位置開始解析)

--blocking: 是否需要使用阻塞的方式進行解析始終爲 False 就好(默認就是False)

--start-time: 從什麼時間開始解析

--sorted-by: 展示的結果通過什麼來排序, 默認是通過 insert 的行數的多少降序排列, 設置的值有 insert/update/delete

2、解析 MySQL binlog
root@(none) 09:17:12>show binary logs;
 ------------------ ----------- 
| Log_name         | File_size |
 ------------------ ----------- 
| mysql-bin.000012 | 437066170 |
| mysql-bin.000013 | 536884582 |
| mysql-bin.000014 | 537032563 |
| mysql-bin.000015 | 536950457 |
| mysql-bin.000016 |  87791004 |
| mysql-bin.000017 |       143 |
| mysql-bin.000018 |       143 |
| mysql-bin.000019 |       143 |
| mysql-bin.000020 |       143 |
| mysql-bin.000021 |      1426 |
 ------------------ ----------- 
10 rows in set (0.01 sec)
 
 
# 使用命令
[root@centos7 tmp]# time python mysql_binlog_stat.py --log-file=mysql-bin.000012 --log-pos=120 --username=root --password=root --sorted-by='insert' 
[
    {
        "app_db.business_item_sku_detail": {
            "row_insert_count": {
                "market_price": 273453,
                "sku_id": 273453,
                "weight": 273453
            },
            "table_dml_count": {
                "insert": 273453,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    },
    {
        "app_db.business_item_sku_property": {
            "row_insert_count": {
                "sku_id": 273112,
                "created": 273112,
                "property_value_id": 273112,
                "business_item_id": 273112,
                "record_id": 273112,
                "property_id": 273112
            },
            "table_dml_count": {
                "insert": 273112,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    },
    {
        "app_db.business_item_pic": {
            "row_insert_count": {
                "created": 270993,
                "business_item_id": 270993,
                "pic_id": 270993,
                "pic_no": 270993,
                "tmall_shop_id": 270993,
                "pic_url": 270993
            },
            "table_dml_count": {
                "insert": 270993,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    },
    {
        "app_db.business_item": {
            "row_insert_count": {
                "guide_commission": 264803,
                "commission_type": 264803,
                "pstatus": 264803
            },
            "table_dml_count": {
                "insert": 264803,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    },
    {
        "test.t_binlog_event": {
            "row_insert_count": {
                "auto_id": 5926,
                "dml_sql": 5926,
                "dml_start_time": 5926,
                "dml_end_time": 5926,
                "start_log_pos": 5926,
                "db_name": 5926,
                "binlog_name": 5926,
                "undo_sql": 5926,
                "table_name": 5926,
                "end_log_pos": 5926
            },
            "table_dml_count": {
                "insert": 5926,
                "update": 0,
                "delete": 4017
            },
            "row_update_count": {}
        }
    },
    {
        "test.ord_order": {
            "row_insert_count": {
                "order_id": 184,
                "pay_type": 181,
                "amount": 184,
                "create_time": 184,
                "serial_num": 181
            },
            "table_dml_count": {
                "insert": 184,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    },
    {
        "test.t1": {
            "row_insert_count": {
                "id": 7,
                "name": 7
            },
            "table_dml_count": {
                "insert": 7,
                "update": 2,
                "delete": 2
            },
            "row_update_count": {
                "name": 2
            }
        }
    },
    {
        "test.area": {
            "row_insert_count": {},
            "table_dml_count": {
                "insert": 0,
                "update": 0,
                "delete": 0
            },
            "row_update_count": {}
        }
    }
]
 
real    5m42.982s
user    5m26.080s
sys     0m8.958s

分析了2G多的binlog數據花了大概6分鐘時間速度,感覺速度還是不行啊 ^_^。

這邊說一下爲什麼不提供 --stop-log-file, --stop-log-pos, --stop-time 參數

主要是因爲 pymysqlreplication 的解析如果是沒有解析到結尾,它在 master 上創建的鏈接會一直存在不會消失,需要人工去master kill掉相關的thread. 我問了作者但是並沒有得到很好的迴應(估計也是我問問題的方式不對吧)

相關問題請看以下鏈接,(同時希望大家一起來解決一這問題, 來完善以下這個小程序)。


本文轉載自:http://www.linuxprobe.com/mysql-table-columns-statistics.html

免費提供最新Linux技術教程書籍,爲開源技術愛好者努力做得更多更好,開源站點:http://www.linuxprobe.com/


發佈了266 篇原創文章 · 獲贊 35 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章