当前位置:网站首页>[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
2022-07-03 08:32:00 【keeper42】
Row-level locks & Table lock
What is a row level lock ? What is a table level lock ? Students who have learned the program must have a certain understanding of the concept of lock , As the name suggests, one is to lock a line , One is to lock a whole watch , Let's look at the following example :
Typical cases
Existing table T_A The structure and data are as follows


Start a transaction (session_1) And execute the following statement

Business one
stay session_1 Without submitting , Start a new business (session_2) And execute the statement

Business two
As a result, the transaction timed out , The problem is coming. , An error is a lock timeout, indicating that a table level lock is used , But it doesn't mean InnoDB Is it a row lock ? It should be noted that row level locks cannot take effect until indexes are used

We give fields column_2 Add index after , After performing the above operation again, it is found that the two transactions do not interfere with each other, and the execution is successful , In high concurrency scenarios, it can greatly improve the efficiency of the program .
Deadlock
Deadlock is mainly caused by different transactions competing for the same resource .

Specific cases
It is also the above T_A Update rows in the table , Deadlock caused when competing for resources


It can be seen that the deadlock will immediately report an error after it occurs . There is a mistake here , In our development, we often encounter too high concurrency, which leads to a large number of locks in the database that cannot be released in time, which is often described as deadlock by developers , It's not , The real deadlock will report an error at the moment of occurrence .
The query principle of index
In the last article, I made a brief introduction to the use of index , Some students may not be very clear about the difference between the two concepts of primary key and index , Let me briefly introduce how to find records through primary keys and indexes in the database :

Primary key storage structure

Index storage structure
The above figure shows the storage structure of primary key and index respectively , You can see that the corresponding record can be found directly through the primary key search , To search by index, you need to find the primary key of the corresponding record , Then find the corresponding record according to the primary key .
Two conclusions can be drawn from the above analysis :
1、 You have to set the primary key , Because the index ultimately needs to locate specific records through the primary key
2、 The primary key should preferably be numeric , In this way, the storage distribution of primary keys is more uniform and the search efficiency is higher
边栏推荐
- Three characteristics
- 了解小程序的笔记 2022/7/3
- Golang string segmentation, substitution and interception
- Graphics_ Games101/202 learning notes
- Go resolve ID card
- Base64 and base64url
- ArrayList
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- 梯度下降法求解BP神经网络的简单Demo
- String class
猜你喜欢

About Wireshark's unsuccessful installation of npcap

Data analysis exercises

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

Animation_ IK overview

UE4 source code reading_ Bone model and animation system_ Animation node

UE4 source code reading_ Mobile synchronization

Vscode, idea, VIM development tool shortcut keys

Introduction to hexadecimal coding

ArrayList

C course design employee information management system
随机推荐
Initial unity
【Rust笔记】06-包和模块
Osganimation library parsing
Mall management system of database application technology course design
Location of package cache downloaded by unity packagemanager
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
What is BFC?
【K&R】中文第二版 个人题解 Chapter1
796 · unlock
Osgearth north arrow display
Unity learning notes
数据分析练习题
MySQL 8
Golang string segmentation, substitution and interception
十六进制编码简介
【Rust 笔记】11-实用特型
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Collection interface
Development material set