您好,登錄后才能下訂單哦!
怎么在Mysql中使用explain分析索引的走向?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
準備工作
1、用戶表一張,有uid ,user_name,real_name ,eamil等字段,詳細見建表語句
2、在user_name字段下增加一個簡單索引user_name,在email,mobile,age三個字段下增加索引complex_index
3、表引擎使用MyISAM,增加
4、準備97000條數(shù)據(jù)(具體的可以根據(jù)實際情況來定數(shù)據(jù)量,這里準備的是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";
結(jié)果分析
使用 user_name 條件
explain select * from qz_users where user_name= "x";
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
是 | user_name | 1 |
使用 email 條件
explain select * from qz_users where email = "x";
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
是 | complex_index | 7 |
使用 email + mobile + sex條件
explain select * from qz_users where email = "x" and mobile = "x" and sex=1;
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
是 | complex_index | 1 |
使用 email + mobile 條件
explain select * from qz_users where email = "x" and mobile = "x";
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
是 | complex_index | 7 |
使用 email + sex 條件
explain select * from qz_users where email = "x" and sex = "x";
結(jié)果
分析
][3] 是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
是 | complex_index | 7 |
使用 sex + mobile 條件
explain select * from qz_users where sex = "x" and mobile = "x";
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
否 | 97185 |
使用 mobile+ sex 條件
explain select * from qz_users where mobile = "18602199680" and sex = "0";
結(jié)果
分析
是否走索引 | 索引名稱 | 掃描記錄數(shù) |
---|---|---|
否 | 97185 |
看完上述內(nèi)容,你們掌握怎么在Mysql中使用explain分析索引的走向的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。