这个sql这样写可以吗  (在线高并发环境)
select  t.* from  Ty t,User u, Ae a  where  u.pId = 7  and t.uId =a.uId and t.aId=a.id and t.ud= u.id and (t.type =2 or t.Type =3)  order by t.id desc limit 20在某些工具分析这个sql写得不是很好 的 ,  请问该如何调整( t(id, aid , uid )           u(id ,aid)             a(id ,uid)  )User u 和 Ae a之间是1对多的关系 。   就是说一个user表不同的id 有不同的aid, 但是aid每条记录都有userid ,不同的ae 可以有相同的uid。  Ty是交易表, 他跟User u, Ae a 2个都是1对多的关系. 请问这个表该如何优化??

解决方案 »

  1.   

    explain select ..
    show index from ...贴出来看一下。
      

  2.   

    | id | select_type | table | type   | possible_keys                                                                    | key           | key_len | ref                      | rows | Extra                                                     |
    +----+-------------+-------+--------+----------------------------------------------------------------------------------+---------------+---------+--------------------------+------+-----------------------------------------------------------+
    |  1 | SIMPLE      | u     | ref    | PRIMARY,idx_partnerId                                                            | idx_pId | 5       | const                    |   14 | Using where; Using index; Using temporary; Using filesort |
    |  1 | SIMPLE      | t     | ref    | idx_TE_TypePId,idx_TE_uid,idx_TE_eId_tt_status,aId | idx_TE_uid | 4       | .u.id        |    1 | Using where                                               |
    |  1 | SIMPLE      | a     | eq_ref | id,idx_a_uid,id_2,id_3,id_4                                       | id            | 4       | .t.aId |    1 | Using where                                               |
    +----+-----------[size=10px]
    --+-------+--------+----------------------------------------------------------------------------------+---------------+---------+--------------------------+------+-----------------------------------------------------------+
    3 rows in set[/size]
      

  3.   


    show index from ...
      

  4.   

    思考这样的sql 没有什么好优化的
    走全表扫描实际走的索引是t.Type  这字段