Table of Contents
Overall architecture
1. Connector
2. Query cache
If the query cache does not hit, then the SQL needs to be actually executed, and the SQL needs to be parsed before execution. This parsing is mainly divided into lexical analysis and syntax analysis. Two steps.
The optimizer takes the AST generated by the analyzer as input, optimizes the SQL, generates what the optimizer considers to be the optimal execution plan, and hands it to the executor for execution. The optimization process includes logical transformation of SQL and cost calculation.
5. Executor
Home Database Mysql Tutorial What are MySQL architectural components

What are MySQL architectural components

May 17, 2021 am 09:41 AM
mysql

This article will introduce you to the MySQL architecture components. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What are MySQL architectural components

Overall architecture

What are MySQL architectural components

1. Connector

The connector is mainly responsible for establishing the connection with the client. For connections, permission verification and management connections, you can use the command show processlist to view connection information. When a user connection is successfully created, the permission information has been read into the memory. If the user's permissions are modified later, it will not take effect if it is not refreshed.

 For a connection, if no instructions are received for a long time (it is in an idle state), the connector will disconnect the link after a certain period of time. This time is controlled by the parameter wait_timeout, which defaults to 8 hours.

The connections in the connector are divided into long connections and short connections:

  • Long connections: After the connection is successful, the client requests to use the same connection

  • Short connection: The connection will be disconnected after each request is executed, and the connection will be re-established if there is another request.

 Because in normal times, we generally want to avoid the overhead of frequently repeatedly creating connections. The long connection used means that a connection is maintained for a long time without being disconnected. However, it should be noted that a connection manages some memory occupied by it during use, and will be released along with the connection when the connection is disconnected. If the connection is not disconnected and continues to accumulate without processing for a long time, it may cause excessive memory usage and be forcibly killed by the system. There are generally two solutions:

  • Disconnect long connections regularly, and disconnect after a period of time or after executing a query that takes up a lot of memory, thereby freeing up memory when a query is needed. When the time comes, re-create the connection

  • Versions after 5.7 can use mysql_reset_connection to reinitialize the connection resources without reconnection and permission verification, and restore the connection to the state when it was created. At the same time, there will also be some other effects, such as releasing table locks, clearing temporary tables, resetting variables set in the session, etc.

2. Query cache

Note: Query cache was abolished after version 8.0

 After the connection is successfully created, the SQL statement can be executed. However, if the query cache is turned on, the query will be queried from the cache before the SQL is actually analyzed. If the cache hits, it will be returned directly. . The query cache is a Key-Value structure, where Key is the SQL statement and Value is the corresponding query result. If the cache misses, subsequent query operations will continue. After the query is completed, the results will be stored in the query cache.

 Why is the query cache deleted? Because query caching usually does more harm than good. If a table is updated, the query cache corresponding to the table will be cleared. For frequently updated tables, the query cache will be invalidated very frequently, basically ineffective, and there is also the overhead of updating the cache. For data tables that will basically remain unchanged, you can choose to use query caching, such as system configuration tables. The cache hit rate of such tables will be higher, and the advantages may outweigh the disadvantages. However, for this configuration, We can also use external caching.

The query cache can be configured through the parameter query_cache_type. This parameter has 3 optional values, which are:

    # 0: Turn off the query cache
  • 1: Turn on query cache
  • 2: Use query cache when there is SQL_CACHE keyword in SQL, such as select SQL_CACHE * from t where xxx;
  • 3. Analyzer

If the query cache does not hit, then the SQL needs to be actually executed, and the SQL needs to be parsed before execution. This parsing is mainly divided into lexical analysis and syntax analysis. Two steps.

    Lexical analysis: Extract keywords from SQL, such as select, from, table name, field name, etc.
  • Grammatical analysis: According to The results of lexical analysis and some grammar rules defined by MySQL check whether the SQL grammar is legal, and eventually an abstract syntax tree (AST) will be generated
  • 4. Optimizer

The optimizer takes the AST generated by the analyzer as input, optimizes the SQL, generates what the optimizer considers to be the optimal execution plan, and hands it to the executor for execution. The optimization process includes logical transformation of SQL and cost calculation.

Logical conversion is similar to Java's static compile-time optimization, which will "simplify" SQL to ensure consistent execution results before and after SQL conversion. For example, where 1=1 and a.id = 2 can be equivalent to where a.id = 2.

The main purpose of cost calculation is to choose the way to execute SQL, including whether to use indexes, which index to use, what order to use for multi-table connections, etc. The cost is divided into service layer cost and engine layer cost. The service layer cost is mainly related to CPU, and the engine layer cost is mainly related to disk I/O. MySQL 5.7 introduces two system tables mysql.server_cost and mysql.engine_cost to configure these two costs. What is configured in the table is the cost corresponding to various operations, such as temporary table creation, sorting, page reading, etc.

The optimizer will calculate the final cost of a query plan based on the generated query plan and the above two cost configurations, and select the one with the smallest cost among multiple query plans to the executor for execution. However, it should be noted that the smallest cost sometimes does not necessarily mean the shortest execution time.

5. Executor

The executor will execute SQL according to the query plan selected by the optimizer. Before execution, it will also verify whether the requesting user has the corresponding query permissions, and finally call the MySQL engine layer Provides an interface to execute SQL statements and return results. If query caching is enabled, the results will also be stored in the query cache.

Related recommendations: "mysql tutorial"

The above is the detailed content of What are MySQL architectural components. 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

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

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)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles