Home Database Mysql Tutorial 怎么去看懂mysql的执行计划

怎么去看懂mysql的执行计划

Jun 07, 2016 pm 04:14 PM
mysql how implement Check plan

mysql的查看执行计划的语句很简单,explain+你要执行的sql语句就OK了。 举一个例子 EXPLAIN SELECT * from employees where employees.gender='M' 返回的结果如下: 这些结果都代表什么? id是一组数字,表示查询中执行select子句或操作表的顺序。 如果id相

mysql的查看执行计划的语句很简单,explain+你要执行的sql语句就OK了。

举一个例子

EXPLAIN SELECT * from employees where employees.gender='M' 

返回的结果如下:

这些结果都代表什么?

id是一组数字,表示查询中执行select子句或操作表的顺序。

如果id相同,则执行顺序从上至下。

如果是子查询,id的序号会递增,id越大则优先级越高,越先会被执行。

id如果相同,则可以认为是一组,从上往下顺序执行,所有组中,id越高,优先级越高,越容易执行。

selecttype有simple,primary,subquery,derived(衍生),union,unionresult。

simple表示查询中不包含子查询或者union。

当查询中包含任何复杂的子部分,最外层的查询被标记成primary。

在select或where列表中包含了子查询,则子查询被标记成subquery。

在from的列表中包含的子查询被标记成derived。

若第二个select出现在union后,则被标记成union,若union在from子句的子查询中,外层的select被标记成derived。

从union表获取结果的select被标记成union result。

type叫访问类型,表示在表中找到所需行的方式,常见类型有all,index,range,ref,eq_ref,const,system,NULL 性能从做至右由差至好。

ALL,即full table scan,mysql将遍历全表来找到所需要的行。

index为full index scan,只遍历索引树。

range表示索引范围扫描 ,对索引的扫描开始于一点,返回匹配的值域的行,常见于between,的查询。

ref为非唯一性索引扫描,返回匹配某个单独值的所有行,常见于非唯一索引即唯一索引的非唯一前缀进行的查找。

eq_ref表示唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配,常见于主键或者唯一索引扫描。

const,system表示当对查询部分进行优化,并转化成一个常量时,使用这些类型访问。比如将主键置于where列表中,mysql就能把该查询置成一个常量。system是const的一个特例,当查询表中只有一行的情况下使用的是system。

NULL表示在执行语句中,不用查表或索引。

possiblekey表示能使用哪个索引在表中找到行,查询涉及到的字段上若存在索引,则该索引被列出,但不一定被查询使用。

key表示查询时使用的索引。若查询中使用了覆盖索引,则该索引仅出现在key中举个例子

employee中gender上有一个索引。使用如下语句

1 EXPLAIN SELECT gender from employees
则结果如下:1  SIMPLE  employees  index  IND_GEN  300695  Using index
1 EXPLAIN SELECT first_name from employees
则结果如下  1  SIMPLE  employees  ALL  300695 
keylen表示索引所使用的字节数,可以通过该列结算查询中使用的索引长度
ref表示上述表的链接匹配条件,即哪些列或常量可被用于查找索引列上的值。
rows表示根据mysql表统计信息及索引选用情况,估算找到所需记录要读取的行数。
extra表示不在其他列并且也很重要的额外信息。 using index表示在相应的select中使用了覆盖索引。 usingwhere表示存储引擎搜到记录后进行了后过滤(POST-FILTER),如果查询未能使用索引,usingwhere的作用只是提醒我们mysql要用where条件过滤z结果集。 using temporay表示用临时表来存储结果集,常见于排序和分组查询。 usingfilesort,mysql中无法用索引完成的排序成为文件排序。

关于覆盖索引的一些概念如下:

MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件 包含所有满足查询需要的数据的索引称为 覆盖索引(Covering Index) 如果要使用覆盖索引,一定要注意select列表中只取出需要的列,不可select *,因为如果将所有字段一起做索引会导致索引文件过大,查询性能下降
mysq的执行计划有一定局限性直接引用了 ?EXPLAIN不会告诉你关于触发器、存储过程的信息或用户自定义函数对查询的影响情况 ?EXPLAIN不考虑各种Cache ?EXPLAIN不能显示MySQL在执行查询时所作的优化工作 ?部分统计信息是估算的,并非精确值 ?EXPALIN只能解释SELECT操作,其他操作要重写为SELECT后查看执行计划

增加一个参考链接http://space.itpub.net/559237/viewspace-496311

总结一下,本文主要是说明了mysql执行计划中几个字段的意思,包括id,select_type,table,type,possable_keys,key,key_len,ref,rows 和Extra。

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

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.

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.

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 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

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.

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

See all articles