Home Database Mysql Tutorial MySQL之explain的type列_MySQL

MySQL之explain的type列_MySQL

Jun 01, 2016 pm 01:18 PM
explain mysql

MySQLexplain

explain 可以分析 select 语句的执行,即 MySQL 的"执行计划。

  type 列

  MySQL 在表里找到所需行的方式。包括(由左至右,由最差到最好): | All | index | range | ref | eq_ref | const,system | null |

  ALL 全表扫描,MySQL 从头到尾扫描整张表查找行。 mysql> explain select * from a\G …

  type: ALL

  如果加上 limit 如 select * from a limit 100 MySQL 会扫描 100 行,但扫描方式不会变,还是从头到尾扫描。

  index 按索引次序扫描表,就是先读索引,再读实际的行,其实还是全表扫描。主要优点是避免了排序,因为索引是排好序的。(按照索引的排序去读对应的数据行。) create table a(a_id int not null, key(a_id)); insert into a value(1),(2); mysql> explain select * from a\G

  …

  type: index

  range 以范围的形式扫描索引建表: create table a(a_id int not null, key(a_id)); insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); mysql> explain select * from a where a_id > 1\G

  …

  type: range …

  IN 比较符也会用 range 表示: mysql> explain select * from a where a_id in (1,3,4)\G

  …

  type: range

  …

  ` ref 非唯一性索引访问建表: create table a(a_id int not null, key(a_id)); insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); mysql> explain select * from a where a_id=1\G

  …

  type: ref …

  eq_ref 使用有唯一性索引查找(主键或唯一性索引)建表及插入数据: create table a(id int primary key); create table a_info(id int primary key, title char(1)); insert into a value(1),(2); insert into a_info value(1, 'a'),(2, 'b'); mysql> explain select * from a join a_info using(id);

  …+--------+--------+…

  …| table | type |…

  …+--------+--------+…

  …| a | index |…

  …| a_info | eq_ref |…

  …+--------+--------+… 此时 a_info 每条记录与 a 一一对应,通过主键 id 关联起来,所以 a_info 的 type 为 eq_ref.删除 a_info 的主键:ALTER TABLE `a_info` DROP PRIMARY KEY; 现在 a_info 已经没有索引了: mysql> explain select * from a join a_info using(id);

  +----+…+--------+--------+… | id |…| table | type |… +----+…+--------+--------+… | 1 |…| a_info | ALL |… | 1 |…| a | eq_ref |… +----+…+--------+--------+… 这次 MySQL 调整了执行顺序,先全表扫描 a_info 表,再对表 a 进行 eq_ref 查找,因为 a 表 id 还是主键。删除 a 的主键:alter table a drop primary key; 现在 a 也没有索引了: mysql> explain select * from a join a_info using(id);

  …+--------+------+…

  …| table | type |…

  …+--------+------+…

  …| a | ALL |…

  …| a_info | ALL |…

  …+--------+------+… 现在两个表都使用全表扫描了。

建表及插入数据: create table a(id int primary key); create table a_info(id int, title char(1), key(id)); insert into a value(1),(2); insert into a_info value(1, 'a'),(2, 'b'); 现在 a_info 表 id 列变为普通索引(非唯一性索引): mysql> explain select * from a join a_info using(id) where a.id=1;

  …+--------+-------+…

  …| table | type |…

  …+--------+-------+…

  …| a | const |…

  …| a_info | ref |…

  …+--------+-------+… a_info 表 type 变为 ref 类型了。所以,唯一性索引才会出现 eq_ref (非唯一性索引会出现 ref ),因为唯一,所以最多只返回一条记录,找到后无需继续查找,因此比 ref 更快。

  const 被称为"常量",这个词不好理解,不过出现 const 的话就表示发生下面两种情况:在整个查询过程中这个表最多只会有一条匹配的行,比如主键 id=1 就肯定只有一行,只需读取一次表数据便能取得所需的结果,且表数据在分解执行计划时读取。返回值直接放在 select 语句中,类似 select 1 AS f .可以通过 extended 选择查看内部过程:

  建表及插入数据: create table a(id int primary key, c1 char(20) not null, c2 text not null, c3 text not null); insert into a values(1, 'asdfasdf', 'asdfasdf', 'asdfasdf'), (2, 'asdfasdf', 'asdfasdf', 'asdfasdf'); mysql> explain extended select * from a where id=1\G

  …

  type: const

  possible_keys: PRIMARY

  key: PRIMARY

  … 用 show warnings 查看 MySQL 是如何优化的: mysql> show warnings\G

  …

  Message: select '1' AS `id`,'asdfasdf' AS `c1`,'asdfasdf' AS `c2`,'asdfasdf' AS

  `c3` from `test`.`a` where 1 查询返回的结果为: mysql> select * from a where id=1;

  +----+----------+----------+----------+

  | id | c1 | c2 | c3 |

  +----+----------+----------+----------+

  | 1 | asdfasdf | asdfasdf | asdfasdf |

  +----+----------+----------+----------+ 可以看出,返回结果中的字段值都以"值 AS 字段名"的形式直接出现在优化后的 select 语句中。修改一下查询: mysql> explain select * from a where id in(1,2)\G

  …

  type: range … 当返回结果超过 1 条时, type 便不再为 const 了。重新建表及插入数据: create table a (id int not null); insert into a value(1),(2),(3); mysql> explain select * from a where id=1\G

  …

  type: ALL 目前表中只有一条 id=1 的记录,但 type 已为 ALL ,因为只有唯一性索引才能保证表中最多只有一条记录,只有这样 type 才有可能为 const .为 id 加普通索引后, type 变为 ref ,改为加唯一或主键索引后, type 便变为 const 了。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles