View mysql lock View

Keywords: mysql lock contention, mysql lock View

--------------------- 
Author: Border Town cn 
Source: CSDN 
Original: https: //blog.csdn.net/miyatang/article/details/78227344 
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

View table lock case

mysql> show status like ‘Table%’;
+—————————-+——–+
| Variable_name | Value |
+—————————-+——–+
| Table_locks_immediate | 795505 |
| Table_locks_waited | 0 |
| Table_open_cache_hits | 0 |
| Table_open_cache_misses | 0 |
| Table_open_cache_overflows | 0 |
+—————————-+——–+
5 rows in set (0.00 sec)

Table_locks_immediate refers to the number of immediate access to table-level locking
Table_locks_waited refers to the number of table-level lock can not be obtained immediately but to wait, if a large quantity, showing that the lock wait, lock contention for

View being locked tables
show OPEN TABLES where In_use> 0;

mysql> show OPEN TABLES where In_use > 0;
+————–+—————+——–+————-+
| Database | Table | In_use | Name_locked |
+————–+—————+——–+————-+
| music | class_record | 1 | 0 |
| vipswoole | chat_message | 3 | 0 |
| music | user_account | 1 | 0 |
| music | sales_channel | 1 | 0 |
| music | class_room | 5 | 0 |
| music | user | 1 | 0 |
| music_school | user | 1 | 0 |
+————–+—————+——–+————-+
7 rows in set (0.00 sec)

mysql>

If you look to lock contention in serious condition, then you can view the currently executing SQL:
MySQL> Show processlist

(mysqladmin -uroot -p -P 3306 processlist)

mysqladmin debug command has a parameter, you can analyze information on the current status of MySQL services, but also can be used to help us locate the details of the current locks, here in detail the current status of MySQL services we analyze this command, execute mysqladmin command as follows:

[root@phpmysql02 data]# mysqladmin -ujss -p -S /data/3306/mysql.sock debug

Password the Enter:
Debug status information will generate an error file mysql, usually lock the information will be saved in the last few lines, we here at the operating system level error log last few lines:

[root@phpmysql02 data]# tail -10 phpmysql02.err

Locked database.table_name the Thread / Waiting lock_type
2 hdpic.t_wiki_zutu Waiting - Highest priority Write Write Lock
123 890 hdpic.t_wiki_zutu_category Locked - Low priority Read Read Lock
123 890 hdpic.t_wiki_zutu_photo Locked - Low priority Read Read Lock
123 890 hdpic.t_wiki_zutu Locked - Read Low priority lock the read
124 906 hdpic.t_wiki_zutu Waiting - Low priority the read the read lock
can be seen from the above information, 123,890 held read locks blocking write and read operations 2 124 906, and this state in line with our reasoning, the next process is more simple, and if the status quo is unacceptable and can not continue to wait, to 123,890 killed, releasing resources to:

mysql> kill 123890;

Query OK, 0 rows affected (0.00 sec)
to perform again show processlist View:

Use the system lock table query:

select r.trx_isolation_level, r.trx_id waiting_trx_id,r.trx_mysql_thread_id waiting_trx_thread,
r.trx_state waiting_trx_state,lr.lock_mode waiting_trx_lock_mode,lr.lock_type waiting_trx_lock_type,
lr.lock_table waiting_trx_lock_table,lr.lock_index waiting_trx_lock_index,r.trx_query waiting_trx_query,
b.trx_id blocking_trx_id,b.trx_mysql_thread_id blocking_trx_thread,b.trx_state blocking_trx_state,
lb.lock_mode blocking_trx_lock_mode,lb.lock_type blocking_trx_lock_type,lb.lock_table blocking_trx_lock_table,
lb.lock_index blocking_trx_lock_index,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
inner join information_schema.innodb_locks lb on lb.lock_trx_id=w.blocking_trx_id
inner join information_schema.innodb_locks lr on lr.lock_trx_id=w.requesting_trx_id \G

Table 3 illustrates involved:

three tables in information_shcema (These tables can be updated by monitoring the current transaction and analyzed for the presence of locking problems)
- (currently active print innodb kernel (ACTIVE) affairs) innodb_trx
- innodb_locks (Print this state produced innodb lock printing only when there lockwait)
- innodb_lock_waits (innodb print lock state current generated only when there is a waiting print lockwait)

1) innodb_trx table structure description (removal of the most telling of eight field)
Field Name Description
Internal trx_id innodb storage engine only thing ID
TRX_STATE
current state of affairs (running and lock wait two states)
trx_started
start time things
trx_requested_lock_id wait lock ID of things, such as the state trx_state for lock wait, then the things that take up resources before the value table with the current transaction waiting ID, if trx_state not lock wait the value is NULL
start time trx_wait_started things waiting
trx_weight right things heavy, in innodb storage engine, when the need to roll back the deadlock, innodb storage engine will choose the smallest value rollback
thread id trx_mysql_thread_id mysql, namely the results show processlist displayed
trx_query things running SQL statements
2) innodb_locks table structure Description

Field Name Description
lock_id Lock ID
ID lock_trx_id things
lock_mode lock mode (S and X locks lock modes)
lock_type lock type table row lock or locks (the RECORD)
lock_table table to be locked
lock_index locked index
lock_space lock live object ID Space
lock_page things, the number of pages locked, the lock table if the value is NULL
lock_rec things, the number of rows of locking, the lock table if the value is NULL
lock_data things primary key lock records, if the value of the lock table NULL (this option can not be trusted)
3) innodb_lock_waits table structure Description
field name Description
requesting_trx_id ID things apply lock resource
lock requested_lock_id application ID
blocking_trx_id block other things, things ID
blocking_lock_id blocking other locks lock ID

These tables can be carried out in accordance with the joint inquiry, to be more intuitive and clearer results, you can refer to the following SQL (can be adjusted according to their fitness habits analysis)

Guess you like

Origin www.cnblogs.com/gered/p/11078453.html