Case analysis of several MySQL update operations

Case analysis of several MySQL update operations

This article will analyze the pros and cons of several data update operations through a case study of updating a user account balance. I hope this helps you all 🐶.

Database version: mysql 5.7.23

Case Study

DDL to create a database:

CREATE TABLE `hw_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `balance` int(11) DEFAULT NULL,
  `status` varchar(20) DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

Update account balance

Direct Update

Solution 1: Update after query

# Data query select * from hw_account where id = 1;

# Update data update hw_account set balance = 5 where id = 1;

The problem is that if the operation is divided into two parts and executed concurrently, the update may be lost.

Optimistic locking scheme

Use the version number operation, that is, add an optimistic lock to the database.

# Data query select * from hw_account where id = 1;

# Update data update hw_account set balance = 5 , version = version + 1 
  where id = 1 and version = n;
  
# Determine whether it is successful if row < 1 {
   rollback}

The existing problem is that if this piece of data is operated concurrently, other requests will fail. If the front-end link of this request is relatively long, the rollback cost will be relatively high.

Lock-free solution

No query is required, database calculation is used, and version number operation is not required. Validity is judged directly through domain values. The specific SQL is as follows:

# Update data update hw_account set balance = balance + @change_num , version = version + 1 
  where id = 1 and version = n;
  
# Determine whether it is successful if row < 1 {
   rollback}

This solution is relatively simple to modify, but it relies on data calculations and does not feel particularly user-friendly.

Queuing Operations

Data requests are queued through redis or zk's distributed locks. Then update the data.

# Pseudo code if (get distributed lock) {
  update hw_account set balance = @balance where id = 1;
} else {
  # Enter waiting, or spin to acquire the lock}

Frequently asked questions

If the update_time field exists in the data, how many rows are affected?

The update_time field is defined as follows. If the data is id = 1, status = 1, and the SQL statement for updating the data is

update hw_account set `status` = 1 where id = 1;

The number of affected rows returned is 0;

If an update is executed but the number of rows affected is 0, will a row lock be added?

Yes, all update statements will add row locks (prerequisite, within a transaction)

References

mysql.com

This is the end of this article about case analysis of several MySQL update operations. For more relevant MySQL update operation content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL database terminal - common operation command codes
  • MySQL database aggregate query and union query operations
  • Detailed basic operations on data tables in MySQL database
  • MySQL database operations and data types
  • Specific use of MySQL operators (and, or, in, not)
  • MySQL 8.0 can now handle JSON
  • Summary of MySQL advanced operation instructions

<<:  Introduction to commonly used MySQL commands in Linux environment

>>:  Detailed explanation of CSS3 media query responsive layout bootstrap framework principle practice (recommended)

Recommend

How to solve the problem of too many open files in Linux

The cause is that the process opens a number of f...

Application of CSS3 animation effects in activity pages

background Before we know it, a busy year is comi...

Common rule priority issues of Nginx location

Table of contents 1. Location / Matching 2. Locat...

MySql Sql optimization tips sharing

One day I found that the execution speed of a SQL...

Why Seconds_Behind_Master is still 0 when MySQL synchronization delay occurs

Table of contents Problem Description Principle A...

Detailed explanation of mysql5.6 master-slave setup and asynchronous issues

Table of contents 1. MySQL master-slave replicati...

Automated front-end deployment based on Docker, Nginx and Jenkins

Table of contents Preliminary preparation Deploym...

How to change the character set encoding to UTF8 in MySQL 5.5/5.6 under Linux

1. Log in to MySQL and use SHOW VARIABLES LIKE &#...

In-depth interpretation of /etc/fstab file in Linux system

Preface [root@localhost ~]# cat /etc/fstab # # /e...

Can asynchrony in JavaScript save await?

I knew before that to synchronously obtain the re...

Detailed explanation of how Nginx works

How Nginx works Nginx consists of a core and modu...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...