怎么在MySQL中使用explain分析索引的走向?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
你所需要的網站建設服務,我們均能行業靠前的水平為你提供.標準是產品質量的保證,主要從事成都做網站、網站制作、成都外貿網站建設、企業網站建設、手機網站制作設計、網頁設計、品牌網站制作、網頁制作、做網站、建網站。創新互聯擁有實力堅強的技術研發團隊及素養的視覺設計專才。
準備工作
1、用戶表一張,有uid ,user_name,real_name ,eamil等字段,詳細見建表語句
2、在user_name字段下增加一個簡單索引user_name,在email,mobile,age三個字段下增加索引complex_index
3、表引擎使用MyISAM,增加
4、準備97000條數據(具體的可以根據實際情況來定數據量,這里準備的是97000+)
5、實驗工具Navcat
建表語句
DROP TABLE IF EXISTS `qz_users`; CREATE TABLE `qz_users` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用戶的 UID', `user_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用戶名', `real_name` varchar(128) CHARACTER SET utf8 DEFAULT NULL COMMENT '用戶姓名', `email` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT 'EMAIL', `mobile` varchar(16) CHARACTER SET utf8 DEFAULT NULL COMMENT '用戶手機', `password` varchar(32) CHARACTER SET utf8 DEFAULT NULL COMMENT '用戶密碼', `salt` varchar(16) CHARACTER SET utf8 DEFAULT NULL COMMENT '用戶附加混淆碼', `avatar_file` varchar(128) CHARACTER SET utf8 DEFAULT NULL COMMENT '頭像文件', `sex` tinyint(1) DEFAULT NULL COMMENT '性別', `birthday` int(10) DEFAULT NULL COMMENT '生日', PRIMARY KEY (`uid`), KEY `user_name` (`user_name`(250)), KEY `complex_index` (`email`,`mobile`,`sex`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
準備的查詢語句
explain select * from qz_users where user_name = "ryanhe"; explain select * from qz_users where email = "x"; explain select * from qz_users where email = "x" and mobile = "x" and sex=1; explain select * from qz_users where email = "x" and mobile = "x"; explain select * from qz_users where email = "x" and sex = "x"; explain select * from qz_users where sex = "x" and mobile = "x"; explain select * from qz_users where mobile = "x" and sex = "0";
結果分析
使用 user_name 條件
explain select * from qz_users where user_name= "x";
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 是 | user_name | 1 | 
使用 email 條件
explain select * from qz_users where email = "x";
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 是 | complex_index | 7 | 
使用 email + mobile + sex條件
explain select * from qz_users where email = "x" and mobile = "x" and sex=1;
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 是 | complex_index | 1 | 
使用 email + mobile 條件
explain select * from qz_users where email = "x" and mobile = "x";
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 是 | complex_index | 7 | 
使用 email + sex 條件
explain select * from qz_users where email = "x" and sex = "x";
結果

分析
| ][3] 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 是 | complex_index | 7 | 
使用 sex + mobile 條件
explain select * from qz_users where sex = "x" and mobile = "x";
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 否 | 97185 | 
使用 mobile+ sex 條件
explain select * from qz_users where mobile = "18602199680" and sex = "0";
結果

分析
| 是否走索引 | 索引名稱 | 掃描記錄數 | 
|---|---|---|
| 否 | 97185 | 
看完上述內容,你們掌握怎么在Mysql中使用explain分析索引的走向的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
                文章名稱:怎么在Mysql中使用explain分析索引的走向
                
                分享路徑:http://www.yijiale78.com/article24/pceoce.html
            
成都網站建設公司_創新互聯,為您提供營銷型網站建設、微信小程序、品牌網站建設、全網營銷推廣、外貿網站建設、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯
