有个大表(100万)a , 上面有个索引(id , nameid ) 这里id为整型nameid 也是整型
 现在从慢查询日志里找到一个sql如下
select.     a  a , bbb  b where .  a.eid=b.id    ...   and  a.id in (1,2,3,4,5,6,7,8,......)1  请问我还需要再建立一个id的索引   (这个索引只有一个字段id )
  (select.     a  a , bbb  b where .  a.eid=b.id    ...   and  a.id in (1,2,3,4,5,6,7,8,......)
从explain里分析可以知道他已经走了 a的索引(id , nameid )              )2  我看到有一个sql 是这样写 的    (select...  where username like '%str%')
我explian发现他也是走索引的 。   但是我还是想把他换成regexp  这样就保证走索引 了。这样可行否??

解决方案 »

  1.   

    1.不用再建id的索引了,mysql的索引是按最左前缀的,你原来的索引已包含id了
    2.对于'%str%',不是必须使用的就不用。就算走了索引也要看其效率。
      

  2.   

     like '%str%'
    这种查询不会利用索引的。
      

  3.   

    like '%str%'
    explain  却发现有的
    mysql> explain select id from a where userName like '%1%';
    +----+-------------+------------+-------+---------------+-----------------+---------+------+-------+--------------------------+
    | id | select_type | table      | type  | possible_keys | key             | key_len | ref  | rows  | Extra                    |
    +----+-------------+------------+-------+---------------+-----------------+---------+------+-------+--------------------------+
    |  1 | SIMPLE      | a          | index | NULL          | username_UE_idx | 753     | NULL | 29019 | Using where; Using index |经过观察他实际还是走了索引的