This article uses examples to explain the principles and usage of MySQL knowledge points such as transactions, indexes, and locks. Share with you for your reference, the details are as follows: Transactions
A transaction is a set of atomic SQL queries, or an independent unit of work. If the database engine executes a set of operation statements, then all operations are executed. If any of them crashes or cannot be executed for other reasons, all statements will not be executed. That is to say, all statements in a transaction are either executed successfully or all failed.
index
An index is a data structure that allows storage engine users to quickly find records. For example SELECT userName FROM user WHERE userId = 1; If you add an index to the userId column, MySQL will use the index to find the row for userId. That is, MySQL first searches the index by value and then returns all data rows that contain the value.
LockHere we mainly discuss downlink level locks
The engine MyISAM can be understood as locking the entire table. It can be read at the same time but not written at the same time. During the locking period, other processes cannot write to the table. If it is a write lock, other processes are not allowed to read.
Engine INNODB, a single row of records is locked, and can be read at the same time but not written at the same time. Row-level locks have high overhead and slow locking; deadlocks may occur; the locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest.
Since InnoDB defaults to Row-Level Lock, MySQL will only perform Row Lock if the primary key is explicitly specified. Otherwise, MySQL will perform Table Lock. Example 1: (Specify the primary key explicitly, and there is such a record, Row Lock) SELECT * FROM products WHERE id='3' FOR UPDATE; SELECT * FROM products WHERE id='3' and type=1 FOR UPDATE; Example 2: (Specify the primary key explicitly. If no such record is found, no lock is performed) SELECT * FROM products WHERE id='-1' FOR UPDATE; Example 3: (no primary key, table lock) SELECT * FROM products WHERE name='Mouse' FOR UPDATE; Example 4: (Unclear primary key, table lock) SELECT * FROM products WHERE id<>'3' FOR UPDATE; Example 5: (Primary key is unclear, table lock) SELECT * FROM products WHERE id LIKE '3' FOR UPDATE; Note 1: FOR UPDATE is only applicable to InnoDB and must be executed in a transaction block (BEGIN/COMMIT) to take effect. Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills". I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Implementation of react routing guard (routing interception)
>>: How to use Linux locate command
This article mainly introduces the installation/st...
I am using the Ubuntu 16.04 system here. Installa...
Table of contents 1. Concept 1.1 What are errors ...
[LeetCode] 196.Delete Duplicate Emails Write a SQ...
In CSS files, sometimes you need to use background...
Table of contents Effect demonstration:Main JS co...
Table of contents 1. Routing related objects 2. L...
Use wget command to download the entire subdirect...
This article does not have any quibbles, it is jus...
This article shares the MySQL 5.7.18 MSI installa...
When using MySql's window function to collect...
Table of contents 1. Database bottleneck 2. Sub-l...
This article mainly introduces: using Vue to impl...
Table of contents Step 1: Installation Step 2: Ci...
A hyperlink URL in Vm needs to be concatenated wit...