最近发现一个很奇怪的问题    有两张表    
a
bigid   INT   11
BusID   INT   11
StopIndex   INT  11a上建了索引
BusID_index    `BusID`b   
BusID1   INT   11
StopIndex1   INT  11
b上建了索引
b_s_inxex    `busid1`, `stopindex1`
写了一条sql     select  *  from   a   , b    where   a.bigid  = 1   and   b.busid1 = a.busid1  
and   b.stopindex1 > a.StopIndex   运行了一下  执行计划是这样的
1 SIMPLE a range BusID_index   BusID_index 4 const 206 Using where; Using index1 SIMPLE b ref b_s_inxex  b_s_inxex  5 busdata.a.BusID 51 Using where; Using index第二条只用了busdata.a.BusID   也就是说明 b.stopindex1 > a.StopIndex     这句没用到索引   
为什么mysql不用索引呢  
这只是一大段sql中的一部分   效率太差了  困扰了我好几天  请高手指教原因  跪谢   

解决方案 »

  1.   

    show index from a;
    show index from b;看一下结果。 MYSQL会根据索引上的分布情况来决定索引的使用。 比如 10000 条记录,索引1上有1000个不同的值, 而索引2上有10 个不同的值(也叫势),则MYSQL当然会选择索引1,这样结果集会是 10000/1000 = 10个,而不会去选用 索引2。
      

  2.   

    mysql中,一个表在一个SQL每引用一次,每一次引用只能用一个索引的另外索引的使用,优化器会根据表数据的分布情况来自己找一条“自认为”最佳路径的贴出下面信息以便分析:
    show index from a; 
    show index from b;
    show table status like 'a';
    show table status like 'b';