Determine whether MySQL update will lock the table through examples

Determine whether MySQL update will lock the table through examples

Two cases:

1. With index 2. Without index

Prerequisites:

Method: Use command line to simulate

1. Since MySQL automatically commits transactions by default, you must first check whether your current database has automatically committed transactions.

Command: select @@autocommit;

The results are as follows:

+————–+
| @@autocommit |
+————–+
| 0 |
+————–+

If it is 1, run the command: set autocommit = 0; set to disable automatic commit

2. The current database table format is as follows

tb_user | CREATE TABLE tb_user (
id bigint(20) NOT NULL AUTO_INCREMENT,
name varchar(32) DEFAULT NULL,
phone varchar(11) DEFAULT NULL,
operator varchar(32) DEFAULT NULL,
gmt_create datetime DEFAULT NULL,
gmt_modified datetime DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

Obviously, I didn't add any indexes except the primary key.

Practical example:

1. No Index

Run the command: begin; to start the transaction, and then run the command: update tb_user set phone=11 where name=”c1”; to make the changes. Do not commit the transaction yet.

Open another window and run the command directly: update tb_user set phone=22 where name=”c2”; You will find that the command is stuck. However, when the previous transaction is submitted through commit, the command will run normally and end, indicating that the table is locked.

2. Add an index to the name field

create index index_name on tb_user(name);

Then continue the operation as in step 1, that is, open a transaction and run update tb_user set phone=11 where name=”c1”; do not commit yet

Then run another command update tb_user set phone=22 where name=”c2”; and you will find that the command will not get stuck, indicating that the table is not locked.

But if another update tb_user set phone=22 where name=”c1”; updates the same row, it means the row is locked.

3. Summary

If there is no index, the update will lock the table. If an index is added, the row will be locked.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Will Update in a Mysql transaction lock the table?
  • Analysis of the locking mechanism of MySQL database
  • Detailed explanation of mysql deadlock checking and deadlock removal examples
  • The normal method of MySQL deadlock check processing
  • How to query whether the mysql table is locked
  • Pessimistic locking and optimistic locking in MySQL
  • Detailed explanation of the meaning and difference between MySQL row locks and table locks
  • Understanding and application analysis of mysql pessimistic locking and optimistic locking
  • MySQL 8.0.19 supports locking an account after entering an incorrect password three times (example)
  • Summary of MySQL lock related knowledge

<<:  Vue3.0 implements the encapsulation of the drop-down menu

>>:  Idea configures tomcat to start a web project graphic tutorial

Recommend

Use of Linux date command

1. Command Introduction The date command is used ...

JavaScript Closures Explained

Table of contents 1. What is a closure? 2. The ro...

In-depth explanation of the style feature in Vue3 single-file components

Table of contents style scoped style module State...

Java+Tomcat environment deployment and installation process diagram

Next, I will install Java+Tomcat on Centos7. Ther...

Analysis of the pros and cons of fixed, fluid, and flexible web page layouts

There is a question that has troubled web designe...

Differences between MySQL MyISAM and InnoDB

the difference: 1. InnoDB supports transactions, ...

Detailed explanation of MySQL database index

Table of contents 1. Introduction to MySQL Index ...

Exploration of three underlying mechanisms of React global state management

Table of contents Preface props context state Sum...

A brief discussion on mobile terminal adaptation

Preface The writing of front-end code can never e...

Linux concurrent execution is simple, just do it this way

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

Install .NET 6.0 in CentOS system using cloud server

.NET SDK Download Link https://dotnet.microsoft.c...

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

Tutorial on using Multitail command on Linux

MultiTail is a software used to monitor multiple ...

WeChat applet implements search function and jumps to search results page

Search Page: search.wxml page: <view class=&qu...