优化使用临时表和大量连接的MySQL查询

时间:2013-01-10 01:28:35

标签: mysql sql centos

我有以下查询:

SELECT id, title, organisation, logo, body, state, state_name, state_order, closing_date, end_date, state_abbr, GROUP_CONCAT(portfolio_id SEPARATOR ',') portfolios, organisation_type_id, featured, hide_apply_link, hide_company_title,description
                FROM(
                    SELECT * FROM(
                        SELECT 
                            j.id, 
                            j.title, 
                            CONCAT(o.name,IF(j.organisation_department = '','',CONCAT(' - ',j.organisation_department))) organisation,
                            o.id organisation_id, 
                            o.logo logo,
                            j.body body,
                            j.plain_body plain_body,
                            IF(d.is_state = 1, s.name,d.name) state, 
                            d.name state_name,
                            d.order state_order,
                            IF(j.hide_closing_date = 1, 'N/A', DATE_FORMAT(j.end_date,'%d %b')) closing_date,
                            j.end_date end_date, 
                            j.created newest, 
                            pjc.category_id category, 
                            d.state_id sid, 
                            s.name state_abbr, 
                            p.id postcode,
                            pj.port_id portfolio_id,
                            o.organisation_type_id,
                            j.featured,
                            j.hide_apply_link,
                            j.hide_company_title,
                            o.description
                        FROM
                            commstratjobs.portfolio_job_category pjc,
                            commstratjobs.organisation o,
                            commstratjobs.display_category d,
                            commstratjobs.section_job sj,
                            commstratjobs.state s,
                            commstratjobs.portfolio_job pj,
                            commstratjobs.job j
                        LEFT JOIN  commstratjobs.postcode p ON p.id = j.postcode_id
                        WHERE pjc.job_id = j.id AND  j.id = sj.job_id AND
                                    sj.section_id = d.id AND
                                    j.organisation_id = o.id AND
                                    s.id = d.state_id AND
                                    pj.job_id = j.id AND
                                    j.published = 1 AND
                                    DATE(j.start_date) <= CURRENT_DATE AND
                                    DATE(j.end_date) >= CURRENT_DATE


                                    ) all_jobs  WHERE state_abbr = 'ACT' GROUP BY id, portfolio_id) all_jobs_grp GROUP BY id ORDER BY featured DESC, end_date ASC;

我为每个id设置索引并加入id

问题在于我上述每个查询的时间都超过了10秒。

有没有办法优化以上?

以下是EXPLAIN声明的输出:

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: <derived2>
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 21
        Extra: Using temporary; Using filesort
*************************** 2. row ***************************
           id: 2
  select_type: DERIVED
        table: <derived3>
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 2700
        Extra: Using where; Using temporary; Using filesort
*************************** 3. row ***************************
           id: 3
  select_type: DERIVED
        table: pjc
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 24
          ref: NULL
         rows: 181535
        Extra: Using index
*************************** 4. row ***************************
           id: 3
  select_type: DERIVED
        table: j
         type: eq_ref
possible_keys: PRIMARY,organisation_id
          key: PRIMARY
      key_len: 8
          ref: commstratjobs.pjc.job_id
         rows: 1
        Extra: Using where
*************************** 5. row ***************************
           id: 3
  select_type: DERIVED
        table: o
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: commstratjobs.j.organisation_id
         rows: 1
        Extra:
*************************** 6. row ***************************
           id: 3
  select_type: DERIVED
        table: p
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: commstratjobs.j.postcode_id
         rows: 1
        Extra: Using index
*************************** 7. row ***************************
           id: 3
  select_type: DERIVED
        table: pj
         type: ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: commstratjobs.j.id
         rows: 1
        Extra: Using where; Using index
*************************** 8. row ***************************
           id: 3
  select_type: DERIVED
        table: d
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 10
        Extra: Using join buffer
*************************** 9. row ***************************
           id: 3
  select_type: DERIVED
        table: s
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 1
          ref: commstratjobs.d.state_id
         rows: 1
        Extra:
*************************** 10. row ***************************
           id: 3
  select_type: DERIVED
        table: sj
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 9
          ref: commstratjobs.pjc.job_id,commstratjobs.d.id
         rows: 1
        Extra: Using index
10 rows in set (1.05 sec)

0 个答案:

没有答案
相关问题