查询慢的原因:1.数据量大 2.加索引及索引优化,explain查看是否命中索引 3.关联查询时,2张表的编码是否一致,具体见“mysql改变表编码及注意事项”
第一种:
前提条件:找到执行非常慢的sql;
如何找呢:还原客户遇到的问题场景,从控制台找到所执行的sql,一句句的去执行,直到找到执行非常慢的sql
1.查询是否锁表
show OPEN TABLES where In_use > 0;
2.查询进程(如果您有SUPER权限,您可以看到所有线程。否则,您只能看到您自己的线程)
show processlist
3.杀死conmand为waiting的进程id(就是上面命令的id列)
kill id
第二种:
1.查看下在锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;
2.杀死进程id(就是上面命令的trx_mysql_thread_id列)
kill 线程ID
第三种:
也许你无法查看到所在的info,这个时候你需要重启服务器,如果是分布式的话,就一台台重启吧;
原理是:杀死所有进程,释放所有锁。
MySQL - 锁等待超时与information_schema的三个表:
-- 1.information_schema.innodb_trx–当前运行的所有事务
select * from information_schema.innodb_trx;-- information_schema.innodb_locks–当前出现的锁
select * from information_schema.innodb_locks;-- information_schema.innodb_lock_waits–锁等待的对应关系
select * from information_schema.innodb_lock_waits;
-------------------------------------------------------------------------------------------
//常用MySQL - 锁等待超时与information_schema的三个表:
-- 1.information_schema.innodb_trx–当前运行的所有事务
select * from information_schema.innodb_trx;-- information_schema.innodb_locks–当前出现的锁
select * from information_schema.innodb_locks;-- information_schema.innodb_lock_waits–锁等待的对应关系
select * from information_schema.innodb_lock_waits;
-------------------------------------------------------------------------------------------
//常用
select * from information_schema.innodb_trx where trx_state = 'LOCK WAIT';SELECT a.* FROM information_schema.processlist a where command <> 'sleep' ORDER BY time desc
select * from information_schema.processlist where time> 60 and user='srapp_stsj';
SELECT
concat('kill',' ', id,';')
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESCEXPLAIN
select * from sr_main where sys_scbj = '0' and mdjlx = 'ls_jz' and sys_spzt = 2
and mhzsfz = '330219193210200010'
----------------------------------------------------------------------------------------------------------------------------------------------
MySQL - 锁等待超时与information_schema的三个表:
-- 1.information_schema.innodb_trx–当前运行的所有事务
select *,trx_mysql_thread_id from information_schema.innodb_trx ;-- information_schema.innodb_locks–当前出现的锁
select * from information_schema.innodb_locks;-- information_schema.innodb_lock_waits–锁等待的对应关系
select * from information_schema.innodb_lock_waits;-------------------------------------------------------------------------------------------
伟哥:命令是用来查看当前运行的所有事务
SELECT
trx_state,
trx_started,
trx_mysql_thread_id,
trx_query,
concat(
'kill ',
trx_mysql_thread_id,
';'
)
FROM
information_schema.innodb_trx
ORDER BY
trx_started -- limit 50show processlist 看看有没有锁表
kill id
kill 34116988;kill 50403077; kill 50508905-------------------------------------------------------------------------------------------
//常用
select * from information_schema.innodb_trx where trx_state = 'LOCK WAIT';SELECT a.* FROM information_schema.processlist a where command <> 'sleep' ORDER BY time desc
select * from information_schema.processlist where time> 60 and user='srapp_stsj';
SELECT
concat('kill',' ', id,';')
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESC
----------------------------
SELECT
concat('kill',' ', id,';')
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESC
------------------------------
SELECT
*
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESCEXPLAIN
select * from sr_main where sys_scbj = '0' and mdjlx = 'ls_jz' and sys_spzt = 2
and mhzsfz = '330219193210200010'EXPLAIN
select 1 from sr_main where 1
---------------------------------------
SELECT
*
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESCSELECT
concat('kill',' ', trx_mysql_thread_id,';')
FROM
information_schema.innodb_trx