MYSQL unlock and lock table introduction

MYSQL unlock and lock table introduction

MySQL Lock Overview

Compared with other databases, MySQL's locking mechanism is relatively simple. Its most notable feature is that different storage engines support different locking 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 and table-level locking, but uses row-level locking by default.

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

Overhead, locking speed, deadlock, granularity, and concurrency performance

l Table-level lock: low overhead, fast locking; no deadlock; large locking granularity, the highest probability of lock conflict, and the lowest concurrency.
l Row-level lock: high overhead, slow locking; deadlock may occur; the locking granularity is the smallest, the probability of lock conflict is the lowest, and the concurrency is the highest.
l Page lock: The overhead and locking time are between table lock and row lock; deadlock may occur; the locking granularity is between table lock and row lock, and the concurrency is average.

MyISAM table locks

The MyISAM storage engine only supports table locks, which is the only lock type supported in the first few versions of MySQL. As applications' requirements for transaction integrity and concurrency continued to increase, 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 gradually appeared (InnoDB is actually a separate company and has now been acquired by Oracle). However, MyISAM table locks are still the most widely used lock type. This section will introduce the use of MyISAM table locks in detail.
Query table-level lock contention

Table lock contention on your system can be analyzed by examining 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, it means that there is serious table-level lock contention.

Get InnoDB row lock contention status

You can analyze the 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 lock contention is found to be serious, such as high values ​​of InnoDB_row_lock_waits and InnoDB_row_lock_time_avg, you can also

Unlock

The first

show processlist;

Find the lock process and kill its ID;

The second

mysql>UNLOCK TABLES;

Lock table

Lock 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;

You may also be interested in:
  • Mysql queries the transactions being executed and how to wait for locks
  • Understanding MySQL Locking Based on Update SQL Statements
  • PHP uses Mysql lock to solve high concurrency
  • Solution to PHP+MySQL high-concurrency locked transaction processing problem
  • Analysis of MySQL lock mechanism and usage
  • How to check where the metadata lock is blocked in MySQL
  • Analysis of the implementation of MySQL statement locking
  • Mysql uses the kill command to solve the deadlock problem (kill a certain SQL statement being executed)
  • A complete record of a Mysql deadlock troubleshooting process
  • MySQL's conceptual understanding of various locks

<<:  How to set up jar application startup on CentOS7

>>:  HTML5 and jQuery to implement preview code examples before uploading local pictures

Recommend

Will css loading cause blocking?

Maybe everyone knows that js execution will block...

Common ways to optimize Docker image size

The Docker images we usually build are usually la...

Linux concurrent execution is simple, just do it this way

Concurrency Functions time for i in `grep server ...

MySQL 5.7.17 latest installation tutorial with pictures and text

mysql-5.7.17-winx64 is the latest version of MySQ...

Application of HTML and CSS in Flash

Application of HTML and CSS in Flash: I accidental...

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

Vue implements the shake function (compatible with ios13.3 and above)

Recently, I made a function similar to shake, usi...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Summary of Nginx load balancing methods

To understand load balancing, you must first unde...

Detailed description of HTML table border control

Only show the top border <table frame=above>...

Docker online and offline installation and common command operations

1. Test environment name Version centos 7.6 docke...

MySQL learning notes: data engine

View the engines supported by the current databas...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

Detailed steps for installing and configuring MySQL 8.0 on CentOS

Preface Here are the steps to install and configu...