濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > mysql優(yōu)化利器之explain使用介紹

mysql優(yōu)化利器之explain使用介紹

熱門(mén)標(biāo)簽:谷歌地圖標(biāo)注位置圖解 如何選擇優(yōu)質(zhì)的外呼系統(tǒng) 東莞外呼企業(yè)管理系統(tǒng) 桂林云電銷(xiāo)機(jī)器人收費(fèi) 手機(jī)外呼系統(tǒng)違法嗎 清遠(yuǎn)申請(qǐng)400電話(huà) 南通電銷(xiāo)外呼系統(tǒng)哪家強(qiáng) 地圖簡(jiǎn)圖標(biāo)注 沈陽(yáng)智能外呼系統(tǒng)供應(yīng)商

一、語(yǔ)法

{EXPLAIN | DESCRIBE | DESC}
  tbl_name [col_name | wild] 
{EXPLAIN | DESCRIBE | DESC}
  [explain_type] SELECT select_options 
explain_type: {EXTENDED | PARTITIONS}

二、數(shù)據(jù)庫(kù)準(zhǔn)備

表一:

DROP TABLE IF EXISTS `products`;
SET @saved_cs_client   = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `products` (
 `products_id` int(11) unsigned NOT NULL auto_increment,
 `products_type` int(11) unsigned NOT NULL default '1',
 `products_quantity` float NOT NULL default '0',
 `products_model` varchar(32) default NULL,
 `products_upc` varchar(32) default NULL,
 `products_isbn` varchar(32) default NULL,
 `products_image` varchar(128) default NULL,
 `products_image_thumbnail` varchar(200) NOT NULL,
 `products_price` decimal(15,4) NOT NULL default '0.0000',
 `products_virtual` tinyint(1) NOT NULL default '0',
 `products_date_added` datetime NOT NULL default '0001-01-01 00:00:00',
 `products_last_modified` datetime default NULL,
 `products_date_available` datetime default NULL,
 `products_weight` float NOT NULL default '0',
 `products_status` tinyint(1) NOT NULL default '0',
 `products_tax_class_id` int(11) NOT NULL default '0',
 `manufacturers_id` int(11) default NULL,
 `products_web_id` int(11) default NULL,
 `products_ordered` float NOT NULL default '0',
 `products_quantity_order_min` float NOT NULL default '1',
 `products_quantity_order_units` float NOT NULL default '1',
 `products_priced_by_attribute` tinyint(1) NOT NULL default '0',
 `product_is_free` tinyint(1) NOT NULL default '0',
 `product_is_call` tinyint(1) NOT NULL default '0',
 `products_quantity_mixed` tinyint(1) NOT NULL default '0',
 `product_is_always_free_shipping` tinyint(1) NOT NULL default '0',
 `products_qty_box_status` tinyint(1) NOT NULL default '1',
 `products_quantity_order_max` float NOT NULL default '0',
 `products_sort_order` int(11) NOT NULL default '0',
 `products_discount_type` tinyint(1) NOT NULL default '0',
 `products_discount_type_from` tinyint(1) NOT NULL default '0',
 `products_price_sorter` decimal(15,4) NOT NULL default '0.0000',
 `master_categories_id` int(11) NOT NULL default '0',
 `products_mixed_discount_quantity` tinyint(1) NOT NULL default '1',
 `metatags_title_status` tinyint(1) NOT NULL default '0',
 `metatags_products_name_status` tinyint(1) NOT NULL default '0',
 `metatags_model_status` tinyint(1) NOT NULL default '0',
 `metatags_price_status` tinyint(1) NOT NULL default '0',
 `metatags_title_tagline_status` tinyint(1) NOT NULL default '0',
 `itemno` varchar(32) default NULL,
 `products_images_no` varchar(10) default '0',
 `products_url` varchar(512) default NULL,
 PRIMARY KEY (`products_id`),
 UNIQUE KEY `itemno` (`itemno`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

表二:

DROP TABLE IF EXISTS `products_image`;
SET @saved_cs_client   = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `products_image` (
 `id` int(10) unsigned NOT NULL auto_increment,
 `products_id` int(10) unsigned NOT NULL,
 `products_images_no` varchar(10) default '0',
 `image_dir` varchar(200) default NULL,
 `products_image_thumbnail` varchar(200) default NULL,
 `flag` int(2) default NULL,
 `up_time` datetime default NULL,
 `web_from` varchar(20) default NULL,
 PRIMARY KEY (`id`),
 KEY `idx_porducts_id` (`products_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

三、關(guān)于explain選項(xiàng)

下面是一個(gè)實(shí)例:

mysql> explain select products_id from products limit 1;
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key   | key_len | ref | rows | Extra    |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE   | products | index | NULL     | PRIMARY | 4    | NULL | 3113 | Using index |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+

id

MySQL  Query  Optimizer選定的執(zhí)行計(jì)劃中查詢(xún)的序列號(hào)。
表示查詢(xún)中執(zhí)行select子句或操作表的順序,id值越大優(yōu)先級(jí)越高,越先被執(zhí)行。id相同,執(zhí)行順序由上至下

select_type

1、SIMPLE:簡(jiǎn)單的select查詢(xún),不使用union及子查詢(xún)
2、PRIMARY:最外層的select查詢(xún)
3、UNION:UNION中的第二個(gè)或隨后的select查詢(xún),不依賴(lài)于外部查詢(xún)的結(jié)果集
4、DEPENDENT UNION:UNION中的第二個(gè)或隨后的select查詢(xún),依賴(lài)于外部查詢(xún)的結(jié)果集
5、UNION RESULT: UNION查詢(xún)的結(jié)果集SUBQUERY子查詢(xún)中的第一個(gè)select查詢(xún),不依賴(lài)于外部查詢(xún)的結(jié)果集
6、DEPENDENT SUBQUERY:子查詢(xún)中的第一個(gè)select查詢(xún),依賴(lài)于外部查詢(xún)的結(jié)果集DERIVED用于from子句里有子查詢(xún)的情況。
    MySQL會(huì)遞歸執(zhí)行這些子查詢(xún),把結(jié)果放在臨時(shí)表里。
7、UNCACHEABLE SUBQUERY:結(jié)果集不能被緩存的子查詢(xún),必須重新為外層查詢(xún)的每一行進(jìn)行評(píng)估
8、UNCACHEABLE UNION:UNION中的第二個(gè)或隨后的select查詢(xún),屬于不可緩存的子查詢(xún)

table

1、system:表僅有一行(系統(tǒng)表)。這是const連接類(lèi)型的一個(gè)特例。
2、const:const用于用常數(shù)值比較PRIMARY KEY時(shí)。當(dāng)查詢(xún)的表僅有一行時(shí),使用system。
3、eq_ref:除const類(lèi)型外最好的可能實(shí)現(xiàn)的連接類(lèi)型。它用在一個(gè)索引的所有部分被連接使用并且索引是UNIQUE或PRIMARY KEY,
    對(duì)于每個(gè)索引鍵,表中只有一條記錄與之匹配。
4、ref:連接不能基于關(guān)鍵字選擇單個(gè)行,可能查找到多個(gè)符合條件的行。叫做ref是因?yàn)樗饕硞€(gè)參考值相比較。
    這個(gè)參考值或者是一個(gè)常數(shù),或者是來(lái)自一個(gè)表里的多表查詢(xún)的結(jié)果值。
5、ref_or_null:如同ref,但是MySQL必須在初次查找的結(jié)果里找出null條目,然后進(jìn)行二次查找。
6、index_merge:說(shuō)明索引合并優(yōu)化被使用了。
7、unique_subquery:在某些IN查詢(xún)中使用此種類(lèi)型,而不是常規(guī)的ref:
    value IN (SELECT primary_key FROM single_table WHERE some_expr)
    index_subquery在某些IN查詢(xún)中使用此種類(lèi)型,與unique_subquery類(lèi)似,但是查詢(xún)的是非唯一性索引:
    value IN (SELECT key_column FROM single_table WHERE some_expr)
8、range:只檢索給定范圍的行,使用一個(gè)索引來(lái)選擇行。key列顯示使用了哪個(gè)索引。
    當(dāng)使用=、>、>、>=、、=、IS NULL、=>、BETWEEN或者IN操作符,用常量比較關(guān)鍵字列時(shí),可以使用range。
9、index:全表掃描,只是掃描表的時(shí)候按照索引次序進(jìn)行而不是行。主要優(yōu)點(diǎn)就是避免了排序,但是開(kāi)銷(xiāo)仍然非常大。
10、all:最壞的情況,從頭到尾全表掃描

 others

possible_keys:指出mysql能在該表中使用哪些索引有助于查詢(xún)。如果為空,說(shuō)明沒(méi)有可用的索引
key:mysql實(shí)際從possible_key選擇使用的索引。如果為null,則沒(méi)有使用索引。
    很少的情況下,mysql會(huì)選擇優(yōu)化不足的索引。這種情況下,
    可以在select語(yǔ)句中使用use  index(indexname)來(lái)強(qiáng)制使用一個(gè)索引
    或者用ignore  index(indexname)來(lái)強(qiáng)制mysql忽略索引
key_len:使用的索引的長(zhǎng)度。在不損失精確性的情況下,長(zhǎng)度越短越好
ref:顯示索引的哪一列被使用了
rows:mysql認(rèn)為必須檢查的用來(lái)返回請(qǐng)求數(shù)據(jù)的行數(shù)

extra

1、Distinct: 一旦mysql找到了與行相聯(lián)合匹配的行,就不再搜索了。
2、Not exists: mysql 優(yōu)化了LEFT JOIN,一旦它找到了匹配LEFT JOIN標(biāo)準(zhǔn)的行,就不再搜索了。
3、Range checked for each: Record(index map:#)沒(méi)有找到理想的索引,
    因此對(duì)于從前面表中來(lái)的每一個(gè)行組合,mysql檢查使用哪個(gè)索引,并用它來(lái)從表中返回行。這是使用索引的最慢的連接之一。
4、Using filesort: 表示MySQL會(huì)對(duì)結(jié)果使用一個(gè)外部索引排序,而不是從表里按索引次序讀到相關(guān)內(nèi)容。
    可能在內(nèi)存或者磁盤(pán)上進(jìn)行排序。MySQL中無(wú)法利用索引完成的排序操作稱(chēng)為“文件排序”。
5、Using index: 列數(shù)據(jù)是從僅僅使用了索引中的信息而沒(méi)有讀取實(shí)際的行動(dòng)的表返回的,
    這發(fā)生在對(duì)表的全部的請(qǐng)求列都是同一個(gè)索引的部分的時(shí)候。
6、Using temporary: mysql需要?jiǎng)?chuàng)建一個(gè)臨時(shí)表來(lái)存儲(chǔ)結(jié)果,這通常發(fā)生在對(duì)不同的列集進(jìn)行ORDER BY上,而不是GROUP BY上。
7、Using where: 使用了WHERE從句來(lái)限制哪些行將與下一張表匹配或者是返回給用戶(hù)。
    如果不想返回表中的全部行,并且連接類(lèi)型ALL或index,這就會(huì)發(fā)生,或者是查詢(xún)有問(wèn)題。

四、具體的實(shí)例

1、mysql版本

mysql> select version();
+------------+
| version() |
+------------+
| 5.1.73-log |
+------------+
1 row in set (0.00 sec)

2、sql語(yǔ)句分析1

mysql> explain select products_id from products; 
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key   | key_len | ref | rows | Extra    |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE   | products | index | NULL     | PRIMARY | 4    | NULL | 3113 | Using index |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------

3、sql語(yǔ)句分析2

mysql> explain select products_id from (select * from products limit 10) b ;    
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| 1 | PRIMARY   | derived2> | ALL | NULL     | NULL | NULL  | NULL |  10 |    |
| 2 | DERIVED   | products  | ALL | NULL     | NULL | NULL  | NULL | 3113 |    |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+

4、sql語(yǔ)句分析3

mysql> explain select products_id from products where products_id=10 union select products_id \

from products where products_id=20 ;
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table   | type | possible_keys | key   | key_len | ref  | rows | Extra    |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY   | products  | const | PRIMARY    | PRIMARY | 4    | const |  1 | Using index |
| 2 | UNION    | products  | const | PRIMARY    | PRIMARY | 4    | const |  1 | Using index |
| NULL | UNION RESULT | union1,2> | ALL  | NULL     | NULL  | NULL  | NULL | NULL |       |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+

5、sql語(yǔ)句分析4

mysql> explain select * from products where products_id in ( select products_id from products where \

products_id=10 union select products_id from products where products_id=20 );
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type    | table   | type | possible_keys | key   | key_len | ref  | rows | Extra    |
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY      | products  | ALL  | NULL     | NULL  | NULL  | NULL | 3113 | Using where |
| 2 | DEPENDENT SUBQUERY | products  | const | PRIMARY    | PRIMARY | 4    | const |  1 | Using index |
| 3 | DEPENDENT UNION  | products  | const | PRIMARY    | PRIMARY | 4    | const |  1 | Using index |
| NULL | UNION RESULT    | union2,3> | ALL  | NULL     | NULL  | NULL  | NULL | NULL |       |
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+

完成

您可能感興趣的文章:
  • MySQL性能分析及explain的使用說(shuō)明
  • mysql之explain使用詳解(分析索引)
  • MySql中如何使用 explain 查詢(xún) SQL 的執(zhí)行計(jì)劃
  • mysql開(kāi)啟慢查詢(xún)(EXPLAIN SQL語(yǔ)句使用介紹)
  • mysql explain的用法(使用explain優(yōu)化查詢(xún)語(yǔ)句)
  • MySQL性能優(yōu)化神器Explain的基本使用分析
  • Mysql實(shí)驗(yàn)之使用explain分析索引的走向
  • Mysql Explain命令的使用與分析
  • MySQL Explain使用詳解
  • MySQL中explain語(yǔ)句的基本使用教程

標(biāo)簽:湖州 貴州 天津 臨沂 內(nèi)蒙古 成都 重慶 常德

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql優(yōu)化利器之explain使用介紹》,本文關(guān)鍵詞  mysql,優(yōu)化,利器,之,explain,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql優(yōu)化利器之explain使用介紹》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于mysql優(yōu)化利器之explain使用介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    诏安县| 甘肃省| 大英县| 郯城县| 北安市| 彩票| 南漳县| 固镇县| 乳源| 东光县| 彭山县| 遂平县| 灵寿县| 黑龙江省| 永川市| 靖州| 阜新| 仙游县| 德钦县| 托克逊县| 永泰县| 清涧县| 嘉鱼县| 西华县| 平舆县| 通许县| 江阴市| 天津市| 苗栗市| 泽普县| 云梦县| 宜兰市| 长兴县| 正蓝旗| 崇明县| 黄陵县| 全椒县| 凤凰县| 锦屏县| 合阳县| 如皋市|