dqqy64515 2011-05-20 11:29
浏览 52
已采纳

优化复杂的mysql查询

The following query is taking 0.1313 seconds on phpmyadmin. Any way to optimize this to make things faster (say like to get it in 0.00XX seconds)? Index already added at columns that are doing the joinings.

SELECT m.id, m.civ, m.prenom, m.nom, m.sexe, m.depart, m.date_entree, m.date_sortie, m.login_userid, m.login_passwd, a.rank_id, r.rank_m, r.rank_f, d.user_id AS depID, c.nom AS cordo, z.rank
FROM `0_member` AS m
LEFT JOIN `0_area` AS a ON ( m.id = a.user_id
AND a.sec_id =2 )
LEFT JOIN `0_rank` AS r ON r.id = a.rank_id
LEFT JOIN `0_depart` AS d ON ( m.depart = d.depart
AND d.user_sec =2 )
LEFT JOIN `0_area` AS z ON ( d.user_id = z.user_id
AND z.sec_id =2 )
LEFT JOIN `0_member` AS c ON d.user_id = c.id
WHERE z.rank = 'mod'
ORDER BY nom
  • 写回答

2条回答 默认 最新

  • 红酒泡绿茶 2011-05-20 13:25
    关注

    Your query has a final "WHERE" clause on the value being FOUND in the "Z" alias table with a rank of 'mod', yet your query is all LEFT JOINs indicating you want all members regardless of a possible match on the right side table you are joining to.

    Additionally, you are joining downstream to the "z" table by depart and depart to a user ID, then re-joining directly to the '0_area' as A table directly on the user's ID which APPEARS it would be the same as found from the linking to the depart table to the 'z' table anyhow.

    That said, and your member joins to depart and then to area...

    My SUGGESTION (and I can rewrite the query as such) is to reverse the order of the query putting your Area table FIRST with an index on the "sec_id, rank" being available... I would have the key order based on whichever category had the smaller subset column first... so either SEC_ID, RANK or RANK, SEC_ID. Then doing simple JOIN (not LEFT JOIN) to the other tables... At a minimum from:

    SELECT STRAIGHT_JOIN
          m.id, 
          m.civ, 
          m.prenom, 
          m.nom, 
          m.sexe, 
          m.depart,  
          m.date_entree, 
          m.date_sortie, 
          m.login_userid, 
          m.login_passwd, 
          a.rank_id, 
          r.rank_m, 
          r.rank_f, 
          d.user_id AS depID, 
          c.nom AS cordo, 
          z.rank
       FROM 
          `0_area` AS z
              JOIN `0_depart` AS d
                 on z.user_id = d.user_id
                 and d.user_sec = 2
                 JOIN `0_member` AS m
                    on d.depart = m.depart
                    AND z.user_id = m.id
              LEFT JOIN `0_rank` AS r
                 on z.rank_id = .rid
       WHERE
              z.sec_id = 2
          AND z.rank = 'mod'
       ORDER BY
          nom
    

    In your original query, you had a join from

    member
       Links to Area (on member's user ID just to ensure the "sec_id = 2")
    

    Since the new query is exclusively STARTING with the "area" table as "Z" alias, and THAT where clause is explicitly "sec_id = 2" value, you'll never need to backlink again...

    Area (only SECID = 2 and rank = mod)
      Links to Depart (on the User's ID)
          Links to Members by (on the depart ID)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用