search
HomeDatabaseMysql TutorialIn-depth understanding of how the MySQL index optimizer works

This article brings you relevant knowledge about mysql, which mainly introduces the relevant content about the working principle of the index optimizer, including the composition of MySQL Server, the MySQL optimizer selects indexes Principle and SQL cost analysis, and finally summarize the entire query process through select query. Let’s take a look at it together. I hope it will be helpful to everyone.

In-depth understanding of how the MySQL index optimizer works

Recommended learning: mysql video tutorial

##1. How the MySQL optimizer selects indexes

Let’s take a look at this table. The SUB_ODR_ID field has created two related indexes. Based on what we learned before, we create a PRIMARY KEY (

ID) auto-incrementing primary key index, (LOG_ID, SUB_ODR_ID) are set as joint index and unique index, and two indexes are set for CREATE_TIME and UPDATE_TIME respectively.

CREATE TABLE `***`  (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `LOG_ID` varchar(32) NOT NULL COMMENT '交易流水号',
  `ODR_ID` varchar(32) NOT NULL COMMENT '父单号',
  `SUB_ODR_ID` varchar(32) NOT NULL COMMENT '子单号',
  `CREATE_TIME` datetime(0) NOT NULL COMMENT '创建时间',
  `CREATE_BY` varchar(32) NOT NULL COMMENT ' 创建人',
  `UPDATE_TIME` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间',
  `UPDATE_BY` varchar(32) NOT NULL COMMENT '更新人',
  PRIMARY KEY (`ID`) USING BTREE,
  UNIQUE INDEX `UNQ_LOG_SUBODR_ID`(`LOG_ID`, `SUB_ODR_ID`) USING BTREE,
  INDEX `IDX_ODR_ID`(`ODR_ID`) USING BTREE,
  INDEX `IDX_SUB_ID`(`SUB_ODR_ID`) USING BTREE,
  INDEX `IDX_CREATE_TIME`(`CREATE_TIME`) USING BTREE,
  INDEX `IDX_UPDATE_TIME`(`UPDATE_TIME`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 SET = utf8 COLLATE = utf8_general_ci COMMENT = '分摊业务明细表' ROW_FORMAT = Dynamic;
In the query field SUB_ODR_ID, three related indexes can theoretically be used: UNQ_LOG_SUBODR_ID, IDX_SUB_ID. How does the MySQL optimizer choose from these three indexes?

In relational databases, B-trees are just data structures used for storage.

How to use it depends on the optimizer of the database. The optimizer determines the selection of a specific index, known as the execution plan. Optimizer selection is based on cost, with the lower the cost, the higher the preference index.

1. MySQL database composition

MySQL database is composed of Server (server) layer and Engine (engine) layer.

The Serve layer has a SQL analyzer, SQL optimizer and SQL executor, which are responsible for the specific execution process of SQL statements.

The Engine layer is responsible for storing specific data, such as the most commonly used InnoDB storage engine, and the TempTable engine used to store temporary result sets in memory.

The SQL optimizer will analyze all possible execution plans and choose the lowest-cost execution. This optimizer is called CBO (cost-based optimizer).

#2. MySQL database cost calculation

In MySQL, the calculation cost of a SQL statement is easy to understand, that is, access Database (database pages, disk) Processes data.

CPU cost, which represents the calculation cost, such as comparison of index key values, comparison of record values ​​and sorting of result sets. These operations are completed at the server layer

IO cost, which represents the cost of engine-level IO. MySQL 8.0 can separately calculate the cost of reading memory IO and disk IO by distinguishing whether the table data is in memory.

Cost  = Server Cost + Engine Cost  = CPU Cost + IO Cost
MySQL optimizer believes that if a piece of SQL needs to create a disk-based temporary table, then the cost at this time is the largest, which is 20 times that of a memory-based temporary table. The cost of comparing index key values ​​and records is very low, but if there are many records to be compared, the cost can be very high.

The MySQL optimizer believes that the cost of reading from disk is 4 times the cost of memory (the cost is not static and will vary depending on the hardware).

2. MySQL query cost

Check the value of each cost and the working principle of the MySQL optimizer. We execute the following SQL statement and analyze the execution process. MySQL index The selection is based on SQL execution cost

EXPLAIN FORMAT=json 
select * from test.fork_business_detail f where f.sub_odr_id = ''
read_cost represents the cost of reading from the InnoDB storage engine;

eval_cost represents the CPU cost of the server layer;

prefix_cost represents the total cost of SQL ;

data_read_per_join represents the total number of bytes in the read record.

{
	"query_block": {
		"cost_info": {
			"query_cost": "1.20"
		},
		"table": {
			"access_type": "ref",
			"possible_keys": [
				"IDX_SUB_ID"
			],
			"key": "IDX_SUB_ID",
			"used_key_parts": [
				"SUB_ODR_ID"
			],
			"key_length": "98",
			"ref": [
				"const"
			],
			"cost_info": {
				"read_cost": "1.00",
				"eval_cost": "0.20",
				"prefix_cost": "1.20",
				"data_read_per_join": "1K"
			},
			"used_columns": [
				"ID",
				"LOG_ID",
				"ODR_ID",
				"SUB_ODR_ID",
				"CREATE_TIME",
				"CREATE_BY",
				"UPDATE_TIME",
				"UPDATE_BY"
			]
		}
	}
}

3. SELECT execution process

How to improve MySQL query performance? First, you need to understand the entire process of SQL processing by the query optimizer. The execution process of SELECT SQL is taken as an example, as shown in the following figure:

The client sends a SELECT query to the server; the server first checks the query cache. If the cache is hit, the results stored in the cache will be returned immediately. Otherwise, enter the next stage;

The server performs SQL parsing and preprocessing, and the query optimizer generates a corresponding execution plan; MySQL calls the API of the storage engine to execute the query based on the execution plan generated by the optimizer; the results will be returned to client and put into the query cache at the same time.

Recommended learning:

mysql video tutorial

The above is the detailed content of In-depth understanding of how the MySQL index optimizer works. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
how to find large tables in mysqlhow to find large tables in mysqlJul 23, 2025 am 02:20 AM

Finding large tables in MySQL can be achieved by querying system tables or checking physical files. 1. Use the information_schema.TABLES table to execute SQL statements to filter out tables with large data volume and large space, and sort them by size; 2. Enter the MySQL data directory through server access permissions, use the command to view the .ibd file size to locate the large table; 3. Analyze the real "large" table based on the index and fragmentation situation, use the DATA_FREE field to view the fragmented space, and execute OPTIMIZETABLE if necessary for optimization. The above methods help identify and deal with large table problems that affect performance from three aspects: statistical information, physical files and storage efficiency.

Managing Large Objects (BLOBs/TEXT) in MySQL EfficientlyManaging Large Objects (BLOBs/TEXT) in MySQL EfficientlyJul 23, 2025 am 02:11 AM

When dealing with large objects (BLOB/TEXT) in MySQL, you need to pay attention to performance and design. 1. Select BLOB or TEXT according to the data type. TEXT is suitable for text, BLOB is used for binary content, and pay attention to the influence of character sets. 2. Avoid using large object types in frequent query fields. It is recommended to split them into separate tables and associate them through foreign keys. 3. Use indexes reasonably, such as prefix indexes or FULLTEXT indexes, to avoid blindly adding normal indexes. 4. Prioritize the use of the InnoDB storage engine and optimize the configuration, such as turning on innodb_file_per_table and considering partitioning strategies to improve the processing efficiency of large objects.

Implementing MySQL Proxy for Load Balancing and FailoverImplementing MySQL Proxy for Load Balancing and FailoverJul 23, 2025 am 02:09 AM

MySQLProxy is a lightweight database middleware for load balancing and failover. Its core functions include: 1. Query analysis and rewrite; 2. Load balancing; 3. Failover. Configuring load balancing requires controlling traffic through Lua scripts, such as polling SELECT requests to multiple slave libraries. Failover requires the script to listen to the connection status, mark the failed node and temporarily skip it. Note when using: 1. The single-threaded model may affect high concurrency performance; 2. Lua script development requires certain capabilities; 3. Lack of built-in health checks; 4. Limited support for connection pools. Overall, it is suitable for scenarios with limited resources and simple needs.

Securing MySQL for Hybrid Cloud EnvironmentsSecuring MySQL for Hybrid Cloud EnvironmentsJul 23, 2025 am 01:55 AM

The security configuration of MySQL database in a hybrid cloud environment needs to focus on the following four aspects: 1. Network access control is the first line of defense, and the database exposure range should be restricted through firewall rules, binding designated network interfaces, and using VPC peer-to-peer connections; 2. Enable and correctly configure SSL encrypted connections to ensure data transmission security and prevent man-in-the-middle attacks; 3. User permissions and authentication strategies should be refined, follow the principle of minimum permissions, create a dedicated account and restrict source; 4. Regular audits and log monitoring, use the log analysis platform to promptly detect abnormal behaviors, improve overall security.

Building a Disaster Recovery Plan for MySQL DatabasesBuilding a Disaster Recovery Plan for MySQL DatabasesJul 23, 2025 am 01:49 AM

AsolidMySQLdisasterrecoveryplanrequiresunderstandingpriorities,choosingtherightbackupstrategy,settingupreplication,andpracticingrecovery.1.IdentifycriticaldatabasesanddefineRPO/RTOtodeterminebackupfrequency.2.Choosebetweenfullorincrementalbackupsusin

MySQL Database Monitoring with Percona ToolkitMySQL Database Monitoring with Percona ToolkitJul 23, 2025 am 01:46 AM

PerconaToolkit can realize MySQL monitoring through four core tools: 1. Use pt-query-digest to analyze slow query logs and locate time-consuming SQL; 2. Use pt-heartbeat to monitor master-slave replication delays and detect delay times; 3. Check configuration risks through pt-variable-advisor to obtain optimization suggestions; 4. Use pt-online-schema-change to observe performance impacts when structural changes. These tools are lightweight and efficient, suitable for quickly diagnosing and monitoring MySQL running status.

Designing MySQL Databases for Inventory ManagementDesigning MySQL Databases for Inventory ManagementJul 23, 2025 am 01:42 AM

When designing an inventory management database, you need to clarify the core table structure, handle inventory changes, optimize query and report, and consider scalability. 1. The core table includes products, warehouses, inventory, and in-store records. Each table has clear fields and ensures consistency through foreign key associations. 2. Inventory changes are processed through transaction updates. Inventory operations are first written to Transactions and then updated to Inventory. Transactions are used to avoid concurrency problems and negative value checks are performed. 3. Query optimization includes establishing composite indexes in the Transactions table, using views to simplify logic, and generating summary tables to accelerate reports. 4. Extended

Implementing MySQL Data Versioning and AuditingImplementing MySQL Data Versioning and AuditingJul 23, 2025 am 01:42 AM

TotrackchangesinaMySQLdatabase,usehistorytableswithtriggersorapplication-levellogging.1.Createashadowtableforeachtrackedtablewithextrafieldslikerevision\_id,revision\_type,revision\_timestamp,andrevision\_user.2.Usetriggerstoautomaticallylogchangesbe

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment