Home Database Mysql Tutorial Detailed 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 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)

What is SOL coin? How does SOL coin work? What is SOL coin? How does SOL coin work? Mar 16, 2024 am 10:37 AM

Solana Blockchain and SOL Token Solana is a blockchain platform focused on providing high performance, security and scalability for decentralized applications (dApps). As the native asset of the Solana blockchain, SOL tokens are mainly used to pay transaction fees, pledge and participate in governance decisions. Solana’s unique features are its fast transaction confirmation times and high throughput, making it a favored choice among developers and users. Through SOL tokens, users can participate in various activities of the Solana ecosystem and jointly promote the development and progress of the platform. How Solana works Solana uses an innovative consensus mechanism called Proof of History (PoH) that is capable of efficiently processing thousands of transactions.

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

What is VET coin? How does VET coin work? What is VET coin? How does VET coin work? Mar 16, 2024 am 11:40 AM

VET Coin: Blockchain-based IoT ecosystem VeChainThor (VET) is a platform based on blockchain technology that aims to enhance the Internet of Things (IoT) field by ensuring the credibility of data and enabling safe transfer of value. supply chain management and business processes. VET coin is the native token of the VeChainThor blockchain and has the following functions: Pay transaction fees: VET coins are used to pay transaction fees on the VeChainThor network, including data storage, smart contract execution and identity verification. Governance: VET token holders can participate in the governance of VeChainThor, including voting on platform upgrades and proposals. Incentives: VET coins are used to incentivize validators in the network to ensure the

What is SHIB coin? How does SHIB coin work? What is SHIB coin? How does SHIB coin work? Mar 17, 2024 am 08:49 AM

ShibaInu Coin: Dog-Inspired Cryptocurrency ShibaInu Coin (SHIB) is a decentralized cryptocurrency inspired by the iconic Shiba Inu emoji. The cryptocurrency was launched in August 2020 and aims to be an alternative to Dogecoin on the Ethereum network. Working Principle SHIB coin is a digital currency built on the Ethereum blockchain and complies with the ERC-20 token standard. It utilizes a decentralized consensus mechanism, Proof of Stake (PoS), which allows holders to stake their SHIB tokens to verify transactions and earn rewards for doing so. Key Features Huge supply: The initial supply of SHIB coins is 1,000 trillion coins, making it one of the largest cryptocurrencies in circulation. Low price: S

What is Polygon coin? How does Polygon coin work? What is Polygon coin? How does Polygon coin work? Mar 16, 2024 am 09:22 AM

Polygon: A multifunctional blockchain that builds the Ethereum ecosystem Polygon is a multifunctional blockchain platform built on Ethereum, formerly known as MaticNetwork. Its goal is to solve the scalability, high fees, and complexity issues in the Ethereum network. Polygon provides developers and users with a faster, cheaper, and simpler blockchain experience by providing scalability solutions. Here’s how Polygon works: Sidechain Network: Polygon creates a network of multiple sidechains. These sidechains run in parallel with the main Ethereum chain and can handle large volumes of transactions, thereby increasing overall network throughput. Plasma framework: Polygon utilizes the Plasma framework, which

What is Beam Coin? How does Beam Coin work? What is Beam Coin? How does Beam Coin work? Mar 15, 2024 pm 09:50 PM

Beam Coin: Privacy-Focused Cryptocurrency Beam Coin is a privacy-focused cryptocurrency designed to provide secure and anonymous transactions. It uses the MimbleWimble protocol, a blockchain technology that enhances user privacy by merging transactions and hiding the addresses of senders and receivers. The design concept of Beam Coin is to provide users with a digital currency option that ensures the confidentiality of transaction information. By adopting this protocol, users can conduct transactions with greater confidence without worrying about their personal privacy information being leaked. This privacy-preserving feature makes Beam Coin work. MimbleWimble protocol enhances privacy by: Transaction merging: It combines multiple transactions into

What is Algorand coin? How does Algorand coin work? What is Algorand coin? How does Algorand coin work? Mar 17, 2024 am 08:30 AM

Algorand: A blockchain platform based on pure Byzantine consensus protocol Algorand is a blockchain platform built on pure Byzantine consensus protocol and aims to provide efficient, secure and scalable blockchain solutions. The platform was founded in 2017 by MIT professor Silvio Micali. Working Principle The core of Algorand lies in its unique pure Byzantine consensus protocol, the Algorand consensus. This protocol allows nodes to achieve consensus in a trustless environment, even if there are malicious nodes in the network. Algorand consensus achieves this goal through a series of steps. Key generation: Each node generates a pair of public and private keys. Proposal phase: A randomly selected node proposes a new zone

What is AR coin? How does AR coin work? What is AR coin? How does AR coin work? Mar 15, 2024 pm 07:25 PM

AR Coin: Digital currency based on augmented reality technology AR Coin is a digital currency that uses augmented reality technology to provide users with the experience of interacting with digital content, allowing them to create immersive experiences in the real world. How it works AR Coin works based on the following key concepts: Augmented Reality (AR): AR technology overlays digital information on the real world, allowing users to interact with virtual objects. Blockchain: Blockchain is a distributed ledger technology used to record and verify transactions. It provides security and transparency to AR coins. Smart Contracts: Smart contracts are codes stored on the blockchain that are used to automate specific operations. They play a vital role in the creation and management of AR coins. The workflow of AR coins is as follows: Create AR body

See all articles