查询花费了太多时间。需要帮助

时间:2015-04-12 14:11:46

标签: mysql join

                SELECT o.ofc_id, o.ofc_joiningrank,o.ofc_gender, ofc_mobile,o.ofc_name,o.ofc_dateofbirth,o.ofc_officercode,
                    o.ofc_joiningrank, (

                SELECT CONCAT(w.wng_name, ', ', d.dst_name) FROM tbl_posts p
                    LEFT JOIN tbl_district_unit d ON (p.pos_dst_id = d.dst_id)
                    LEFT JOIN tbl_wings w         ON (p.pos_wng_id = w.wng_id)
                    LEFT JOIN tbl_cadre c         ON (p.pos_cdr_id = c.cdr_id)
                    LEFT JOIN tbl_grades g        ON (p.pos_grd_id = g.grd_id)
                    WHERE p.pos_id = a.apt_pos_id) as posting, 

            r.rnk_abbreviation,r.rnk_grade,r.rnk_name,
                    g.grd_name, t.ptp_name,d.dst_name AS post_district, w.wng_name, c.cdr_name, 
                    p.pos_sortid, p.pos_status, p.pos_id,p.pos_parent_id,
            a.apt_id, a.apt_name, a.apt_startdate,a.apt_ptp_id,a.apt_pos_id, a.apt_enddate ,p.post_name,
            tbr.rgn_name,edu.edu_degree,
            (select dst_name from tbl_district_unit where dst_id = o.ofc_district) as homedistrict,
                (select grd_name from tbl_grades where grd_id = p.pos_grd_id) as postgrade,
            (select rnk_name from tbl_ranks where o.ofc_joiningrank = rnk_id) as joiningrank
            FROM tbl_posts p
            LEFT JOIN tbl_appointments              a  ON (a.apt_pos_id = p.pos_id)
            LEFT JOIN tbl_officer               o  ON (a.apt_ofc_id = o.ofc_id)
            LEFT JOIN tbl_district_unit         d  ON (p.pos_dst_id = d.dst_id)
            LEFT JOIN tbl_regions               tbr ON (tbr.rgn_id = d.dst_rgn_id)
            LEFT JOIN tbl_wings                 w  ON (p.pos_wng_id = w.wng_id)
            LEFT JOIN tbl_cadre                 c  ON (p.pos_cdr_id = c.cdr_id)
            LEFT JOIN tbl_ranks                 r  ON (a.apt_rnk_id = r.rnk_id)
            LEFT JOIN tbl_education                        edu ON (edu.edu_ofc_id = o.ofc_id)
            LEFT JOIN tbl_grades                g  ON (a.apt_grd_id = g.grd_id)
            LEFT JOIN tbl_postingtype           t  ON (a.apt_ptp_id = t.ptp_id)
            WHERE tbr.rgn_id = ('" . $regionID . "') 
            order by p.pos_sortid ASC");

这个查询占用了太多时间,因为我在大约20个区域的循环中执行此查询。因此,对于每个区域执行此查询。索引已经放置。如何优化?任何解决方案??

1 个答案:

答案 0 :(得分:0)

请从查询中删除多余的内容。这将有助于减少查询执行时间: 这是更新的查询。如果有帮助,请检查并告诉我

取值@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NI

SELECT o.ofc_id, o.ofc_joiningrank,o.ofc_gender,ofc_mobile,o.ofc_name,o.ofc_dateofbirth,o.ofc_officercode, r.rnk_abbreviation,r.rnk_grade,r.rnk_name, g.grd_name, t.ptp_name, d.dst_name AS post_district, w.wng_name, c.cdr_name, 
   p.pos_sortid, p.pos_status, p.pos_id,p.pos_parent_id,a.apt_id, a.apt_name, a.apt_startdate,a.apt_ptp_id,a.apt_pos_id, a.apt_enddate ,p.post_name, tbr.rgn_name, edu.edu_degree,
   (select dst_name from tbl_district_unit where dst_id = o.ofc_district) as homedistrict,
   (select grd_name from tbl_grades where grd_id = p.pos_grd_id) as postgrade,
   (select rnk_name from tbl_ranks where o.ofc_joiningrank = rnk_id) as joiningrank
        FROM tbl_posts p
        LEFT JOIN tbl_appointments              a  ON (a.apt_pos_id = p.pos_id)
        LEFT JOIN tbl_officer               o  ON (a.apt_ofc_id = o.ofc_id)
        LEFT JOIN tbl_district_unit         d  ON (p.pos_dst_id = d.dst_id)
        LEFT JOIN tbl_regions               tbr ON (tbr.rgn_id = d.dst_rgn_id)
        LEFT JOIN tbl_wings                 w  ON (p.pos_wng_id = w.wng_id)
        LEFT JOIN tbl_cadre                 c  ON (p.pos_cdr_id = c.cdr_id)
        LEFT JOIN tbl_ranks                 r  ON (a.apt_rnk_id = r.rnk_id)
        LEFT JOIN tbl_education                        edu ON (edu.edu_ofc_id = o.ofc_id)
        LEFT JOIN tbl_grades                g  ON (a.apt_grd_id = g.grd_id)
        LEFT JOIN tbl_postingtype           t  ON (a.apt_ptp_id = t.ptp_id)
        WHERE tbr.rgn_id = ('" . $regionID . "') 
        order by p.pos_sortid ASC");

取值@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NI