MySQL索引使用问题

数据表结构 自增ID, tel(varchar(11),普通索引);

当使用如下SQL语句时 :

     explain SELECT `uid` FROM `purchase_user` WHERE tel = "XX" or tel = "XX" or tel = "XX" or tel = "XX" or tel = "XX" or tel ="YY";

其中XX数据,数据表中已存在记录,YY数据表中不存在; 则没有使用索引,进行了全表扫描;

当SQL语句如下时 :

     explain SELECT `uid` FROM `purchase_user` WHERE tel = "XX" or tel = "XX" or tel ="YY";

此时则使用了索引进行查询,而非全表扫描, 将 or 替换成 in 也是如此.

阅读 2.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题