Detailed Explanation of All MySQL-SQL Locks (Part 1)

​♥️Author: Xiao Liu at Station C

♥️Personal homepage: Xiao Liu's homepage

♥️Efforts may not necessarily pay off, but there will be gains! Come on! Work hard together for a better life!

♥️ Learn the operation and maintenance experience summed up in two years, and a full set of network experiment tutorials for Cisco simulators. Column: Cloud Computing Technology Column

♥️Xiao Liu private message can ask casually, as long as you know it, you will not be stingy, thank you CSDN for letting you and me meet!

Table of contents

MySQL

SQL

Lock

1 Overview

2 global locks

2.1 Introduction

2.2 Grammar

1). Add a global lock

2). Data backup

3). Release the lock

2.3 Features

3 Table-level locks

3.1 Introduction

3.2 Table lock

A. Read lock

B. Write lock

test:

3.3 Metadata lock

 Demo:

 3.4 Intention lock

1 Introduction

 2). Classification\


MySQL

MySQL is a relational database management system, developed by the Swedish company MySQL AB, which is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System, relational database management system) application software. MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility. The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual authorization policy, which is divided into community edition and commercial edition. Due to its small size, fast speed, low overall cost of ownership, especially the open source feature, MySQL is generally chosen as the website for the development of small, medium and large websites. database.

SQL

Structured Query Language (Structured Query Language), referred to as SQL, is a special-purpose programming language. It is a database query and programming language for accessing data and querying, updating and managing relational database systems. Structured Query Language is a high-level non-procedural programming language that allows users to work on high-level data structures. It does not require the user to specify the data storage method, nor does it require the user to understand the specific data storage method, so different database systems with completely different underlying structures can use the same structured query language as the interface for data input and management. Structured query language statements can be nested, which makes it extremely flexible and powerful.

Lock

1 Overview

A lock is a mechanism by which a computer coordinates concurrent access to a resource by multiple processes or threads. In a database, in addition to the contention of traditional computing resources (CPU, RAM, I/O), data is also a resource shared by many users. How to ensure the consistency and validity of concurrent access to data is a problem that all databases must solve, and lock conflicts are also an important factor affecting the performance of concurrent access to databases. From this perspective, locks are particularly important and more complex for databases.

The locks in MySQL are divided into the following three categories according to the lock granularity:

Global lock: locks all tables in the database.

Table-level lock: each operation locks the entire table.

Row-level lock: Each operation locks the corresponding row data.

2 global locks

2.1 Introduction

The global lock is to lock the entire database instance. After locking, the entire instance is in a read-only state. Subsequent DML write statements, DDL statements, and transaction commit statements that have been updated will be blocked.

Its typical usage scenario is to do a logical backup of the entire database and lock all tables to obtain a consistent view and ensure data integrity.

Why do you need to add a lock for the logical backup of the whole database?

A. Let's first analyze the possible problems without adding a global lock.

Suppose there are three tables in the database: tb_stock inventory table, tb_order order table, tb_orderlog order date

log table.

 When performing data backup, first back up the tb_stock inventory table. 

Then next, in the business system, the order is placed, the inventory is deducted, and the order is generated (update the tb_stock table, insert

tb_order table).

Then execute the logic of backing up the tb_order table.

Insert order log operation in business.

Finally, back up the tb_orderlog table.

There are problems with the data backed up at this time. Because of the data backed up, the data in the tb_stock table and the tb_order table are different

To (there is the order information of the latest operation, but the inventory has not decreased).

So how to avoid this kind of problem? At this time, it can be solved with the help of MySQL's global lock.

B. Let’s analyze the situation after adding the global lock

Before logically backing up the database, first add a global lock to the entire database. Once the global lock is added, other DDL and DML are all in a blocked state, but DQL statements can be executed, that is, they are in a read-only state, and the data A backup is a query operation. Then, during the process of data logical backup, the data in the database will not change, thus ensuring the consistency and integrity of the data.

2.2 Grammar

1). Add a global lock

flush tables with read lock ;

2). Data backup

mysqldump -uroot –p1234 itcast > itcast.sql

Instructions related to data backup will be explained in detail in the following MySQL management chapter.

3). Release the lock

unlock tables ;

2.3 Features

Adding a global lock to the database is a relatively heavy operation, and there are the following problems:

If it is backed up on the main library, no updates can be performed during the backup period, and the business basically has to be shut down.

If it is backed up on the slave library, the slave library cannot execute the binary log (binlog) synchronized from the master library during the backup period, which will lead to

Cause master-slave delay.

In the InnoDB engine, we can add the parameter --single-transaction parameter during backup to complete the consistency without locking

permanent data backup.

mysqldump --single-transaction -uroot –p123456 itcast > itcast.sql

3 table-level locks

3.1 Introduction

Table-level locks lock the entire table for each operation. The locking granularity is large, the probability of lock conflicts is the highest, and the concurrency is the lowest. It is used in storage engines such as MyISAM, InnoDB, and BDB.

Table-level locks are mainly divided into the following three categories:

table lock

Meta data lock (meta data lock, MDL)

intent lock

3.2 Table lock

For table locks, there are two categories:

Table shared read lock (read lock)

Table exclusive write lock (write lock)

grammar:

Locking: lock tables table name... read/write.

Release lock: unlock tables / client disconnects.

Features:

A. Read lock

Client 1 on the left has a read lock on the specified table, which will not affect the reading of client 2 on the right, but will block the writing of the client on the right.

test:

B. Write lock

 On the left is client 1, which has a write lock on the specified table, which will block the reading and writing of the client on the right.

test:

 Conclusion: Read locks will not block other clients' reads, but will block writes. A write lock will block both the reading of other clients and the writing of other clients.

3.3 Metadata lock

meta data lock, metadata lock, MDL for short.

The MDL locking process is automatically controlled by the system and does not need to be used explicitly. It will be added automatically when accessing a table. The main function of the MDL lock is to maintain the data consistency of the table metadata. When there are active transactions on the table, the metadata cannot be written. In order to avoid conflicts between DML and DDL , ensure the correctness of reading and writing.

The metadata here can be simply understood as the table structure of a table. That is to say, when a certain table involves uncommitted transactions, the table structure of this table cannot be modified.

MDL was introduced in MySQL5.5. When adding, deleting, modifying and querying a table, add MDL read lock (shared); when changing the table structure, add MDL write lock (exclusive).

Metadata locks added during common SQL operations:

 Demo:

When executing SELECT, INSERT, UPDATE, DELETE and other statements, metadata shared locks (SHARED_READ / SHARED_WRITE) are added, which are compatible.

When the SELECT statement is executed, the metadata shared lock (SHARED_READ) is added, which will block the metadata exclusive lock

(EXCLUSIVE), are mutually exclusive.

 We can view the metadata locks in the database through the following SQL:

select object_type,object_schema,object_name,lock_type,lock_duration from
performance_schema.metadata_locks ;

During the operation, we can check the locking status of the metadata lock through the above SQL statement.

 3.4 Intention lock

1 Introduction

In order to avoid conflicts between row locks and table locks added during DML execution, intent locks are introduced in InnoDB, so that table locks do not need to check whether each row of data is locked, and use intent locks to reduce table lock checks.

If there is no intent lock, after the client adds a row lock to a pair of tables, how does client 2 add a table lock to the table? Let’s briefly analyze it through the schematic diagram:

First, the client one starts a transaction, and then executes a DML operation. When executing a DML statement, a row lock will be added to the rows involved.

When client 2 wants to add a table lock to this table, it will check whether the current table has a corresponding row lock. If not, add a table lock. At this time, it will check from the first row of data to the last row of data. less efficient.

 After having the intent lock:

Client 1, when executing a DML operation, will add row locks to the rows involved, and will also add intent locks to the table.

And other clients, when adding a table lock to this table, will determine whether the table lock can be successfully added according to the intent lock added to the table, instead of judging the row lock situation row by row.

 2). Classification\

Intent shared lock (IS): added by the statement select ... lock in share mode. shared lock with table lock

(read) compatible, mutually exclusive with the table lock exclusive lock (write).

Intent exclusive lock (IX): added by insert, update, delete, select...for update. Shared with table lock

Shared locks (read) and exclusive locks (write) are mutually exclusive, and intent locks are not mutually exclusive.

Once the transaction is committed, the intent shared lock and the intent exclusive lock will be released automatically.

You can use the following SQL to view the locking status of intent locks and row locks:

select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from
performance_schema.data_locks;

Demonstration: A. Intent shared locks are compatible with table read locks

 B. Intentional exclusive locks are mutually exclusive with table read locks and write locks

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/131384399