python自動結束mysql慢查詢會話的實例代碼
更新時間:2019年10月27日 14:23:47 作者:微微
這篇文章主要介紹了python自動結束mysql慢查詢會話,主要涉及到了mysql慢查詢會話查詢,定時任務的相關知識,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
生產環(huán)境的有些sql查詢寫得太復雜,或是表很大,對應索引未建立或建立不合理,或是查詢未充分使用索引等,就有可能出現(xiàn)慢查詢,一些慢查詢需要修改程序,可能沒那么快能解決,這時如果有個腳本能自動檢測符合條件的慢查詢會話并結束,那么是很方便的,當然運維人員也可順便弄個檢測慢查詢并告警的腳本。
涉及知識點
- mysql慢查詢會話查詢
- schedule定時任務調度
- pymysql執(zhí)行sql
代碼分解
mysql慢查詢
#會話查詢,只能查詢所有會話,不能按條件過濾,不過比較好記
show PROCESSLIST;
#從information_schema中查詢會話,可以按條件過濾
SELECT
*
FROM
information_schema.`PROCESSLIST`;
#查詢符合條件的慢會話,id是會話ID,info是正在執(zhí)行的sql,time是會話持續(xù)時間,殺會話時注意要做好過濾
SELECT
id,
info,
time
FROM
information_schema.`PROCESSLIST`
WHERE
info LIKE '%select * from table%'
AND time > 10;
#直接使用sql批量殺會話,拼接kill xxx;后,拷貝了在控制臺執(zhí)行
SELECT
concat('KILL ', id, ';')
FROM
information_schema.`PROCESSLIST`
WHERE
info LIKE '%select * from table%'
AND time > 10;
腳本主入口
if __name__ == '__main__':
#每5秒執(zhí)行檢查任務
schedule.every(5).seconds.do(kill_slow)
#此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務,有就執(zhí)行
while True:
schedule.run_pending()
time.sleep(1)
schedule的其它示例
import schedule
import time
def job(message='stuff'):
print("I'm working on:", message)
#每10分鐘
schedule.every(10).minutes.do(job)
#每小時
schedule.every().hour.do(job, message='things')
#每天10點30分
schedule.every().day.at("10:30").do(job)
while True:
schedule.run_pending()
time.sleep(1)
pymysql使用
# 連接數(shù)據(jù)庫,設置結果集用dict返回,autocommit自動提交事務
db = pymysql.connect(host='localhost', db='dbname',
user='root', passwd='admin',
port=3306, charset='utf8',
cursorclass=pymysql.cursors.DictCursor, autocommit=True)
cursor = db.cursor()
查詢符合條件的慢會話并結束
def kill_slow():
cursor.execute(
"""
SELECT
id,
info,
time
FROM
information_schema.`PROCESSLIST`
WHERE
info LIKE '%select * from table%'
AND time > 10;
""")
slow_sessions = cursor.fetchall()
for slow_session in slow_sessions:
print("slow session detected, kill it:\n id:%s\nsql:%s" % (
slow_session[0], slow_session[1]))
cursor.execute("kill %s", slow_session[0])
完整代碼
import time
import pymysql
import schedule
# 連接數(shù)據(jù)庫,設置結果集用dict返回,autocommit自動提交事務
db = pymysql.connect(host='localhost', db='dbname',
user='root', passwd='admin',
port=3306, charset='utf8',
cursorclass=pymysql.cursors.DictCursor, autocommit=True)
cursor = db.cursor()
def kill_slow():
cursor.execute(
"""
SELECT
id,
info,
time
FROM
information_schema.`PROCESSLIST`
WHERE
info LIKE '%select * from table%'
AND time > 10;
""")
slow_sessions = cursor.fetchall()
for slow_session in slow_sessions:
print("slow session detected, kill it:\n id:%s\nsql:%s" % (
slow_session[0], slow_session[1]))
cursor.execute("kill %s", slow_session[0])
if __name__ == '__main__':
# 每5秒執(zhí)行檢查任務
schedule.every(5).seconds.do(kill_slow)
# 此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務,有就執(zhí)行
while True:
schedule.run_pending()
time.sleep(1)
總結
以上所述是小編給大家介紹的python自動結束mysql慢查詢會話的實例代碼,希望對大家有所幫助!

