Detailed explanation of mysql lock mechanism and principle

foreword

Whether it is a database or many back-end programming languages, there is a lock mechanism. The existence of locks effectively solves the preemption of common resources under concurrency and ensures the stability and consistency of data. How does lock work in mysql? What about? What is its underlying working principle? This article will introduce the mechanism of mysql lock in detail.

Introduction to mysql lock

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.

In MySQL, according to the granularity of the lock, it can be divided into the following three categories:

  • Global lock: lock all tables in a database;
  • Table-level lock: each operation locks the entire table;
  • Row-level lock: Each operation locks the corresponding row data;

Of course, there can be other different dimensions for the classification of locks. A more detailed division is listed below

From the perspective of operation types, mysql locks can be directly divided into: read locks and write locks;

  • Read lock: also known as shared lock, represented by S in English. For the same piece of data, the read operations of multiple transactions can be performed simultaneously without affecting each other and without blocking each other.

Guess you like

Origin blog.csdn.net/zhangcongyi420/article/details/127595389