当前位置:网站首页>[MySQL] basic knowledge review
[MySQL] basic knowledge review
2022-06-13 06:09:00 【LawrenceR77】
This is my review MySQL I took some notes when I was ( Actually, I copied it once ), Review the information step by step https://juejin.cn/user/2612095358879895 This blog .
1.MySQL Architecture
It can be roughly divided into four layers :
- adjoining course : Complete connection processing 、 Authentication 、 Safety and so on
- Core service layer : There are mainly query caches 、 analyzer 、 Optimizer 、 Actuators etc. , Contains all built-in functions , Such as time 、 date 、 Mathematics, etc
- Storage engine layer : The engine exists as a plug-in , This layer is the implementation part of the underlying data access operation , Server pass API Communicating with the storage engine
- Data storage layer : File systems store data , Storing data 、 Log etc. , Complete the interaction with the engine .
2.SQL Statement execution
- The client sends a SQL Query to server
- The server checks the query cache , If hit cache , The stored result is returned , Otherwise continue down
- Server segment run SQL analysis 、 Preprocessing , Then the optimizer generates the corresponding execution plan
- MySQL The execution plan generated by the optimizer does not use the storage engine API To execute the query , The engine queries the data store and returns the corresponding results
- The server returns the results to the client
3.SQL The connector
The connector is responsible for the connection between the client and the database , Establishing a connection 、 authentication 、 Maintaining and managing links . The permission of the link is determined at the time of establishment , Once the link is established , The permissions will not change until the connection is re established .
Links have a status all the time , Used to indicate MySQL What is being done now . Official MySQL States include :
- Sleep Idle state , The thread is waiting for the client to send a new request
- Query The thread is executing the query or sending the result to the client
- Locked stay MySQL Server layer , The thread is waiting for a table lock , Locks implemented at the storage engine level are not reflected in this state
- Analyzing and statistics The thread is collecting statistics for the storage engine , And generate an execution plan for the query
- Copying to tmp table[on disk] The thread is executing the query , And copy the results to a temporary table , In this state, sorting is generally performed 、 Grouping and other operations , If there is on disk Mark , It indicates that the temporary table is currently being put on the disk
- Sorting result The thread is sorting the result set
- Sending data Threads can transfer data between multiple states , Or generate a result set , Or returning data to the client
4. client / Server communication
MySQL The communication between client and server is half duplex , The advantage is that communication is simple and fast , But there is no flow control .
The client passes the query to the server through a packet , The server can pass the parameter max_allowed_packet To specify the maximum limit for receiving packets , If the packet is too large , The query will be rejected and the corresponding error will be thrown .
contrary , The data that the server responds to the client will consist of multiple packets , The client needs to continuously receive packets , To ensure that the query results are completely received , This process is to push data from the server to the client , The client cannot stop the server .
5. The query cache
Before parsing the query statement , If the cache is on , that MySQL Will check whether the query hit the data in the cache , If hit cache , The result in the cache will be returned directly . The previous query statements and corresponding results will exist in memory in the form of key value pairs . Query and cache key You must match exactly to hit the cache .
Because the hit conditions of the cache are quite strict , Cache invalidation is very frequent , And as long as the table has any updates , The query cache for this table will be cleared , Therefore, query caching often does more harm than good . The whole block function of query cache is MySQL 8 Deleted as a whole .
6. Query parser and preprocessor
The parser is responsible for disassembling SQL request , Generate a corresponding parse tree , Convert the request to MySQL Something recognizable . The parsing process includes lexical analysis and grammatical analysis , Lexical analysis will mainly SQL Split into multiple words according to the separator , Then the grammar analysis will verify the validity of words and combinations .
The preprocessor will check whether the parse tree is legal according to some rules , Such as the data table accessed 、 Does the column exist , Check whether the name and alias are ambiguous .
7. Query optimizer
A query can be executed in multiple ways with the same end result , The role of the optimizer is to find the best solution .MySQL The optimizer is cost based , It will choose the execution plan with the lowest cost , But not every optimization result is the best . May lead to MySQL Factors that choose a poor execution plan include :
- The statistics are not accurate
- Cost estimates do not equal actual execution costs
- Optimization is based on cost , Not necessarily the fastest
- Never consider other queries executed concurrently
- It may not be possible to estimate the full implementation plan
The types of optimizations that the optimizer can handle include :
- Redefine the order of the associated tables , That is, change the original association order in the query
- External connection is converted into internal connection
- Use some equivalence rules to normalize expressions
- Optimize by engine or feature COUNT、MIN、MAX Expressions such as
- Estimate and convert part of the expression into a constant expression
- Overwrite index scan , When the columns in the index contain all the columns in the query , Directly use the index to return the required data , There is no need to query the corresponding row
- In some cases, self queries are converted to a more efficient form , So as to reduce multiple queries and multiple access to data
- When it is found that the query requirements are met , Terminate the query immediately , If you use limit When clause
- If the values of two columns are related by an equation , be MySQL Can put one of the columns Where The condition is passed to another column
- Yes IN When the conditions , First the IN() Sort the data in the list , Then the binary search method is used to determine whether the values in the list meet the conditions
In execution SQL When the sentence is , Add before query explain extended, Add... After querying show warning You can see the reconstructed SQL sentence .
8. Actuators and storage engines
Parse the tree and optimize the execution plan according to the previous query , The execution engine will continuously call various interfaces of the storage engine , Extract the data . The storage engine is to execute SQL Of the statement , It follows certain steps to query memory cache data 、 Update disk 、 Query disk data, etc .
MySQL Common storage engines are :InnoDB、MyISAM、ISAM、Memory etc.
9. Create tables and information about tables
perform create table Build table , By default InnoDB engine , adopt engin=xxx Parameters can specify which engine to use . When creating a ,MySQL Will create a table with the same name .fmr File to store table definitions .
adopt show table status like <table_name> Command to view information about the table . The display information includes :
- Name: Table name
- Engine: Storage engine type
- Row_format: Line format ,Dynamic Is variable , It usually contains variable length fields , Such as VARCHAR or BLOB
- Rows: Number of rows in the table , about InnoDB, This value is an estimate , For other engines the exact value
- Avg_row_length: Average number of bytes per line
- Data_length: The size of the table data / byte
- Index_length: The size of the index / byte
- Auto_increment: The next self increment
- Collation: Table's default character set and collation
- other : Such as creation time 、 Version, etc
10.InnoDB
InnoDB The main features of the engine are :
- Support transactions
- Row level lock design
- Support foreign keys
- Support Unlocked read , The default read operation does not generate locks
- All four isolation levels are implemented , The default is Repeatable , And at the same time through Clearance lock Prevent unreal reading
- use Multi version concurrency control To support high concurrency
- Provide Insert cache 、 The two time to write 、 adaptive hash index 、 read-ahead High performance and high availability
InnoDB Put the data in a logical table space , A tablespace is a black box managed by an engine , It consists of a series of data files ,InnoDB You can store the data and index of each table in a separate file , For the data in the table ,InnoDB The method of aggregation is adopted , Therefore, each table is stored in the order of primary key , If the specified primary key is not displayed when the table is defined , For each row 6 Bytes of ROWID, And take this as the primary key .
11.MyISAM
The main features :
- Support full text search 、 Compress 、 Spatial functions, etc
- Transaction and row level locks are not supported
- Unable to reply safely after crash
- The buffer pool only buffers index files , Do not buffer data
MyISAM The table of is stored in two files , Data files and index files , Respectively by .MYD and .MYI Extension name
12.Memory
The main features :
- All data Save in memory , No disk required IO
- The table structure will be preserved after restart , But the data will be lost
- Support Hash Indexes , High reading efficiency ; Table lock , Low concurrent write performance
- I won't support it BLOB、TEXT Column of type , The length of each line is fixed
MySQL Use temporary tables to store intermediate results during query execution , The temporary watch used internally is Memory surface , If the middle table is too large or contains BLOB、TEXT Field , It will be transformed into MyISAM surface .
13.Archive
The main features :
- High compression ratio , The storage space is about InnoDB Of 10-15 One of the points
- Index not supported , Can't cache data
- Unsupported transaction
- Row-level locks
- Support insert、replace and select operation , But not supported update and delete
Its design goal is only to provide high-speed insertion and compression functions , Suitable for data archiving and storage .
边栏推荐
- Introduction to USB learning (5) -- looking back, the man was in the dim light
- Pod libwebp error reporting solution
- Leetcode- longest palindrome string - simple
- = = relation between int and integer
- USB status error and its cause (error code)
- 华为开发者认证与DevEco Studio编译器下载
- Download and installation of universal player potplayer, live stream m3u8 import
- Concurrent programming -- source code analysis of thread pool
- Self summarizing
- Waterfall flow layout of uni app Homepage
猜你喜欢
随机推荐
MySQL custom function
@The detailed explanation of configurationproperties and the problem that all properties of the entity bean modified by this annotation are null after injection are solved
[to]12 common IP commands in the iproute installation package
Leetcode- reverse string ii- simple
Introduction to USB learning (5) -- looking back, the man was in the dim light
自定义View —— 可伸展的CollapsExpendView
Not in the following list of legal domain names, wechat applet solution
Adding classes dynamically in uni app
AI实现亲人“复活”|老照片修复|老照片上色,免费APP推荐
Leetcode longest harmonic subsequence simple
Tongweb card, tongweb card, tongweb card
Uni app disable native navigation bar
Leetcode judge subsequence simple
【DP之01背包】
Data conversion analysis tool
Leetcode- key formatting - simple
Leetcode- reverse string - simple
Use of Nacos configuration center
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
Leetcode- distribute cookies - simple








