当前位置:网站首页>[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
边栏推荐
- Abstract classes and interfaces
- 796 · 开锁
- 【Rust笔记】05-错误处理
- Golang 字符串分割,替换和截取
- Osgearth starry background
- GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
- Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
- Base64和Base64URL
- [linear table] basic operation of bidirectional linked list specify node exchange
- Vscode, idea, VIM development tool shortcut keys
猜你喜欢

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程

Some understandings of 3dfiles

C course design employee information management system

Constraintlayout's constraintset dynamically modifies constraints

Base64编码简介

Introduction to Base64 coding

Kunlunbase meetup is waiting for you!

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation

KunlunBase MeetUP 等您来!

单调栈-42. 接雨水
随机推荐
UE4 source code reading_ Mobile synchronization
Location of package cache downloaded by unity packagemanager
How does unity fixedupdate call at a fixed frame rate
Student educational administration management system of C # curriculum design
Unity Editor Extension - drag and drop
Get to know unity2 for the first time
Minimap plug-in
OpenGL learning notes
Unity editor expansion - scrolling list
[cloud native] introduction and use of feign of microservices
UE4 source code reading_ Bone model and animation system_ Animation process
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Unity editor expansion - the design idea of imgui
Dealing with duplicate data in Excel with xlwings
Markdown learning
jupyter远程服务器配置以及服务器开机自启
[updating] wechat applet learning notes_ three
[audio and video] ijkplayer error code
Intersectionpicker in osgearth
梯度下降法求解BP神经网络的简单Demo