当前位置:网站首页>Execution process of MySQL query request - underlying principle
Execution process of MySQL query request - underlying principle
2022-07-06 18:33:00 【@Positive_ function】
Come and explore MySQL Well !

One 、 Connection management
Client processes can adopt TCP/IP、 Named pipes or shared memory 、UNIX Domain socket And other communication methods to establish a connection with the server process . Whenever a client process connects to a server process , The server process will create a thread to deal with the interaction with the client ; When the client exits, it will disconnect from the server , The server does not immediately destroy the threads that interact with the client , Instead, cache it , When another new client connects , The cached thread will be allocated to the new client . This eliminates the need to create and destroy threads frequently , So it saves money . From this we can also see ,MySQL The server will assign a thread to each connected client , However, too many threads will seriously affect the system performance , So we also need to limit the number of clients that can connect to the server at the same time . When the client program initiates a connection , Need to carry host information 、 user name 、 Password and other information , The server program authenticates the information provided by the client program . If authentication fails , The server program will reject the connection . in addition , If the client program and the server program do not run on the same computer , We can also adopt Transport layer security (TransportLayer Security,TLS) The protocol encrypts the connection , So as to ensure the security of data transmission .
When the connection is established , The server thread associated with the client will always wait for the request sent by the client .MySQL The request received by the server is just a text message , The text message has to go through various processes . What if you want to know what happened , Please read on .
Two 、 Analysis and optimization
Up to now ,MySQL The server has obtained the request in text form , Then I have to go through “ Ninety-eight is difficult ” To deal with , Some of the more important parts are The query cache 、 Syntax parsing and Query optimization . Let's take a closer look at .
2.1 The query cache
If I ask you 9 + 8 * 16- 3 * 2 * 17 What's the value of , You may use a calculator to calculate , Or more powerful, direct mental arithmetic , Finally, we got the result 357. If I ask you again 9 + 8 * 16 - 3 * 2 * 17 What's the value of , Will you still calculate foolishly ? We have just calculated , Just say the answer directly .
MySQL The same is true for the server program to process query requests , It will cache the query requests and results that have just been processed . If you have the same request next time , Just look up the results directly from the cache , There is no need to look up in the underlying table . This query cache can be shared between different clients . in other words . If the client A Just sent a query request , And the client B Then the same query request is sent , So the client B You can directly use the data in the query cache for this query .
Of course ,MySOL The server is not as smart as anyone , If there is any character difference between the two query requests ( for example , Space 、 notes 、 Case write ), Will cause the cache to miss . in addition , If the query request contains some system functions 、 User defined variables and functions 、 The system tables , Such as myql、information_schema、performance_schema Tables in the database , Then the request will not be cached . Take some system functions as an example , Two calls to the same function may produce different results . Like functions NOW, Each call will generate the latest current time . If this function is called in two query requests , Even if the text information of the query request is the same , Then two queries at different times should also get different results . If the results are cached at the first query , Using the result of the first query directly in the second query is wrong !
But since it's caching , Then there is a time when the cache fails .MySQL Our caching system monitors every table involved , As long as the structure or data of the table is modified , For example, we used INSERT、UPDATE、DELETE、TRUNCATE TABLE、ALTER TABLE、DROP TABLE or DROP DATABASE sentence , Then all query caches related to the table will become invalid and be deleted from the query cache !
Although query caching can sometimes improve system performance , But it also has to incur some overhead due to maintaining this cache . For example, we need to search in the query cache every time , After the query request is processed, the query cache needs to be updated , The memory area corresponding to the query cache needs to be maintained 、 from MySQL 5.7.20 Start , Query caching is not recommended , stay MySQL 8.0 Delete it directly in .
2.2 Syntax parsing
If the query cache misses , Next, you need to enter the formal query stage . Because the request sent by the client program is just a piece of text , therefore MySQL The server program first analyzes this text , Determine whether the syntax of the request is correct , Then the table to be queried from the text 、 All kinds of query conditions are extracted and put into MySQL Some data structures used inside the server .
essentially , Extracting the required information from the specified text is a compilation process , Involving lexical analysis 、 Syntax analysis 、 Semantic analysis and other stages . These issues do not fall within the scope of our discussion , You just need to understand that this step is required in the process of processing requests .
2.3 Query optimization
After parsing , The server program gets the information it needs , For example, what are the tables and columns to query 、 What are the search criteria . But that's not enough , Because we write MySQL Statement execution efficiency may not be very high ,MySQL Our optimization program will optimize our statements , If external connection is converted to internal connection 、 Expression simplification 、 Sub query into a bunch of things such as connections . The result of optimization is to generate an execution plan , This execution plan indicates which indexes should be used to execute the query , And what is the connection order between tables . We can use EXPLAIN Statement to view the execution plan of a statement .
3、 ... and 、 Storage engine
Until the server program completes the query optimization , You haven't really accessed the data in the real table .MySQL The server encapsulates the data storage and extraction operations into a module called storage engine . We know , A table consists of row by row records , But it's just a logical concept . How to physically represent records , How to read data from the table and how to write data to specific physical memory , It's all the responsibility of the storage engine . In order to achieve different functions ,MySQL Provides a wide range of storage engines , Tables managed by different storage engines may have different storage structures , The access algorithm may also be different .
Why is it called an engine ? Maybe this name is more popular , In fact, this was called table processor before storage engine , Later, people may think it's too rustic , It becomes a storage engine . Its function is to receive instructions from the upper layer , Then read or write the data in the table .
MySQL Supported storage engines :
Storage engine support for some functions :
From 《MySQL How it works —— Understand from the root MySQL》.
I hope my sharing above will help you , thank you !
边栏推荐
- Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
- Unity资源顺序加载的一个方法
- 微信为什么使用 SQLite 保存聊天记录?
- CSRF vulnerability analysis
- 递归的方式
- SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
- [swoole series 2.1] run the swoole first
- Windows连接Linux上安装的Redis
- Markdown grammar - better blogging
- Bonecp uses data sources
猜你喜欢

Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery

Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera

Use cpolar to build a business website (1)

Maixll dock camera usage

Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)

Recommend easy-to-use backstage management scaffolding, everyone open source

win10系统下插入U盘有声音提示却不显示盘符

Penetration test information collection - CDN bypass

十、进程管理

Why does wechat use SQLite to save chat records?
随机推荐
On time and parameter selection of asemi rectifier bridge db207
Recommend easy-to-use backstage management scaffolding, everyone open source
Grafana 9.0 is officially released! It's the strongest!
std::true_ Type and std:: false_ type
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
Brief description of SQL optimization problems
A method of sequentially loading Unity Resources
递归的方式
当保存参数使用结构体时必备的开发技巧方式
【Swoole系列2.1】先把Swoole跑起来
Excel usage record
TOP命令详解
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
Interview shock 62: what are the precautions for group by?
UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"
Excellent open source fonts for programmers
Jerry's updated equipment resource document [chapter]
D binding function
Cocos2d Lua 越来越小样本 内存游戏
【LeetCode第 300 场周赛】