python自動結(jié)束mysql慢查詢會話的實例代碼
生產(chǎn)環(huán)境的有些sql查詢寫得太復(fù)雜,或是表很大,對應(yīng)索引未建立或建立不合理,或是查詢未充分使用索引等,就有可能出現(xiàn)慢查詢,一些慢查詢需要修改程序,可能沒那么快能解決,這時如果有個腳本能自動檢測符合條件的慢查詢會話并結(jié)束,那么是很方便的,當(dāng)然運維人員也可順便弄個檢測慢查詢并告警的腳本。
涉及知識點
- mysql慢查詢會話查詢
- schedule定時任務(wù)調(diào)度
- 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í)行檢查任務(wù)
schedule.every(5).seconds.do(kill_slow)
#此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務(wù),有就執(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ù)庫,設(shè)置結(jié)果集用dict返回,autocommit自動提交事務(wù)
db = pymysql.connect(host='localhost', db='dbname',
user='root', passwd='admin',
port=3306, charset='utf8',
cursorclass=pymysql.cursors.DictCursor, autocommit=True)
cursor = db.cursor()
查詢符合條件的慢會話并結(jié)束
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ù)庫,設(shè)置結(jié)果集用dict返回,autocommit自動提交事務(wù)
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í)行檢查任務(wù)
schedule.every(5).seconds.do(kill_slow)
# 此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務(wù),有就執(zhí)行
while True:
schedule.run_pending()
time.sleep(1)
總結(jié)
以上所述是小編給大家介紹的python自動結(jié)束mysql慢查詢會話的實例代碼,希望對大家有所幫助!
相關(guān)文章
Python實戰(zhàn)之MNIST手寫數(shù)字識別詳解
MNIST數(shù)據(jù)集是機器學(xué)習(xí)領(lǐng)域中非常經(jīng)典的一個數(shù)據(jù)集,由60000個訓(xùn)練樣本和10000個測試樣本組成,每個樣本都是一張28 * 28像素的灰度手寫數(shù)字圖片。本文主要介紹了通過 MNIST實現(xiàn)手寫數(shù)字識別,需要的可以參考一下2022-01-01
python將字符串列表轉(zhuǎn)換為數(shù)值列表的幾種方法
這篇文章主要介紹了python將字符串列表轉(zhuǎn)換為數(shù)值列表的幾種方法,包括使用map()和float()/int()、列表推導(dǎo)式、pandas庫以及numpy庫,每種方法都有其適用場景,如小型列表、靈活轉(zhuǎn)換、處理復(fù)雜數(shù)據(jù)集和大規(guī)模數(shù)據(jù)計算等,需要的朋友可以參考下2025-04-04
conda創(chuàng)建pytorch環(huán)境報錯
這篇文章主要介紹了conda創(chuàng)建pytorch環(huán)境報錯,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04

