MySQL lock table problem handling

Overview of MySQL Lock


Compared with other databases, MySQL's lock mechanism is relatively simple, and its most notable feature is that different storage engines support different lock mechanisms. For example, the MyISAM and MEMORY storage engines use table-level locking; the BDB storage engine uses page-level locking, but also supports table-level locking; the InnoDB storage engine supports both row-level locking (row-level locking), table-level locking is also supported, but row-level locking is used by default.


The characteristics of these three types of locks in MySQL can be roughly summarized as follows.


Overhead, locking speed, deadlock, granularity, concurrency


Table-level lock: low overhead, fast locking; no deadlock; large locking granularity, the highest probability of lock conflict, and the lowest concurrency.


Row-level locks: high overhead, slow locking; deadlocks; the smallest locking granularity, the lowest probability of lock conflicts, and the highest concurrency.


Page locks: Overhead and locking time are between table locks and row locks; deadlocks can occur; locking granularity is between table locks and row locks, and the degree of concurrency is average.

MyISAM table locks The
MyISAM storage engine only supports table locks, which are the only lock types supported by MySQL in the first few versions. With the continuous improvement of the application's requirements for transaction integrity and concurrency, MySQL began to develop a transaction-based storage engine. Later, the BDB storage engine that supports page locks and the InnoDB storage engine that supports row locks appeared (actually InnoDB is a separate storage engine). a company that has now been acquired by Oracle Corporation). But MyISAM's table lock is still the most widely used lock type. This section details the use of MyISAM table locks.
Querying for table-level lock contention
You can analyze table lock contention on your system by checking the table_locks_waited and table_locks_immediate status variables:
mysql> show status like 'table%';
+-------------+-------+
| Variable_name | Value |
+------------- ------------+-------+
| Table_locks_immediate | 2979 |
| Table_locks_waited | 0 |
+------------------ -----+-------+
2 rows in set (0.00 sec))
If the value of Table_locks_waited is relatively high, there is a serious table-level lock contention.

Get InnoDB row lock contention   
You can analyze row lock contention on your system by checking the InnoDB_row_lock status variable:
mysql> show status like 'innodb_row_lock%';
+--------------- ----------------+-------+
| Variable_name | Value |
+------------------- ------------+-------+
| InnoDB_row_lock_current_waits | 0 |
| InnoDB_row_lock_time | 0 |
| InnoDB_row_lock_time_avg | 0 |
| InnoDB_row_lock_time_max | 0 |
| InnoDB_row_lock_waits | 0 |
+-------------------------------+-- -----+
5 rows in set (0.01 sec)

If you find that the lock contention is serious, for example, the values ​​of InnoDB_row_lock_waits and InnoDB_row_lock_time_avg are relatively high, you can also unlock them in the following ways.


MYSQL unlock

the first

show processlist;

find the lock process, kill id; the



second

mysql>UNLOCK TABLES;


MYSQL lock table

locks the data table to prevent the table from being updated during the backup process

mysql>LOCK TABLES tbl_name READ;


Add a write lock to the table:

mysql>LOCK TABLES tbl_name WRITE;

from: http://www.cnblogs.com/wanghuaijun/p/5949934.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306158&siteId=291194637