日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 执行计划_mysql执行计划

發布時間:2025/3/15 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 执行计划_mysql执行计划 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

執行計劃使用explain sql查詢。

1、 構造數據

usecoshaho002;drop table if existsinfo;create tableinfo(

idint primary keyAUTO_INCREMENT,

namevarchar(32),

agetinyint,

sexvarchar(8),

addressvarchar(32),

phonevarchar(32),

birthday date,

descriptionvarchar(128)

);alter table info add unique(phone);alter table info add indexname_index(name);alter table info add indexname_age_sex_index(name, age, sex);alter table info addfulltext(description);

describe info;INSERT INTO info VALUES(null, 'hkx1', 1, 'M', 'sc', '1234567890', '1989-01-03', 'chengxuyuan huawei1'),

(null, 'hkx2', 2, 'F', 'sc', '1234567891', '1989-02-03', 'chengxuyuan huawei2'),

(null, 'hkx3', 3, 'M', 'sc', '1234567892', '1989-03-03', 'chengxuyuan huawei3'),

(null, 'hkx4', 4, 'F', 'sc', '1234567893', '1989-07-05', 'chengxuyuan huawei4'),

(null, 'hkx5', 5, 'F', 'sc', '1234567894', '1989-07-04', 'chengxuyuan huawei5'),

(null, 'hkx6', 6, 'M', 'sc', '1234567895', '1989-07-06', 'chengxuyuan huawei6'),

(null, 'hkx7', 7, 'F', 'sc', '1234567896', '1989-07-07', 'chengxuyuan huawei7'),

(null, 'hkx1', 8, 'F', 'sc', '1234567897', '1989-07-08', 'chengxuyuan huawei8'),

(null, 'hkx2', 9, 'F', 'sc', '1234567898', '1989-07-03', 'chengxuyuan huawei9'),

(null, 'hkx3', 10, 'M', 'sc', '1234567899', '1989-01-03', 'chengxuyuan huawei10'),

(null, 'hkx4', 11, 'F', 'sc', '1234567810', '1989-07-03', 'chengxuyuan huawei11'),

(null, 'hkx5', 5, 'F', 'sc', '1234567820', '1989-07-03', 'chengxuyuan huawei12'),

(null, 'hkx6', 6, 'M', 'sc', '1234567830', '1989-02-03', 'chengxuyuan huawei13'),

(null, 'hkx7', 10, 'F', 'sc', '1234567840', '1989-09-03', 'chengxuyuan huawei14'),

(null, 'hkx8', 1, 'M', 'sc', '1234567850', '1989-01-03', 'chengxuyuan huawei1'),

(null, 'hkx9', 13, 'F', 'sc', '1234567860', '1989-06-03', 'chengxuyuan huawei2'),

(null, 'hkx0', 2, 'M', 'sc', '1234567870', '1989-07-03', 'chengxuyuan huawei3');select * from info;

2、 查詢執行計劃

usecoshaho002;select * frominfo;

explainselect * from info where id = 2;

explainselect * from info where name = 'hkx1';

explainselect * from info where name like 'hkx1%';

explainselect * from info where name like '%1';

explainselect * from info where age = 2;

explainselect * from info where name = 'hkx1' and sex = 'M';

explainselect * from info where name = 'hkx1' and age = 0;

explainselect * from info where name = 'hkx1' and age = 0 and sex ='M';

explainselect * from info where phone = '1234567890';

explainselect * from info where description = 'chengxuyuan huawei1';

explainselect * from info where name = 'hkx1' union select * from info;

可以看到,查詢結果包含id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,Extra組成。

1、 type

type表訪問類型,和查詢效率緊密相關。type效率從好到壞依次為:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般來說,得保證查詢至少達到range級別,最好能達到ref。

ALL:全表掃描。

index:掃描全部索引樹。

range:掃描部分索引(索引范圍掃描)。常見于between、、like等的查詢。

ref:非唯一性索引掃描,返回匹配某個單獨值的所有行。常見于使用非唯一索查詢,組合索引查詢;

eq_ref:唯一性索引掃描,對于每個索引鍵,表中只有一條記錄與之匹配。常見于主鍵或唯一索引掃描;

const,system:表最多有一個匹配行,它將在查詢開始時被讀取,列值當做常量處理。如將使用主鍵查詢。(system是const特例,表只有一行記錄)

NULL:MySQL在優化過程中分解語句,執行時甚至不用訪問表或索引。

2、 ?id

select查詢的序列號

3、 select_type

select查詢的類型,主要是區別普通查詢和聯合查詢、子查詢之類的復雜查詢。

(1) SIMPLE:查詢中不包含子查詢或者UNION

(2) 查詢中若包含任何復雜的子部分,最外層查詢則被標記為:PRIMARY

(3) 在SELECT或WHERE列表中包含了子查詢,該子查詢被標記為:SUBQUERY

(4) 在FROM列表中包含的子查詢被標記為:DERIVED(衍生)

(5) 若第二個SELECT出現在UNION之后,則被標記為UNION;若UNION包含在?FROM子句的子查詢中,外層SELECT將被標記為:DERIVED

(6) 從UNION表獲取結果的SELECT被標記為:UNION RESULT

4、 table

該行查詢所引用的表。

5、 possible_keys

指出MySQL能使用哪個索引在該表中找到行。查詢涉及到的字段上若存在索引,則該索引將被列出,但不一定被查詢使用。如果是空的,沒有相關的索引。這時要提高性能,可通過檢驗WHERE子句,看是否引用某些字段,或者檢查字段不是適合索引。

6、 key

顯示MySQL實際決定使用的鍵。如果沒有索引被選擇,鍵是NULL。

7、 key_len

顯示MySQL決定使用的鍵長度。表示索引中使用的字節數,可通過該列計算查詢中使用的索引的長度。如果鍵是NULL,長度就是NULL。文檔提示特別注意這個值可以得出一個多重主鍵里mysql實際使用了哪一部分。

注:key_len顯示的值為索引字段的最大可能長度,并非實際使用長度,即key_len是根據表定義計算而得,不是通過表內檢索出的。

8、 ref

顯示哪個字段或常數與key一起被使用。

9、 rows

這個數表示mysql要遍歷多少數據才能找到,表示MySQL根據表統計信息及索引選用情況,估算的找到所需的記錄所需要讀取的行數,在innodb上可能是不準確的。

10、 Extra

包含不適合在其他列中顯示但十分重要的額外信息。

(1)Only index:意味著信息只用索引樹中的信息檢索出的,這比掃描整個表要快。

(2)using where:使用上了where限制,表示MySQL服務器在存儲引擎受到記錄后進行“后過濾”(Post-filter),如果查詢未能使用索引,Using where的作用只是提醒我們MySQL將用where子句來過濾結果集。

(3)impossible where: 表示用不著where,一般就是沒查出來啥。

(4)Using filesort:MySQL中無法利用索引完成的排序操作稱為文件排序,當我們試圖對一個沒有索引的字段進行排序時,就是filesoft。它跟文件沒有任何關系,實際上是內部的一個快速排序。

(5)Using temporary:表示MySQL需要使用臨時表來存儲結果集,常見于排序和分組查詢,使用filesort和temporary的話會很吃力,WHERE和ORDER BY的索引經常無法兼顧,如果按照WHERE來確定索引,那么在ORDER BY時,就必然會引起Using filesort,這就要看是先過濾再排序劃算,還是先排序再過濾劃算。

參考:http://blog.csdn.net/xifeijian/article/details/19773795

總結

以上是生活随笔為你收集整理的mysql 执行计划_mysql执行计划的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。