一些查看数据库中事务和锁情况的常用语句
查询 正在执行的事务:
SELECT * FROM information_schema.INNODB_TRX
根据这个事务的线程ID(trx_mysql_thread_id):
查看事务等待状况:
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
FROM
information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;
● 1
● 2
● 3
● 4
● 5
● 6
● 7
● 8
● 9
● 10
● 11
查看更具体的事务等待状况:
SELECT
b.trx_state,
e.state,
e.time,
d.state AS block_state,
d.time AS block_time,
a.requesting_trx_id,
a.requested_lock_id,
b.trx_query,
b.trx_mysql_thread_id,
a.blocking_trx_id,
a.blocking_lock_id,
c.trx_query AS block_trx_query,
c.trx_mysql_thread_id AS block_trx_mysql_tread_id
FROM
information_schema.INNODB_LOCK_WAITS a
LEFT JOIN information_schema.INNODB_TRX b ON a.requesting_trx_id = b.trx_id
LEFT JOIN information_schema.INNODB_TRX c ON a.blocking_trx_id = c.trx_id
LEFT JOIN information_schema.PROCESSLIST d ON c.trx_mysql_thread_id = d.id
LEFT JOIN information_schema.PROCESSLIST e ON b.trx_mysql_thread_id = e.id
ORDER BY
a.requesting_trx_id;
● 1
● 2
● 3
● 4
● 5
● 6
● 7
● 8
● 9
● 10
● 11
● 12
● 13
● 14
● 15
● 16
● 17
● 18
● 19
● 20
● 21
● 22
查看未关闭的事务:
–MySQL 5.6
SELECT
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.ID,
b.USER,
b.DB,
b.COMMAND,
b.TIME,
b.STATE,
b.INFO,
c.PROCESSLIST_USER,
c.PROCESSLIST_HOST,
c.PROCESSLIST_DB,
d.SQL_TEXT
FROM
information_schema.INNODB_TRX a
LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id
AND b.COMMAND = 'Sleep'
LEFT JOIN PERFORMANCE_SCHEMA.threads c ON b.id = c.PROCESSLIST_ID
LEFT JOIN PERFORMANCE_SCHEMA.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
● 1
● 2
● 3
● 4
● 5
● 6
● 7
● 8
● 9
● 10
● 11
● 12
● 13
● 14
● 15
● 16
● 17
● 18
● 19
● 20
● 21
● 22
–MySQL 5.5
SELECT
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.ID,
b. USER,
b. HOST,
b.DB,
b.COMMAND,
b.TIME,
b.STATE,
b.INFO
FROM
information_schema.INNODB_TRX a
LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id
WHERE
b.COMMAND = 'Sleep';
● 1
● 2
● 3
● 4
● 5
● 6
● 7
● 8
● 9
● 10
● 11
● 12
● 13
● 14
● 15
● 16
● 17
● 18
查看某段时间以来未关闭事务:
SELECT
trx_id,
trx_started,
trx_mysql_thread_id
FROM
INFORMATION_SCHEMA.INNODB_TRX
WHERE
trx_started < date_sub(now(), INTERVAL 1 MINUTE)
AND trx_operation_state IS NULL
AND trx_query IS NULL;
事务查看
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- MySQL技术内幕:InnoDB存储引擎(第2版) 姜承尧 第1章 MySQL体系结构和存储引擎 >> 在上述例子...
- ORACLE: dba_users 数据库用户信息 dba_segments 表段信息 dba_extents ...
- 最新数据监控项: Aborted Clients 因客户端没有正确地关闭而被丢弃的连接的个数,数字增大意味着有客户...
- 绝大部的人从小到大的学习,完全是凭感觉的? 靠着还不赖的智商和认知能力,一步步平稳而没有什么惊喜的学着大部分“被安...