国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

msyql中Explain的用法詳解

2024-07-24 12:38:27
字體:
供稿:網(wǎng)友

Explain在mysql的作用我想大家都明白,顯示了mysql如何使用索引來處理select語句以及連接表,可以幫助選擇更好的索引和寫出更優(yōu)化的查詢語句.

一.語法:explain < table_name >

例如:explain select * from t3 where id=3952602;

二.explain輸出解釋,代碼如下:

  1. +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ 
  2. | id | select_type | table | type  | possible_keys     | key     | key_len | ref   | rows | Extra | 
  3. +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ 

1.id

我的理解是SQL執(zhí)行的順利的標(biāo)識(shí),SQL從大到小的執(zhí)行.例如:

  1. mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b; 
  2. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 
  3. | id | select_type | table      | type   | possible_keys     | key     | key_len | ref  | rows | Extra | 
  4. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 
  5. |  1 | PRIMARY     | <derived2> | system | NULL              | NULL    | NULL    | NULL |    1 |       | 
  6. |  2 | DERIVED     | <derived3> | system | NULL              | NULL    | NULL    | NULL |    1 |       | 
  7. |  3 | DERIVED     | t3         | const  | PRIMARY,idx_t3_id | PRIMARY | 4       |      |    1 |       | 
  8. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 

很顯然這條SQL是從里向外的執(zhí)行,就是從id=3 向上執(zhí)行.

2.select_type

就是select類型,可以有以下幾種:

(1) SIMPLE

簡(jiǎn)單SELECT(不使用UNION或子查詢等) 例如:

  1. mysql> explain select * from t3 where id=3952602; 
  2. +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ 
  3. | id | select_type | table | type  | possible_keys     | key     | key_len | ref   | rows | Extra | 
  4. +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ 
  5. |  1 | SIMPLE      | t3    | const | PRIMARY,idx_t3_id | PRIMARY | 4       | const |    1 |       | 
  6. +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+  

(2).PRIMARY

我的理解是最外層的select.例如:

  1. mysql> explain select * from (select * from t3 where id=3952602) a ; 
  2. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 
  3. | id | select_type | table      | type   | possible_keys     | key     | key_len | ref  | rows | Extra | 
  4. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 
  5. |  1 | PRIMARY     | <derived2> | system | NULL              | NULL    | NULL    | NULL |    1 |       | 
  6. |  2 | DERIVED     | t3         | const  | PRIMARY,idx_t3_id | PRIMARY | 4       |      |    1 |       | 
  7. +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ 

(3).UNION

UNION中的第二個(gè)或后面的SELECT語句.例如

  1. mysql> explain select * from t3 where id=3952602 union all select * from t3 ; 
  2. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ 
  3. | id | select_type  | table      | type  | possible_keys     | key     | key_len | ref   | rows | Extra | 
  4. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ 
  5. |  1 | PRIMARY      | t3         | const | PRIMARY,idx_t3_id | PRIMARY | 4       | const |    1 |       | 
  6. |  2 | UNION        | t3         | ALL   | NULL              | NULL    | NULL    | NULL  | 1000 |       | 
  7. |NULL | UNION RESULT | <union1,2> | ALL   | NULL              | NULL    | NULL    | NULL  | NULL |       | 
  8. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+  

(4).DEPENDENT UNION

UNION中的第二個(gè)或后面的SELECT語句,取決于外面的查詢,代碼如下:

  1. mysql> explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3)  ; 
  2. +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ 
  3. | id | select_type        | table      | type   | possible_keys     | key     | key_len | ref   | rows | Extra                    | 
  4. +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ 
  5. |  1 | PRIMARY            | t3         | ALL    | NULL              | NULL    | NULL    | NULL  | 1000 | Using where              | 
  6. |  2 | DEPENDENT SUBQUERY | t3         | const  | PRIMARY,idx_t3_id | PRIMARY | 4       | const |    1 | Using index              | 
  7. |  3 | DEPENDENT UNION    | t3         | eq_ref | PRIMARY,idx_t3_id | PRIMARY | 4       | func  |    1 | Using where; Using index | 
  8. |NULL | UNION RESULT       | <union2,3> | ALL    | NULL              | NULL    | NULL    | NULL  | NULL |                          | 
  9. +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ 

(4).UNION RESULT

UNION的結(jié)果,代碼如下:

  1. mysql> explain select * from t3 where id=3952602 union all select * from t3 ; 
  2. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ 
  3. | id | select_type  | table      | type  | possible_keys     | key     | key_len | ref   | rows | Extra | 
  4. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ 
  5. |  1 | PRIMARY      | t3         | const | PRIMARY,idx_t3_id | PRIMARY | 4       | const |    1 |       | 
  6. |  2 | UNION        | t3         | ALL   | NULL              | NULL    | NULL    | NULL  | 1000 |       | 
  7. |NULL | UNION RESULT | <union1,2> | ALL   | NULL              | NULL    | NULL    | NULL  | NULL |       | 
  8. +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ 

(5).SUBQUERY

子查詢中的第一個(gè)SELECT,代碼如下:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄱阳县| 虹口区| 江川县| 夏邑县| 从江县| 江门市| 锡林浩特市| 西昌市| 上林县| 独山县| 甘泉县| 靖安县| 盐池县| 绥芬河市| 尖扎县| 嘉兴市| 博客| 娱乐| 渝中区| 朝阳市| 桃江县| 许昌市| 景德镇市| 两当县| 房产| 六安市| 邳州市| 定南县| 象州县| 黄陵县| 堆龙德庆县| 南木林县| 甘肃省| 江源县| 广饶县| 阿拉善盟| 航空| 灵宝市| 讷河市| 赫章县| 和林格尔县|