开发者

Optimizing a complex mysql query

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 joining开发者_Python百科s.

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


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)


Try moving this statements " a.sec_id =2 ", "d.user_sec =2 ", " z.sec_id =2 " from ON sections to WHERE section like you already did with "z.rank = 'mod'". Like this:

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
LEFT JOIN `0_rank` AS r ON r.id = a.rank_id
LEFT JOIN `0_depart` AS d ON m.depart = d.depart
LEFT JOIN `0_area` AS z ON d.user_id = z.user_id
LEFT JOIN `0_member` AS c ON d.user_id = c.id
WHERE z.rank = 'mod'
AND a.sec_id =2
AND d.user_sec =2
AND z.sec_id =2
ORDER BY nom
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜