The role of EXPLAIN in Mysql
1. MYSQL index
Index (Index): A data structure that helps Mysql obtain data efficiently. Used to improve search efficiency, it can be compared to a dictionary. It can be simply understood as a sorted and fast search data structure.
The role of the index: to facilitate querying and sorting (so adding an index will affect the where statement and order by sorting statement).
In addition to the data, the database also maintains data structures that satisfy specific search algorithms, and these data structures reference the data in some way. This allows advanced search algorithms to be implemented on these data structures. These data structures are indexes.
The index itself is also very large and cannot be stored entirely in memory, so the index is often stored on disk in the form of an index file.
The index we usually refer to is generally a B-tree index unless otherwise specified. (Clustered index, compound index, prefix index, and unique index are all B+ tree indexes by default). In addition to B-tree indexes, there are also hash indexes.
Advantages: A. Improve data retrieval efficiency and reduce database IO costs
B. Sort data through index columns, reducing data sorting costs and CPU consumption.
Disadvantages: A. The index is also a table. This table saves the primary key and index fields and points to the records of the entity table, so the index also takes up space.
B. When performing INSERT, UPDATE, and DELETE operations on the table, MYSQL will not only update the data, but also save the corresponding information about the index column fields added to the index file each time it is updated.
In the actual production environment, we need to analyze step by step, optimize and establish the optimal index, and optimize our query conditions.
Classification of indexes: 1. Single-value index An index only contains one field, and a table can have multiple single-column indexes.
2. Unique index The value of the index column must be unique, but null values are allowed.
3. Composite index An index contains multiple columns
It is recommended to create no more than 5 indexes for a table
Syntax:
Create 1. CREATE [UNIQUE] INDEX indexName ON myTable (columnName(length));
2. ALTER myTable Add [UNIQUE] INDEX [indexName] ON (columnName(length));
Delete: DROP INDEX [indexName] ON myTable;
View: SHOW INDEX FROM table_name\G;
2. The role of EXPLAIN
##EXPLAIN: Simulate how the Mysql optimizer executes SQL query statements to know how Mysql processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.mysql> explain select * from tb_user;+----+-------------+---------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | tb_user | ALL | NULL | NULL | NULL | NULL | 1 | NULL | +----+-------------+---------+------+---------------+------+---------+------+------+-------+
(1)id column:
(1)、id 相同执行顺序由上到下 mysql> explain -> SELECT*FROM tb_order tb1 -> LEFT JOIN tb_product tb2 ON tb1.tb_product_id = tb2.id -> LEFT JOIN tb_user tb3 ON tb1.tb_user_id = tb3.id;+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+| 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 1 | NULL || 1 | SIMPLE | tb2 | eq_ref | PRIMARY | PRIMARY | 4 | product.tb1.tb_product_id | 1 | NULL || 1 | SIMPLE | tb3 | eq_ref | PRIMARY | PRIMARY | 4 | product.tb1.tb_user_id | 1 | NULL |+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+(2)、如果是子查询,id序号会自增,id值越大优先级就越高,越先被执行。 mysql> EXPLAIN -> select * from tb_product tb1 where tb1.id = (select tb_product_id from tb_order tb2 where id = tb2.id =1);+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+| 1 | PRIMARY | tb1 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL || 2 | SUBQUERY | tb2 | ALL | NULL | NULL | NULL | NULL | 1 | Using where |+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+(3)、id 相同与不同,同时存在 mysql> EXPLAIN -> select * from(select * from tb_order tb1 where tb1.id =1) s1,tb_user tb2 where s1.tb_user_id = tb2.id;+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL || 1 | PRIMARY | tb2 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL || 2 | DERIVED | tb1 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+derived2:衍生表 2表示衍生的是id=2的表 tb1
(2) select_type column: Operation type of data reading operation 1. SIMPLE: Simple select query, SQL does not contain subqueries or UNION.
2. PRIMARY: The query contains complex subquery parts, and the outermost query is marked as PRIMARY
3. SUBQUERY: The subquery is included in the select or WHERE list
4. DERIVED: In FROM The subqueries contained in the list will be marked as DERIVED (derived table), and MYSQL will recursively execute these subqueries and put the result set into the zero-time table.
5. UNION: If the second SELECT appears after UNION, it will be marked as UNION; if UNION is included in the subquery of the FROM clause, the outer SELECT will be marked as DERIVED
6. UNION RESULT: Select the result obtained from the UNION table
(3) table column: Which table does the row of data relate to
(4) type column: access type from best to worst system > const > eq_ref > ref > range > index > ALL
1、system:表只有一条记录(等于系统表),这是const类型的特例,平时业务中不会出现。
2、const:通过索引一次查到数据,该类型主要用于比较primary key 或者unique 索引,因为只匹配一行数据,所以很快;如果将主键置于WHERE语句后面,Mysql就能将该查询转换为一个常量。
3、eq_ref:唯一索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或者唯一索引扫描。
4、ref:非唯一索引扫描,返回匹配某个单独值得所有行,本质上是一种索引访问,它返回所有匹配某个单独值的行,就是说它可能会找到多条符合条件的数据,所以他是查找与扫描的混合体。
5、range:只检索给定范围的行,使用一个索引来选着行。key列显示使用了哪个索引。一般在你的WHERE 语句中出现between 、< 、> 、in 等查询,这种给定范围扫描比全表扫描要好。因为他只需要开始于索引的某一点,而结束于另一点,不用扫描全部索引。
6、index:FUll Index Scan 扫描遍历索引树(扫描全表的索引,从索引中获取数据)。
7、ALL 全表扫描 从磁盘中获取数据 百万级别的数据ALL类型的数据尽量优化。
(五)possible_keys列:显示可能应用在这张表的索引,一个或者多个。查询涉及到的字段若存在索引,则该索引将被列出,但不一定被查询实际使用。
(六)keys列:实际使用到的索引。如果为NULL,则没有使用索引。查询中如果使用了覆盖索引,则该索引仅出现在key列表中。覆盖索引:select 后的 字段与我们建立索引的字段个数一致。
(七)ken_len列:表示索引中使用的字节数,可通过该列计算查询中使用的索引长度。在不损失精确性的情况下,长度越短越好。key_len 显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出来的。
(八)ref列:显示索引的哪一列被使用了,如果可能的话,是一个常数。哪些列或常量被用于查找索引列上的值。
(九)rows列(每张表有多少行被优化器查询):根据表统计信息及索引选用的情况,大致估算找到所需记录需要读取的行数。
(十)Extra列:扩展属性,但是很重要的信息。
1、 Using filesort(文件排序):mysql无法按照表内既定的索引顺序进行读取。 mysql> explain select order_number from tb_order order by order_money; +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ | 1 | SIMPLE | tb_order | ALL | NULL | NULL | NULL | NULL | 1 | Using filesort | +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ row in set (0.00 sec) 说明:order_number是表内的一个唯一索引列,但是order by 没有使用该索引列排序,所以mysql使用不得不另起一列进行排序。 2、Using temporary:Mysql使用了临时表保存中间结果,常见于排序order by 和分组查询 group by。 mysql> explain select order_number from tb_order group by order_money; +----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+ | 1 | SIMPLE | tb_order | ALL | NULL | NULL | NULL | NULL | 1 | Using temporary; Using filesort | +----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+ row in set (0.00 sec) 3、Using index 表示相应的select 操作使用了覆盖索引,避免访问了表的数据行,效率不错。 如果同时出现Using where ,表明索引被用来执行索引键值的查找。 如果没有同时出现using where 表明索引用来读取数据而非执行查找动作。 mysql> explain select order_number from tb_order group by order_number; +----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+ | 1 | SIMPLE | tb_order | index | index_order_number | index_order_number | 99 | NULL | 1 | Using index | +----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+ row in set (0.00 sec) 4、Using where 查找 5、Using join buffer :表示当前sql使用了连接缓存。 6、impossible where :where 字句 总是false ,mysql 无法获取数据行。 7、select tables optimized away: 8、distinct:
The above is the detailed content of The role of EXPLAIN in Mysql. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

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.

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.
