您好,登錄后才能下訂單哦!
這篇文章主要介紹python中如何自動(dòng)結(jié)束mysql慢查詢會(huì)話,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
生產(chǎn)環(huán)境的有些sql查詢寫得太復(fù)雜,或是表很大,對(duì)應(yīng)索引未建立或建立不合理,或是查詢未充分使用索引等,就有可能出現(xiàn)慢查詢,一些慢查詢需要修改程序,可能沒那么快能解決,這時(shí)如果有個(gè)腳本能自動(dòng)檢測(cè)符合條件的慢查詢會(huì)話并結(jié)束,那么是很方便的,當(dāng)然運(yùn)維人員也可順便弄個(gè)檢測(cè)慢查詢并告警的腳本。
涉及知識(shí)點(diǎn)
mysql慢查詢會(huì)話查詢
schedule定時(shí)任務(wù)調(diào)度
pymysql執(zhí)行sql
代碼分解
mysql慢查詢
#會(huì)話查詢,只能查詢所有會(huì)話,不能按條件過(guò)濾,不過(guò)比較好記 show PROCESSLIST; #從information_schema中查詢會(huì)話,可以按條件過(guò)濾 SELECT * FROM information_schema.`PROCESSLIST`; #查詢符合條件的慢會(huì)話,id是會(huì)話ID,info是正在執(zhí)行的sql,time是會(huì)話持續(xù)時(shí)間,殺會(huì)話時(shí)注意要做好過(guò)濾 SELECT id, info, time FROM information_schema.`PROCESSLIST` WHERE info LIKE '%select * from table%' AND time > 10; #直接使用sql批量殺會(huì)話,拼接kill xxx;后,拷貝了在控制臺(tái)執(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) #每小時(shí) schedule.every().hour.do(job, message='things') #每天10點(diǎn)30分 schedule.every().day.at("10:30").do(job) while True: schedule.run_pending() time.sleep(1)
pymysql使用
# 連接數(shù)據(jù)庫(kù),設(shè)置結(jié)果集用dict返回,autocommit自動(dòng)提交事務(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()
查詢符合條件的慢會(huì)話并結(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ù)庫(kù),設(shè)置結(jié)果集用dict返回,autocommit自動(dòng)提交事務(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)
以上是“python中如何自動(dòng)結(jié)束mysql慢查詢會(huì)話”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。