赞
踩
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`gender` int(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `age` (`age`),
KEY `username` (`username`(191))
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4;
# 查看索引
show keys from users
explain select * from t_user where username = 'admin'
SELECT `username` FROM `t_user` WHERE age=20;-- 会使用索引
SELECT `username` FROM `t_user` WHERE age+10=30;-- 不会使用索引!!因为所有索引列参与了计算
SELECT `username` FROM `t_user` WHERE age=30-10;-- 会使用索引
-- 不会使用索引,因为使用了函数运算,原理与上面相同
SELECT username FROM t_user WHERE concat(username,'1') ='admin1';
-- 会使用索引
SELECT username FROM t_user WHERE username =concat('admin','1');
like 模糊查询 前模糊或者 全模糊不走索引
select * from user where username like '%mysql测试'
SELECT * FROM t_user WHERE `age`='23' -- 走索引
select * from t_user where username = 'mysql测试' or password ='123456'
select * from t_user where username <> 'mysql测试'
select * from t_user where username is not null --is not null 不走索引
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。