1. 出現長時間執行的查詢的緣由sql
因爲SQL執行效率差而致使的長時間查詢:ide
因爲被SQL注入而致使的長時間查詢:測試
因爲DDL語句引發表元數據鎖等待:fetch
2. 長時間執行的查詢帶來的問題spa
一般來講,除非是BI/報表類查詢,不然長時間執行的查詢對於應用缺少意義。orm
消耗系統資源,好比大量長時間查詢可能會引發 CPU、IOPS 和/或 鏈接數 使用率太高等問題。索引
帶來系統不穩定的隱患(好比 InnoDB 引擎表上的長時間查詢可能會致使 ibdata1 系統文件尺寸的增長)事件
3. 如何避免長時間執行的查詢ssl
應用方面應注意增長防止 SQL 注入的保護。資源
在新功能模塊上線前,進行壓力測試,避免出現執行效率不好的 SQL 大量執行的狀況。
儘可能在業務低峯期進行索引建立刪除、表結構修改、表維護和表刪除操做。
4. 如何處理長時間執行的查詢
a、經過命令 show processlist; 查看當前執行會話,Kill會話長時間查詢。
b、建立事件自動清理長時間執行的查詢
create event my_long_running_query_monitor on schedule every 5 minute starts '2018-08-08 11:00:00' on completion preserve enable do begin declare v_sql varchar(500); declare no_more_long_running_query integer default 0; declare c_tid cursor for select concat ('kill ',id,';') from information_schema.processlist where time >= 3600 and user = substring(current_user(),1,instr(current_user(),'@')-1) and command not in ('sleep') and state not like ('waiting for table%lock'); declare continue handler for not found set no_more_long_running_query=1; open c_tid; repeat fetch c_tid into v_sql; set @v_sql=v_sql; prepare stmt from @v_sql; execute stmt; deallocate prepare stmt; until no_more_long_running_query end repeat; close c_tid; end;