目錄
本文MySQL索引失效的各種效情況,對每種情況寫出示例SQL並在數據庫中查看執行計划。
一、環境信息
CentOS 7.4
Mysql 5.7.32
二、表、數據准備
建立一張用戶表
CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8;
USE `test`;
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint NOT NULL DEFAULT 0 COMMENT '主鍵,用戶唯一id',
`user_name` varchar(32) NOT NULL DEFAULT '' COMMENT '用戶名',
`password` varchar(64) NOT NULL DEFAULT '' COMMENT '密碼',
`email` varchar(32) NOT NULL DEFAULT '' COMMENT '郵箱',
`phone_number` varchar(16) NOT NULL DEFAULT '' COMMENT '電話號碼',
`avatar` varchar(256) NOT NULL DEFAULT '' COMMENT '頭像',
`create_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '用戶賬號創建時間',
`update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '上次更新記錄時間',
`last_login_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '上次登錄時間',
`status` int(2) NOT NULL DEFAULT 0 COMMENT '用戶狀態 0-正常 1-封禁',
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '用戶信息表';
創建存儲過程,插入10萬條測試數據
DROP PROCEDURE if exists insert_t_user_test;
DELIMITER $$
CREATE PROCEDURE insert_t_user_test(IN loop_times INT)
BEGIN
DECLARE var INT DEFAULT 0;
WHILE var < loop_times DO
SET var = var + 1;
INSERT INTO `t_user` VALUES (var, CONCAT('rkyao-', var), '123456', 'rkyao@163.com', '15251831704', 'avatar.jpg', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
END WHILE;
COMMIT;
END $$
CALL insert_t_user_test(100000);
三、復合索引的失效情況
創建復合索引
CREATE INDEX `idx_user` ON `t_user` (`user_name`, `email`, `phone_number`);
最左前綴法則
不符合最左前綴法則會使索引失效,必須從索引的最左列開始查詢,且不跳過中間列,和where指定的條件順序無關。
-- where查詢字段順序和索引定義一致
-- 走索引 命中三個字段 user_name, email, phone_number
explain select * from t_user where user_name = 'rkyao-1' and email = 'rkyao@163.com' and phone_number = '15251831704';
-- where查詢字段順序和索引定義不一致
-- 走索引 命中三個字段 user_name, email, phone_number
explain select * from t_user where phone_number = '15251831704' and email = 'rkyao@163.com' and user_name = 'rkyao-1';
-- 走索引 命中兩個字段 user_name, email
explain select * from t_user where user_name = 'rkyao-1' and email = 'rkyao@163.com';
-- 走索引 命中一個字段 user_name
explain select * from t_user where user_name = 'rkyao-1' and phone_number = '15251831704';
-- where查詢字段不包含索引最左邊的列
-- 不走索引
explain select * from t_user where email = 'rkyao@163.com' and phone_number = '15251831704';
or查詢
or查詢會使索引失效。
-- 不走索引
explain select * from t_user where user_name = 'rkyao-1' or email = 'rkyao@163.com';
四、單列索引的失效情況
在user_name
和email
字段上分別創建單列索引
-- 刪除聯合索引
DROP INDEX `idx_user` ON `t_user`;
-- 創建單列索引
CREATE INDEX `idx_user_name` ON `t_user` (`user_name`);
CREATE INDEX `idx_user_email` ON `t_user` (`email`);
like模糊查詢使用前通配符
模糊查詢時使用前通配符匹配會使索引失效。
-- 不走索引
explain select * from t_user where user_name like '%555%';
-- 不走索引
explain select * from t_user where user_name like '%555';
-- 走索引
explain select * from t_user where user_name like '555%';
-- 走索引
explain select * from t_user where user_name like '555';
索引列上使用函數
索引列上使用函數,或者進行運算,不走索引
-- 不走索引
explain select * from t_user where concat(user_name, '123') = '555';
字符串索引沒加引號
查詢字符串類型字段時,條件值沒加引號,不走索引
-- 不走索引
explain select * from t_user where user_name = 555;
使用 != 、 <>、>、<
使用不等於,不走索引。
-- 不走索引
explain select * from t_user where user_name != '555';
-- 不走索引
explain select * from t_user where user_name <> '555';
-- 不走索引
explain select * from t_user where user_name > '555';
is null 或 is not null
is null 或 is not null,不走索引。
-- 不走索引
explain select * from t_user where user_name is null;
-- 不走索引
explain select * from t_user where user_name is not null;
in 或 not in
in走索引,使用not in不走索引。
-- 走索引
explain select * from t_user where user_name in ('rkyao-6222', 'rkyao-5678');
-- 不走索引
explain select * from t_user where user_name not in ('rkyao-6222', 'rkyao-5678');
兩個單列索引and查詢
where條件里and查詢兩個索引列時,只會有一個索引生效,也就是創建時間早的索引生效。
-- 走索引 命中user_name字段
explain select * from t_user where email = '123@163.com' and user_name = '555';
兩個單列索引or查詢
where條件里or查詢兩個索引列時,兩個索引都生效。
-- 走索引 命中user_name字段
explain select * from t_user where email = '123@163.com' or user_name = '555';