现在有个测试表
一个表是user,大概20W数据,另一个表是user_oper,大概3W
user(id,name,sex,birthday),user_oper(id,uid,arm,oper)
现在要查询出user_oper表的记录,但要知道用户名的名字,user_oper表uid是索引我写的是这样的
select a.*,b.name from user_oper a,user b where a.uid = b.id;查询完,速度0.203sselect a.*,b.name from user_oper a left join user b on a.uid = b.id;这2个sql运行速率差不多
还有没有能改进查询速度快mysql查询优化

解决方案 »

  1.   

    1、这里是sqlserver专区
    2、你的两个查询逻辑是不一样的,数据也应该不一样的,其实没有可比性,第一个查询可以改写为:
    select a.*,b.name from user_oper a inner join user b on a.uid = b.id;
      

  2.   

    select a.*,b.name 
    from user_oper a
    INNER JOIN [user] b --id字段应该是主键
    ON a.[uid] = b.id
    where a.NAME = 'xxx' --#1.name字段建立索引--如果没有WHERE中的条件,两个语句的差别在于:第一个是INNER JOIN,第二个是LEFT JOIN
    --如果[user]表中的id数据在user_oper表中不存在,那么第一个效率会高,同时,他们查询出来的结果集是不一样的
      

  3.   

    单从sql上看没什么改进的了 已经是最好了