重写一次:
1、select a.*,b.name from table1 a,table2 b where a.xxx=xx and a.dm=b.dm order by a.id desc limit 0,10
2、select * from table1 where xxx=xx order by id desc limit 0,10
    各位不知看清楚了没有,类似这样的两句话在php中执行时间差别很大(数据库为mysql),各位有什么好办法没有?
    说明:table1是一个大的表,table2是一个字典表,通过一个dm字段联系起来。我只想在table1中存dm,不想存name等相关属性。但是想通过一个sql语句将table2中的相关属性也带出来。
    谢谢!分不够可以加。

解决方案 »

  1.   

    可以适当给where后面的字段加索引
      

  2.   

    你看看我的一个做法,结合我自己的也许会讲得清楚一些:
    比如现有一个数据库表用户考试表(usertest)该表采用如下结构:
    id,userid,testdate,testscroes<---userid 为用户表(usertable)中的用户的id,这样可能会查某个成绩时要知道username(用户名)就会:
    select * form  usertable where id in(select * from usertest where testscroes=98)

    这样可能在小数据量时速度不及直接存在一个表中快,但是当用户的数据量超过几百万时,它就快得多了,因为数据冗余小
    我建议采用这种方法,不知道说清楚了没有
      

  3.   

    1、对dm进行索引
    2、查询使用左连接。
       select a.*, b.m from table1 a
       left join table2 b on a.dm=b.dm
       where a.xxx=xx
       order by a.id
       limit 0,10