Home Database Mysql Tutorial MYSQL的伪行级锁_MySQL

MYSQL的伪行级锁_MySQL

Jun 01, 2016 pm 01:41 PM
Lock

bitsCN.com
MYSQL的伪行级锁 之前一直以为mysql的innodb引擎所支持的行级锁和oracle,postgresql是一样的,是对数据行上加锁。但其实是不一样的,理解不一样,对mysql的锁机制就容易产生误解。innodb的行级锁实际上是基于索引项来锁定的。以下是验证测试过程  一.数据准备mysql> use test;Database changedmysql> show create table t_kenyon /G*************************** 1. row ***************************       Table: t_kenyonCreate Table: CREATE TABLE `t_kenyon` (  `id` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec) mysql> set autocommit = 0;Query OK, 0 rows affected (0.00 sec)   mysql> show variables like '%autocommit%';+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit    | OFF   |+---------------+-------+1 row in set (0.00 sec) mysql> select * from t_kenyon;+------+| id   |+------+|    1 ||  123 ||  789 ||  345 ||   78 ||   78 |+------+6 rows in set (0.00 sec)以上是测试表t_kenyon,设置提交方式为手动提交.  二.过程(开启两个session,分别设置autocommit=off)    1.session one updatemysql> update t_kenyon set id = 999 where id = 1;Query OK, 1 row affected (0.04 sec)Rows matched: 1  Changed: 1  Warnings: 0 mysql> select * from t_kenyon;+------+| id   |+------+|  999 ||  123 ||  789 ||  345 ||   78 ||   78 |+------+6 rows in set (0.00 sec)2.session two updatemysql> show variables like 'autocommit';+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit    | OFF   |+---------------+-------+1 row in set (0.00 sec)   mysql> select * from t_kenyon;+------+| id   |+------+|    1 ||  123 ||  789 ||  345 ||   78 ||   78 |+------+6 rows in set (0.00 sec) mysql> update t_kenyon set id = 88888 where id = 345;第二个session更新的值是345,但是也一直被阻塞,直到session1被rollback或者commit,如果session1未做回滚或者提交,session2中的该阻塞在超出mysql的锁时间限制时自动回滚,该参数为innodb_lock_wait_timeout,默认值50秒 现象如下ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction加索引后的测试 3.session one update mysql> create index ind_kenyon on t_kenyon(id);Query OK, 0 rows affected (28.58 sec)Records: 0  Duplicates: 0  Warnings: 0   mysql> update t_kenyon set id = 999 where id = 1;Query OK, 1 row affected (0.03 sec)Rows matched: 1  Changed: 1  Warnings: 0 mysql> select * from t_kenyon;+------+| id   |+------+|   78 ||   78 ||  123 ||  345 ||  789 ||  999 |+------+6 rows in set (0.00 sec)4.session two update mysql> select * from t_kenyon;+------+| id   |+------+|    1 ||   78 ||   78 ||  123 ||  345 ||  789 |+------+6 rows in set (0.00 sec)   mysql> update t_kenyon set id = 7777 where id = 345;Query OK, 1 row affected (0.03 sec)Rows matched: 1  Changed: 1  Warnings: 0 mysql> select * from t_kenyon;+------+| id   |+------+|    1 ||   78 ||   78 ||  123 ||  789 || 7777 |+------+6 rows in set (0.00 sec)执行计划mysql> explain select * from t_kenyon where id = 345 /G*************************** 1. row ***************************           id: 1      select_type: SIMPLE        table: t_kenyon         type: refpossible_keys: ind_kenyon          key: ind_kenyon      key_len: 5          ref: const         rows: 1        Extra: Using where; Using index1 row in set (0.00 sec)可以看到加了索引后,不同的数据更新并没有被阻塞,实现了真正意义上行锁 三.行级锁的扩展限制 参考:http:///database/201208/145888.html  作者 kenyon bitsCN.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Python GIL (Global Interpreter Lock): Uncovering the principles and performance impact behind it Python GIL (Global Interpreter Lock): Uncovering the principles and performance impact behind it Feb 27, 2024 am 09:00 AM

pythonGIL (Global Interpreter Lock) is an important mechanism in Python. It limits that only one thread can execute Python bytecode at the same time. This is mainly to ensure the stability of the Python interpreter, because Python's memory management and garbage collection mechanisms are single-threaded. If multiple threads are allowed to execute Python bytecode at the same time, memory corruption or other unpredictable errors may result. The principle of GIL is relatively simple. It is a lock maintained by the Python interpreter, and when a thread executes Python bytecode, it acquires the GIL. If other threads want to execute Python bytecode, they must wait for the GIL to be released. When the GIL is released, other

How to use Oracle to query whether a table is locked? How to use Oracle to query whether a table is locked? Mar 06, 2024 am 11:54 AM

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

What is the difference between locked and unlocked iPhones? Detailed introduction: Comparison of the differences between locked and unlocked iPhones What is the difference between locked and unlocked iPhones? Detailed introduction: Comparison of the differences between locked and unlocked iPhones Mar 28, 2024 pm 03:10 PM

Apple mobile phones are the most widely chosen mobile phones recently, but we often see people discussing the difference between locked and unlocked Apple mobile phones online, and they are entangled in which one to buy. Today, Chen Siqi will share with you the differences between locked and unlocked iPhones and help you solve problems. In fact, there is not much difference between the two in appearance and function. The key lies in the price and use. What is a locked version and an unlocked version? An iPhone without locking restrictions means that it is not restricted by the operator, and the SIM card of any operator can be used normally. A locked version means that it has a network lock and can only use SIM cards provided by the designated operator and cannot use others. In fact, unlocked Apple phones can use mobile,

Performance comparison of distributed locks implemented by Redis Performance comparison of distributed locks implemented by Redis Jun 20, 2023 pm 05:46 PM

As Internet applications become larger and larger, distributed systems become more and more common. In these systems, distributed locks are an essential feature. Due to the strong demand for distributed locks, there are various implementation methods. Among them, Redis is a popular tool that is widely used in distributed lock implementation. In this article, we will explore the performance comparison of distributed locks implemented by Redis. 1. Basic Concepts of Redis Before discussing the distributed lock performance of Redis, we need to understand some basic concepts of Redis.

How is the lock in golang function implemented? How is the lock in golang function implemented? Jun 05, 2024 pm 12:39 PM

Locks in the Go language implement synchronized concurrent code to prevent data competition: Mutex: Mutex lock, which ensures that only one goroutine acquires the lock at the same time and is used for critical section control. RWMutex: Read-write lock, which allows multiple goroutines to read data at the same time, but only one goroutine can write data at the same time. It is suitable for scenarios that require frequent reading and writing of shared data.

Locks and synchronization in Python concurrent programming: keeping your code safe and reliable Locks and synchronization in Python concurrent programming: keeping your code safe and reliable Feb 19, 2024 pm 02:30 PM

Locks and Synchronization in Concurrent Programming In concurrent programming, multiple processes or threads run simultaneously, which can lead to resource contention and inconsistency issues. To solve these problems, locks and synchronization mechanisms are needed to coordinate access to shared resources. Concept of Lock A lock is a mechanism that allows only one thread or process to access a shared resource at a time. When one thread or process acquires a lock, other threads or processes are blocked from accessing the resource until the lock is released. Types of locks There are several types of locks in python: Mutex lock (Mutex): ensures that only one thread or process can access resources at a time. Condition variable: Allows a thread or process to wait for a certain condition and then acquire the lock. Read-write lock: allows multiple threads to read resources at the same time, but only allows one thread to write resources

In-depth analysis of the underlying implementation mechanism of Golang locks In-depth analysis of the underlying implementation mechanism of Golang locks Dec 28, 2023 am 11:26 AM

A detailed explanation of the underlying implementation principles of Golang locks requires specific code examples. Overview: Concurrent programming is a very important part of modern software development, and locks are a mechanism to achieve concurrency control. In Golang, the concept of locks is widely used in concurrent programming. This article will deeply explore the underlying implementation principles of Golang locks and provide specific code examples. The underlying implementation principle of mutex lock (Mutex) Mutex lock is one of the most commonly used lock types in Golang. It uses an underlying data structure sync.M

How to use locks to achieve thread safety in Go language How to use locks to achieve thread safety in Go language Mar 23, 2024 pm 07:00 PM

Using locks to achieve thread safety in Go language With the increasing popularity of concurrent programming, it has become particularly important to ensure safe access of data between multiple goroutines. In the Go language, locks can be used to achieve thread safety and ensure that access to shared resources in a concurrent environment will not cause data competition problems. This article will introduce in detail how to use locks to achieve thread safety in the Go language and provide specific code examples. What is a lock? A lock is a synchronization mechanism commonly used in concurrent programming that can coordinate synchronization between multiple goroutines.

See all articles