赞
踩
在MySQL中,并不是你建立了索引,并且你在SQL中使用到了该列,MySQL就肯定会使用到那些索引的,有一些情况很可能在你不知不觉中,你就“成功的避开了”MySQL的所有索引。
- 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');
- SELECT * FROM USER WHERE username LIKE 'mysql测试%' --走索引
- SELECT * FROM USER WHERE username LIKE '%mysql测试' --不走索引
- SELECT * FROM USER WHERE username LIKE '%mysql测试%' --不走索引
- -- stock_code字符串类型带索引
- SELECT * FROM `stock_data` WHERE stock_code = '600538' --走索引
- SELECT * FROM `stock_data` WHERE stock_code = 600538 --不走索引
- -- stock_code带索引,open不带索引
- SELECT * FROM `stock_data` WHERE `stock_code` = '600538' OR `open` = 6.62 -- 不走索引
- -- stock_code带索引,up_down_pre带索引
- SELECT * FROM `stock_data` WHERE `stock_code` = '600538' OR `up_down_pre` = 5.1 -- 走索引
SELECT * FROM t_user WHERE username <> 'mysql测试'
- SELECT * FROM t_user WHERE username IS NULL -- 不走索引
- SELECT * FROM t_user WHERE username IS NOT NULL -- 不走索引
- -- stock_code数据类型为varchar
- SELECT * FROM `stock_data` WHERE `stock_code` IN ('600538') -- 走索引
- SELECT * FROM `stock_data` WHERE `stock_code` IN ('600538','688663','688280') -- 走索引
- SELECT * FROM `stock_data` WHERE `stock_code` IN (大量数据) -- 不走索引
- SELECT * FROM `stock_data` WHERE `stock_code` IN (600538) -- 不走索引
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。