selcect name from tableA where code in (select code from tableB where id='tableB_id')and sex='01';其中where语句后面的and有执行顺序吗,是先执行code in (select code from tableB where id='tableB_id') 还是先执行sex='01'?急,我想先执行sex='01'提高效率。我的数据库是mysql的,谢谢!

解决方案 »

  1.   

    tableA 是按code建立的,tableB没建立索引
      

  2.   

    在code和sex上建立组合索引会提高效率。
    单独在sex上建立索引值的变化很少(基本等于布尔型),没有效率。
    执行顺序应该是先执行子查询,然后子查询返回的结果中进行条件比较。
    如果每次比较都要做一次子查询效率太低,而且没有必要,这个在优化器中应该会处理的。
      

  3.   

    where 后的顺序 MYSQL会根据索引自行优化。你现在有索引,MYSQL会先使用 code in (select code from tableB where id='tableB_id')  无论这个条件在前在后。你可以用 explain selcect name from tableA where code in (select code from tableB where id='tableB_id')and sex='01';
    看一下执行计划。
      

  4.   

    where执行顺序是从后往前执行的。。