search
HomeDatabaseMysql TutorialDetailed introduction to the working principle of mysql

Detailed introduction to the working principle of mysql

Aug 21, 2019 pm 02:09 PM
working principle

1. Components of Mysql

Mysql consists of SQL interface, parser, optimizer, cache, and storage engine.

2. Mysql working principle diagram

Detailed introduction to the working principle of mysql

##3. Description of each component of Mysql schematic diagram

3-1: connectors

Interact with sql statements in other programming languages, such as php, java, etc.

3-2:Management Serveices & Utilities

System management and control tools

3-3、Connection Pool

Manage buffer users Connection, thread processing and other requirements that require caching

3-4. SQL Interface (SQL interface)

Accepts the user's SQL command and returns the results that the user needs to query. For example, select from is to call SQL Interface

3-5, Parser (parser)

When the SQL command is passed to the parser, it will be verified and parsed by the parser.

Main functions of the parser:

a. Decompose the SQL statement into a data structure and pass this structure to subsequent steps. The subsequent transmission and processing of SQL statements are based on this structure

b. If an error is encountered during the decomposition, it means that the sql statement is unreasonable and the statement will not continue to be executed.

3-6. Optimizer (Query Optimizer)

The SQL statement will use the query optimizer to optimize the query before querying (generating multiple execution plans, and finally the database will choose the most optimized plan to execute and return the results as soon as possible) He uses "select-projection" -Join" strategy for querying.

You can understand it with an example: select uid,name from user where gender = 1;

This select query first selects based on the where statement, instead of querying all the tables first. Perform gender filtering

This select query first performs attribute projection based on uid and name, instead of removing all attributes and then filtering

Connect these two query conditions to generate the final query result.

3-7. Cache and Buffer (query cache)

If the query cache has a hit query result, the query statement can directly fetch data from the query cache.

This caching mechanism is composed of a series of small caches. For example, table cache, record cache, key cache, permission cache, etc.

3-8. Engine (storage engine)

The storage engine is the specific subsystem in MySql that deals with files, and it is also the MySQL One of the most unique places.

Mysql's storage engine is plug-in. It customizes a file access mechanism based on an abstract interface of the file access layer provided by MySql AB (this access mechanism is called a storage engine).

4. SQL statement execution process

The database is usually not used directly, but other programming languages ​​call mysql through SQL statements, which is processed by mysql and returned for execution. result. So how does Mysql process the SQL statement after it receives it?

First, the request of the program will interact with it through the connectors of mysql. After the request is received, it will be temporarily stored in the connection pool (connection pool) and processed by the processor (

Management Serveices & Utilities) manage. When the request enters the processing queue from the waiting queue, the manager will throw the request to the SQL interface (SQL Interface). After the SQL interface receives the request, it will hash the request and compare it with the results in the cache. If there is a complete match, the processing result will be returned directly through the cache; otherwise, a complete process will be required:

(1) The SQL interface is thrown to the subsequent interpreter (Parser). The interpreter will determine whether the SQL statement is correct or not, and if it is correct, it will convert it into a data structure.

(2) After the interpreter is processed, it comes to the optimizer (Optimizer) behind, which will generate multiple execution plans. In the end, the database will choose the most optimized plan for execution and return the results as soon as possible.

(3) After determining the optimal execution plan, the SQL statement can be handed over to the storage engine (Engine) for processing. The storage engine will obtain the corresponding data from the back-end storage device and return it to the original path. to the program.

5. Note

(1) How to cache query data

The storage engine processes the data and returns it to the program At the same time, it will also retain a copy of the data in the cache so that the next same request can be processed more quickly. The specific situation is that mysql will hash the query statement, execution results, etc., and keep them in the cache, waiting for the next query.

(2) The difference between buffer and cache

As you can see from the mysql schematic diagram, there are actually two buffers and caches in the cache. The difference between them:

To put it simply, buffer is a write cache and cache is a read cache.

(3) How to judge whether the required data has been cached in the cache

There may be a misunderstanding here. When processing SQL statements, in order to judge whether the query results have been cached, the entire process will be Go through it again, obtain the execution result, and then compare it with the required one to see if there is a hit. In this way, since no matter whether the query content is cached in the cache, you have to go through the entire process, what is the advantage of caching?

In fact, this is not the case. After the first query, mysql will hash the query statement and query results and keep them in the cache. After the SQL query arrives, it will be hashed in the same way. The two hash values ​​are compared. If they are the same, it is a hit and the query result is returned from the cache; otherwise, the entire process needs to be repeated.

I hope this article can provide some help to students who are learning databases. If there is something wrong in the article, please point it out. Thank you very much!

For more Mysql related questions, please visit the PHP Chinese website: mysql video tutorial

The above is the detailed content of Detailed introduction to the working principle of mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

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 Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.