-- 删除历史存储过程 DROP PROCEDURE IF EXISTS `insert_t_user`
-- 创建存储过程 delimiter $
CREATE PROCEDURE insert_t_user(IN limit_num int) BEGIN DECLARE i INT DEFAULT 10; DECLARE id_no varchar(18) ; DECLARE username varchar(32) ; DECLARE age TINYINT DEFAULT 1; WHILE i < limit_num DO SET id_no = CONCAT("NO", i); SET username = CONCAT("Tom",i); SET age = FLOOR(10 + RAND()*2); INSERT INTO `t_user` VALUES (NULL, id_no, username, age, NOW()); SET i = i + 1; END WHILE;
END $ -- 调用存储过程 call insert_t_user(100); 关于存储过程的创建和存储,可暂时不执行,当用到时再执行。
-- 内存计算,得知要查询的id为1 explain select * from t_user where id = 1 ; -- 参数侧计算 explain select * from t_user where id = 2 - 1 ; 第三种索引失效情况:索引列参与了运算,会导致全表扫描,索引失效。
4 索引列参使用了函数 示例:
explain select * from t_user where SUBSTR(id_no,1,3) = '100';
explain select id from t_user order by age; explain select id , username from t_user order by age; explain select id_no from t_user order by id_no; 上述三条SQL语句都是走索引的,也就是说覆盖索引的场景也是可以正常走索引的。
现在将id和id_no组合起来进行order by:
explain select * from t_user order by id,id_no desc; explain select * from t_user order by id,id_no desc limit 10; explain select * from t_user order by id_no desc,username desc;