mysql 解除死锁_mysql查询慢及如何解除死锁状态

查询慢的原因: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 DESC

EXPLAIN

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 50

show 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 DESC

EXPLAIN

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 DESC

SELECT

concat('kill',' ', trx_mysql_thread_id,';')

FROM

information_schema.innodb_trx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值