有一条sql语句 select a.name ,b.name from tableA a,tableB b where a.id = b.id and (其他过滤条件)
其中a.id 为tableA主键 有唯一性索引 数据量才1千多
b.id 为日志表tableB 的外键 ,慢去条件的才8000多条记录
用如上语句查询比较慢(可以明显感觉有停顿)如果改成这样:select a.name ,b.name from tableA a,tableB b where b.id = a.id and .....
既把等值比较的顺序改变一下即可
速度可以接受不知道什么原因,等值比较的原理是什么?请不吝赐教

解决方案 »

  1.   

    你分析下2种情况下的执行计划, 应该就比较清楚了:explain SQL语句
      

  2.   

    用mysql客户端分析过了 
    第一种情况 里面有一个提示:Range checked for each record (index map: 0x2)
    而另外一个是useing where
      

  3.   

    贴出explain SQL语句完整结果才能便于分析如果再有show create table 表名;show indexes from 表名 的结果就更好分析了
      

  4.   

    explian select a.name ,b.name from tableA a,tableB b where a.id = b.id and 看一下结果是什么?理论上是不会出现你所说的这种情况的。
      

  5.   


    其实上面叫你贴出那些完整结果,你都没贴,别人很难帮你分析的从你上面extra抓的信息看来,你后面的查询条件应该还有范围查询之类的情况建议你贴完整吧,如这样:
    mysql> explain select * from tb_1 \G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: tb_1
             type: index
    possible_keys: NULL
              key: ix_tb_1b
          key_len: 68
              ref: NULL
             rows: 1
            Extra: Using index
    1 row in set (0.02 sec)mysql>有这样的结果才容易分析
      

  6.   

    我把explain贴出来 ,感觉不是很有用第一种方式 比较慢的方式:1 PRIMARY <derived2> ALL 299 Using temporary; Using filesort
    1 PRIMARY <derived4> ALL 33
    4 DERIVED t21 ALL 33
    2 DERIVED <derived3> ALL 8170
    2 DERIVED t02 ALL INDEX_JBPMWFID 1216 Range checked for each record (index map: 0x2)
    3 DERIVED t30 ALL 89465 Using where; Using temporary; Using filesort
    *************************************************************************************第二种方式 比较快的方式1 PRIMARY <derived2> ALL 299 Using temporary; Using filesort
    1 PRIMARY <derived4> ALL 33
    4 DERIVED t21 ALL 33
    2 DERIVED <derived3> ALL 8170
    2 DERIVED t02 ALL INDEX_JBPMWFID 1216 Using where
    3 DERIVED t30 ALL 89465 Using where; Using temporary; Using filesort
    以上就是用explain + sql语句 所有的输出了 
    请高手赐教
      

  7.   

    看了你九楼的结果,感觉不应该有差别啊。你是如何确定第二种比较快的?快多少?象下面一样操作,看一下时间是多少?mysql> select * from a;
    +------+---------------------+
    | id   | date                |
    +------+---------------------+
    |    1 | 2009-10-10 11:11:11 |
    |    2 | 2009-10-10 11:11:13 |
    |    3 | 2009-10-10 11:11:15 |
    |    4 | 2009-10-10 11:11:17 |
    +------+---------------------+
    4 rows in set (0.08 sec)mysql>
      

  8.   

     一个2s多 一个需要15s 差别很明显  因为是一个统计的sql 我没有吧具体的sql贴出来 
      

  9.   

    另外和数据页大小是不是有关系 虽然两个表满足条件的记录分别是1千多与8千多 但是每条记录元组的数据项很多,会不会数据页大小 导致了IO的次数差别?因为MySQL都是默认安装的 没有对设置做优化和调整
      

  10.   

    用exists 就完全没有延迟 不到1s就出来了