CREATE DEFINER=`root`@`localhost` PROCEDURE `P_teset`() BEGIN DECLARE v_val VARCHAR(20); DECLARE v_str VARCHAR(20); DECLARE v_i INT ; DECLARE v_j INT;
SET v_str ='abcdefghijklmnopqrstuvwxyz'; SET v_i=0; SET v_j=0;
WHILE v_i<600000 DO SET v_val ='';
WHILE v_j< 13 DO SET v_val= CONCAT(v_val,SUBSTRING(v_str,1,FLOOR(1+RAND()*26))); SET v_j=v_j+1; END WHILE; SET v_j=0; INSERT INTO tb_test(fval) VALUES(v_val);
961406条用时1.422s --------------------------------------------- 加索引: ALTER TABLE tb_test ADD INDEX my_index(fval); --------------------------------------------- 再测试: SELECT * FROM tb_test WHERE fval LIKE '%ab';
70231条 用时1.094s
SELECT * FROM tb_test WHERE fval LIKE 'ab%'
961406条用时1.485s
表为MyISAM格式。
为什么加了索引却慢了?
------解决方案-------------------- LIKE '%ab';不走索引
------解决方案-------------------- 这个时间和缓存也有关系,你最好每次执行sql都重启下数据库已清空缓存
------解决方案--------------------