Table of Contents
一、EXPLAIN 语句中type列的值
二、连接类型部分示例" >二、连接类型部分示例
Home Database Mysql Tutorial mysql explain type连接类型示例_MySQL

mysql explain type连接类型示例_MySQL

May 30, 2016 pm 05:10 PM
Example type

对于MySQL执行计划的获取,我们可以通过explain方式来查看,explain方式看似简单,实际上包含的内容很多,尤其是输出结果中的type类型列。理解这些不同的类型,对于我们SQL优化举足轻重,本文仅描述explian输出结果中的type列,同时给出其演示。

一、EXPLAIN 语句中type列的值

<code class="hljs oxygene">type:
    连接类型
    system          表只有一行
    const           表最多只有一行匹配,通用用于主键或者唯一索引比较时
    eq_ref          每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种,
                    特点是使用=,而且索引的所有部分都参与join且索引是主键或非空唯一键的索引
    ref             如果每次只匹配少数行,那就是比较好的一种,使用=或<=>,可以是左覆盖索引或非主键或非唯一键
    fulltext        全文搜索
    ref_or_null     与ref类似,但包括NULL
    index_merge     表示出现了索引合并优化(包括交集,并集以及交集之间的并集),但不包括跨表和全文索引。
                    这个比较复杂,目前的理解是合并单表的范围索引扫描(如果成本估算比普通的range要更优的话)
    unique_subquery 在in子查询中,就是value in (select...)把形如&ldquo;select unique_key_column&rdquo;的子查询替换。
                    PS:所以不一定in子句中使用子查询就是低效的!
    index_subquery  同上,但把形如&rdquo;select non_unique_key_column&ldquo;的子查询替换
    range           常数值的范围
    index           a.当查询是索引覆盖的,即所有数据均可从索引树获取的时候(Extra中有Using Index);
                    b.以索引顺序从索引中查找数据行的全表扫描(无 Using Index);
                    c.如果Extra中Using Index与Using Where同时出现的话,则是利用索引查找键值的意思;
                    d.如单独出现,则是用读索引来代替读行,但不用于查找
    all             全表扫描</code>
Copy after login

二、连接类型部分示例

<code class="hljs oxygene"><code class="hljs asciidoc">1、all
-- 环境描述
(root@localhost) [sakila]> show variables like &#39;version&#39;;
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| version       | 5.6.26 |
+---------------+--------+

MySQL采取全表遍历的方式来返回数据行,等同于Oracle的full table scan
(root@localhost) [sakila]> explain select count(description) from film;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | film  | ALL  | NULL          | NULL | NULL    | NULL | 1000 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+

2、index
MySQL采取索引全扫描的方式来返回数据行,等同于Oracle的full index scan
(root@localhost) [sakila]> explain select title from film \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: film
         type: index
possible_keys: NULL
          key: idx_title
      key_len: 767
          ref: NULL
         rows: 1000
        Extra: Using index
1 row in set (0.00 sec)

3、  range
索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行,常见于between、<、>等的查询
等同于Oracle的index range scan
(root@localhost) [sakila]> explain select * from payment where customer_id>300 and customer_id<400\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: payment
         type: range
possible_keys: idx_fk_customer_id
          key: idx_fk_customer_id
      key_len: 2
          ref: NULL
         rows: 2637
        Extra: Using where
1 row in set (0.00 sec)

(root@localhost) [sakila]> explain select * from payment where customer_id in (200,300,400)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: payment
         type: range
possible_keys: idx_fk_customer_id
          key: idx_fk_customer_id
      key_len: 2
          ref: NULL
         rows: 86
        Extra: Using index condition
1 row in set (0.00 sec)

4、ref
非唯一性索引扫描或者,返回匹配某个单独值的所有行。常见于使用非唯一索引即唯一索引的非唯一前缀进行的查找
(root@localhost) [sakila]> explain select * from payment where customer_id=305\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: payment
         type: ref
possible_keys: idx_fk_customer_id
          key: idx_fk_customer_id
      key_len: 2
          ref: const
         rows: 25
        Extra: 
1 row in set (0.00 sec)

idx_fk_customer_id为表payment上的外键索引,且存在多个不不唯一的值,如下查询
(root@localhost) [sakila]> select customer_id,count(*) from payment group by customer_id
    -> limit 2;
+-------------+----------+
| customer_id | count(*) |
+-------------+----------+
|           1 |       32 |
|           2 |       27 |
+-------------+----------+

-- 下面是非唯一前缀索引使用ref的示例
(root@localhost) [sakila]> create index idx_fisrt_last_name on customer(first_name,last_name);
Query OK, 599 rows affected (0.09 sec)
Records: 599  Duplicates: 0  Warnings: 0

(root@localhost) [sakila]> select first_name,count(*) from customer group by first_name 
    -> having count(*)>1 limit 2;
+------------+----------+
| first_name | count(*) |
+------------+----------+
| JAMIE      |        2 |
| JESSIE     |        2 |
+------------+----------+
2 rows in set (0.00 sec)

(root@localhost) [sakila]> explain select first_name from customer where first_name=&#39;JESSIE&#39;\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: customer
         type: ref
possible_keys: idx_fisrt_last_name
          key: idx_fisrt_last_name
      key_len: 137
          ref: const
         rows: 2
        Extra: Using where; Using index
1 row in set (0.00 sec)

(root@localhost) [sakila]> alter table customer drop index idx_fisrt_last_name;
Query OK, 599 rows affected (0.03 sec)
Records: 599  Duplicates: 0  Warnings: 0

--下面演示出现在join是ref的示例
(root@localhost) [sakila]> explain select b.*,a.* from payment a inner join
    -> customer b on a.customer_id=b.customer_id\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: b
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 599
        Extra: NULL
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: a
         type: ref
possible_keys: idx_fk_customer_id
          key: idx_fk_customer_id
      key_len: 2
          ref: sakila.b.customer_id
         rows: 13
        Extra: NULL
2 rows in set (0.01 sec)

5、eq_ref
类似于ref,其差别在于使用的索引为唯一索引,对于每个索引键值,表中只有一条记录与之匹配。
多见于主键扫描或者索引唯一扫描。
(root@localhost) [sakila]> explain select * from film a join film_text b 
    -> on a.film_id=b.film_id;
+----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref              | rows | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+
|  1 | SIMPLE      | b     | ALL    | PRIMARY       | NULL    | NULL    | NULL             | 1000 | NULL        |
|  1 | SIMPLE      | a     | eq_ref | PRIMARY       | PRIMARY | 2       | sakila.b.film_id |    1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+


(root@localhost) [sakila]> explain select title from film where film_id=5;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | film  | const | PRIMARY       | PRIMARY | 2       | const |    1 | NULL  |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

6、const、system:
当MySQL对查询某部分进行优化,这个匹配的行的其他列值可以转换为一个常量来处理。
如将主键或者唯一索引置于where列表中,MySQL就能将该查询转换为一个常量
(root@localhost) [sakila]> create table t1(id int,ename varchar(20) unique);
Query OK, 0 rows affected (0.05 sec)

(root@localhost) [sakila]> insert into t1 values(1,&#39;robin&#39;),(2,&#39;jack&#39;),(3,&#39;henry&#39;);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(root@localhost) [sakila]> explain select * from (select * from t1 where ename=&#39;robin&#39;)x;
+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+
| id | select_type | table      | type   | possible_keys | key   | key_len | ref   | rows | Extra |
+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+
|  1 | PRIMARY     | <derived2> | system | NULL          | NULL  | NULL    | NULL  |    1 | NULL  |
|  2 | DERIVED     | t1         | const  | ename         | ename | 23      | const |    1 | NULL  |
+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+
2 rows in set (0.00 sec)

7、type=NULL
MySQL不用访问表或者索引就可以直接得到结果
(root@localhost) [sakila]> explain select sysdate();
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)</derived2></code></code>
Copy after login
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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks 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)

Introduction to Python functions: Introduction and examples of exec function Introduction to Python functions: Introduction and examples of exec function Nov 03, 2023 pm 02:09 PM

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

Go language indentation specifications and examples Go language indentation specifications and examples Mar 22, 2024 pm 09:33 PM

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

Oracle DECODE function detailed explanation and usage examples Oracle DECODE function detailed explanation and usage examples Mar 08, 2024 pm 03:51 PM

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

Introduction to Python functions: Usage and examples of abs function Introduction to Python functions: Usage and examples of abs function Nov 03, 2023 pm 12:05 PM

Introduction to Python functions: usage and examples of the abs function 1. Introduction to the usage of the abs function In Python, the abs function is a built-in function used to calculate the absolute value of a given value. It can accept a numeric argument and return the absolute value of that number. The basic syntax of the abs function is as follows: abs(x) where x is the numerical parameter to calculate the absolute value, which can be an integer or a floating point number. 2. Examples of abs function Below we will show the usage of abs function through some specific examples: Example 1: Calculation

Introduction to Python functions: functions and examples of eval function Introduction to Python functions: functions and examples of eval function Nov 04, 2023 pm 12:24 PM

Introduction to Python functions: functions and examples of the eval function In Python programming, the eval function is a very useful function. The eval function can execute a string as program code, and its function is very powerful. In this article, we will introduce the detailed functions of the eval function, as well as some usage examples. 1. Function of eval function The function of eval function is very simple. It can execute a string as Python code. This means that we can convert a string

Introduction to Python functions: Usage and examples of isinstance function Introduction to Python functions: Usage and examples of isinstance function Nov 04, 2023 pm 03:15 PM

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

Introduction to Python functions: functions and examples of sorted function Introduction to Python functions: functions and examples of sorted function Nov 03, 2023 pm 02:47 PM

Introduction to Python functions: functions and examples of sorted functions Python is a very powerful programming language with a wealth of built-in functions and modules. In this series of articles, we will introduce the commonly used functions of Python one by one and provide corresponding examples to help readers better understand and apply these functions. This article will introduce the functions and examples of the sorted function in detail. The sorted function is used to sort an iterable object and return a new sorted list. Can be used for numbers and words

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

See all articles