国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1.2 Introduction to the basic components of the Server layer
1) Connector
2) Query cache (removed after MySQL 8.0 version)
3) Analyzer
4) Optimizer
5) Executor
2 Statement Analysis
2.1 Query Statement
2.2 Update statement
Home Database Mysql Tutorial How are SQL statements executed in MySQL?

How are SQL statements executed in MySQL?

Apr 11, 2019 am 11:44 AM
java mysql

The content of this article is about how to execute SQL statements in MySQL? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article will analyze the execution process of the next sql statement in MySQL, including how the sql query flows within MySQL and how the update of the sql statement is completed.

Before analyzing, I will take you to look at the infrastructure of MySQL. Knowing which components MySQL is composed of and what the functions of these components are can help us understand and solve these problems.

A MySQL infrastructure analysis

1.1 MySQL basic architecture overview

The following figure is a brief architecture diagram of MySQL. From the figure below, you can easily Clearly see how the user's SQL statement is executed inside MySQL.

Let’s briefly introduce the basic functions of some components involved in the picture below to help everyone understand this picture. The functions of these components will be introduced in detail in Section 1.2.

Connector: Identity authentication is related to permissions (when logging in to MySQL).

Query cache: When executing a query statement, the cache will be queried first (removed after MySQL 8.0 version, because this function is not very practical).

Analyzer: If the cache is not hit, the SQL statement will go through the analyzer. To put it bluntly, the analyzer must first see what your SQL statement is doing, and then check your SQL statement. Is the syntax correct?

Optimizer: Execute according to the solution that MySQL considers to be the best.

Executor: Execute the statement and return data from the storage engine.

How are SQL statements executed in MySQL?

Simply put, MySQL is mainly divided into Server layer and storage engine layer:

Server layer: mainly includes connectors and queries Cache, analyzer, optimizer, executor, etc. All cross-storage engine functions are implemented in this layer, such as stored procedures, triggers, views, functions, etc. There is also a general log module binglog log module.

Storage engine: Mainly responsible for data storage and reading. It adopts a replaceable plug-in architecture and supports multiple storage engines such as InnoDB, MyISAM, and Memory. Among them, the InnoDB engine has its own The log module redolog module. The most commonly used storage engine now is InnoDB, which has been used as the default storage engine since MySQL version 5.5.5.

1.2 Introduction to the basic components of the Server layer

1) Connector

The connector is mainly related to functions related to identity authentication and permissions, just like a very high level Like the doorman.

Mainly responsible for user login database, user identity authentication, including verification of account password, permissions and other operations. If the user account password has passed, the connector will query all the permissions of the user in the permissions table, and then The permission logic judgment in this connection will rely on the permission data read at this time. In other words, as long as the connection is not disconnected, even if the administrator modifies the user's permissions, the user will not be affected.

2) Query cache (removed after MySQL 8.0 version)

The query cache is mainly used to cache the SELECT statement we execute and the result set of the statement.

After the connection is established, when executing the query statement, the cache will be queried first. MySQL will first verify whether the sql has been executed and cache it in the memory in the form of Key-Value. Key is the query estimate and Value is Result set. If the cache key is hit, it will be returned directly to the client. If there is no hit, subsequent operations will be performed. After completion, the result will be cached to facilitate the next call. Of course, when the cache query is actually executed, the user's permissions will still be verified to see whether there are query conditions for the table.

It is not recommended to use cache for MySQL queries, because query cache failures may be very frequent in actual business scenarios. If you update a table, all query caches on this table will be cleared. For data that is not updated frequently, it is still possible to use caching.

So, generally we do not recommend using query cache in most cases.

The cache function was deleted after MySQL version 8.0. Officials also believed that this function had few practical application scenarios, so they simply deleted it.

3) Analyzer

If MySQL does not hit the cache, it will enter the analyzer. The analyzer is mainly used to analyze what the SQL statement is for. The analyzer will also be divided into several categories. Step:

The first step, lexical analysis, a SQL statement consists of multiple strings, first of all, extract the keywords, such as select, propose the query table, propose the field name, propose Query conditions and so on. After completing these operations, you will enter the second step.

The second step, syntax analysis, is mainly to determine whether the sql you entered is correct and conforms to the syntax of MySQL.

After completing these 2 steps, MySQL is ready to start execution, but how to execute it and how to achieve the best result? At this time, the optimizer needs to come into play.

4) Optimizer

The function of the optimizer is to execute what it considers to be the optimal execution plan (sometimes it may not be optimal. This article involves an in-depth explanation of this part of knowledge), For example, how to choose an index when there are multiple indexes, how to choose the association order when querying multiple tables, etc.

It can be said that after going through the optimizer, it can be said that the specific execution of this statement has been determined.

5) Executor

After selecting the execution plan, MySQL is ready to start execution. First, it will check whether the user has permission before execution. If there is no permission, an error will be returned. Information, if it has permission, it will call the engine's interface and return the result of the interface execution.

2 Statement Analysis

2.1 Query Statement

Having said so much, how is a sql statement executed? In fact, our sql can be divided into two types, one is query and the other is update (add, update, delete). Let’s first analyze the query statement. The statement is as follows:

select?*?from?tb_student??A?where?A.age='18'?and?A.name='?張三?';

Combined with the above description, we analyze the execution process of this statement:

First check whether the statement has permissions. If there is no permission, directly An error message is returned. If you have permission, before MySQL8.0 version, the cache will be queried first, and this SQL statement will be used as the key to query whether there is a result in the memory. If there is a direct cache, if not, proceed to the next step.

Perform lexical analysis through the analyzer and extract the key elements of the sql statement. For example, the above statement is a query select. The table name to be queried is tb_student. All columns need to be queried. The query conditions are The id of this table is '1'. Then determine whether the SQL statement has syntax errors, such as whether the keywords are correct, etc. If there is no problem, proceed to the next step.

The next step is for the optimizer to determine the execution plan. The above SQL statement can have two execution plans:

??a.先查詢學(xué)生表中姓名為“張三”的學(xué)生,然后判斷是否年齡是?18。
??b.先找出學(xué)生中年齡?18?歲的學(xué)生,然后再查詢姓名為“張三”的學(xué)生。

Then the optimizer selects execution efficiency according to its own optimization algorithm The best solution (the optimizer thinks that sometimes it may not be the best). Then after confirming the execution plan, you are ready to start execution.

Perform permission verification. If there is no permission, an error message will be returned. If there is permission, the database engine interface will be called and the engine execution result will be returned.

2.2 Update statement

The above is the execution process of a query sql, then let's see how an update statement is executed? The sql statement is as follows:

update?tb_student?A?set?A.age='19'?where?A.name='?張三?';

Let’s modify Zhang San’s age. The age field will definitely not be set in the actual database, otherwise it will be beaten by the technical person in charge. In fact, each statement will basically follow the process of the previous query, but the log must be recorded when executing the update. This will introduce the log module. MySQL’s own log module binlog (archive log ), all storage engines can be used, our commonly used InnoDB engine also comes with a log module redo log (redo log), we will discuss the execution of this statement in InnoDB mode process. The process is as follows:

First query the data of Zhang San. If there is a cache, the cache will also be used.

Then get the query statement, change the age to 19, and then call the engine API interface to write this row of data. The InnoDB engine saves the data in the memory and records the redo log at the same time. At this time, the redo log enters the prepare state. Then tell the executor that the execution is completed and can be submitted at any time.

After receiving the notification, the executor records the binlog, then calls the engine interface and submits the redo log as the submission status.

update completed.

Some students here will definitely ask, why do we need to use two log modules instead of one log module?

This is because MySQL did not work with InnoDB at the beginning. Engine (InnoDB engine is inserted into MySQL in the form of a plug-in by other companies). MySQL's own engine is MyISAM, but we know that redo log is unique to the InnoDB engine and is not available in other storage engines. This results in the lack of crash-safe capabilities. (With the crash-safe capability, even if the database restarts abnormally, previously submitted records will not be lost). Binlog logs can only be used for archiving.

It’s not that you can’t use only one log module, it’s just that the InnoDB engine supports transactions through redo log. Then, some students will ask, can I use two log modules, but not so complicated? Why does redo log introduce prepare pre-commit state? Here we use proof by contradiction to explain why we do this?

First write the redo log and submit it directly, then write the binlog. Suppose that after writing the redo log, the machine hangs and the binlog log is not written. Then after the machine is restarted, the machine will The data is restored through redo log, but bingog does not record the data at this time. When the machine is backed up later, this piece of data will be lost, and the master-slave synchronization will also lose this piece of data.

Write the binlog first, and then write the redo log. Assume that after writing the binlog, the machine restarts abnormally. Since there is no redo log, the machine cannot restore this record, but the binlog has Record, then the same reason as above will cause data inconsistency.

If the redo log two-stage submission method is used, it will be different. After writing the binglog, and then submitting the redo log will prevent the above problems and ensure the consistency of the data. So the question is, is there an extreme situation? Assume that the redo log is in the pre-commit state and the binglog has been written. What will happen if an abnormal restart occurs at this time?
This depends on the processing mechanism of MySQL. The processing process of MySQL is as follows:

Judge whether the redo log is complete. If it is judged to be complete, submit it immediately.

If the redo log is only pre-submitted but not in commit status, it will be judged at this time whether the binlog is complete. If it is complete, the redo log will be submitted. If it is incomplete, the transaction will be rolled back.

This solves the problem of data consistency.

Three Summary

MySQL is mainly divided into Server and Engine layers. The Server layer mainly includes connectors, query caches, analyzers, optimizers, and executors. At the same time There is also a log module (binlog), which can be shared by all execution engines. Redolog is only available in InnoDB.

The engine layer is plug-in type and currently mainly includes MyISAM, InnoDB, Memory, etc.

The execution process of the query statement is as follows: Permission verification (if the cache is hit)---"Query cache---"Analyzer---"Optimizer---"Permission verification---"Executor- --》Engine

The update statement execution process is as follows: Analyzer----》Permission verification----》Executor---》Engine---redo log(prepare status---》 binlog---》redo log (commit status) [Related recommendations: MySQL Tutorial]

The above is the detailed content of How are SQL statements executed in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How does a HashMap work internally in Java? How does a HashMap work internally in Java? Jul 15, 2025 am 03:10 AM

HashMap implements key-value pair storage through hash tables in Java, and its core lies in quickly positioning data locations. 1. First use the hashCode() method of the key to generate a hash value and convert it into an array index through bit operations; 2. Different objects may generate the same hash value, resulting in conflicts. At this time, the node is mounted in the form of a linked list. After JDK8, the linked list is too long (default length 8) and it will be converted to a red and black tree to improve efficiency; 3. When using a custom class as a key, the equals() and hashCode() methods must be rewritten; 4. HashMap dynamically expands capacity. When the number of elements exceeds the capacity and multiplies by the load factor (default 0.75), expand and rehash; 5. HashMap is not thread-safe, and Concu should be used in multithreaded

PHP prepared statement get result PHP prepared statement get result Jul 14, 2025 am 02:12 AM

The method of using preprocessing statements to obtain database query results in PHP varies from extension. 1. When using mysqli, you can obtain the associative array through get_result() and fetch_assoc(), which is suitable for modern environments; 2. You can also use bind_result() to bind variables, which is suitable for situations where there are few fields and fixed structures, and it is good compatibility but there are many fields when there are many fields; 3. When using PDO, you can obtain the associative array through fetch (PDO::FETCH_ASSOC), or use fetchAll() to obtain all data at once, so the interface is unified and the error handling is clearer; in addition, you need to pay attention to parameter type matching, execution of execute(), timely release of resources and enable error reports.

mysql common table expression (cte) example mysql common table expression (cte) example Jul 14, 2025 am 02:28 AM

CTE is a temporary result set in MySQL used to simplify complex queries. It can be referenced multiple times in the current query, improving code readability and maintenance. For example, when looking for the latest orders for each user in the orders table, you can first obtain the latest order date for each user through the CTE, and then associate it with the original table to obtain the complete record. Compared with subqueries, the CTE structure is clearer and the logic is easier to debug. Usage tips include explicit alias, concatenating multiple CTEs, and processing tree data with recursive CTEs. Mastering CTE can make SQL more elegant and efficient.

Choosing appropriate data types for columns in MySQL tables Choosing appropriate data types for columns in MySQL tables Jul 15, 2025 am 02:25 AM

WhensettingupMySQLtables,choosingtherightdatatypesiscrucialforefficiencyandscalability.1)Understandthedataeachcolumnwillstore—numbers,text,dates,orflags—andchooseaccordingly.2)UseCHARforfixed-lengthdatalikecountrycodesandVARCHARforvariable-lengthdata

How to format a date in Java with SimpleDateFormat? How to format a date in Java with SimpleDateFormat? Jul 15, 2025 am 03:12 AM

Create and use SimpleDateFormat requires passing in format strings, such as newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); 2. Pay attention to case sensitivity and avoid misuse of mixed single-letter formats and YYYY and DD; 3. SimpleDateFormat is not thread-safe. In a multi-thread environment, you should create a new instance or use ThreadLocal every time; 4. When parsing a string using the parse method, you need to catch ParseException, and note that the result does not contain time zone information; 5. It is recommended to use DateTimeFormatter and Lo

Top Java interview questions Top Java interview questions Jul 14, 2025 am 01:59 AM

High-frequency questions in Java interviews are mainly focused on basic syntax, object-oriented, multithreaded, JVM and collection frameworks. The most common questions include: 1. There are 8 basic Java data types, such as byte, short, int, long, float, double, char and boolean. It is necessary to note that String is not the basic data type; 2. Final is used to modify classes, methods or variables to represent immutable, and finally is used to ensure code execution in exception processing. Finalize is an Object class method for cleaning before garbage collection; 3. Multi-thread synchronization can be achieved through synchronized keywords, ReentrantLock, and vo.

What is a BiConsumer in Java? What is a BiConsumer in Java? Jul 14, 2025 am 02:54 AM

BiConsumer is a functional interface in Java that handles operations that do not return results. It belongs to the java.util.function package and is suitable for scenarios where two data are required to operate at the same time, such as key-value pairs that traverse Map. A common usage is to iterate with Map's forEach method. Unlike other functional interfaces such as Consumer and BiFunction, BiConsumer does not generate a return value. The implementation methods include lambda expressions, method references and anonymous classes. When using them, you need to pay attention to the order of type parameters, non-returnable values, and exception handling.

See all articles