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

优化使用临时表和大量连接的MySQL查询,mysql,sql,centos,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,descripti

我有以下疑问:

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;

您是否尝试在内括号中为SELECT语句创建视图。我认为mysql会为每次运行重新编译语句。如果你有一个观点,它可能会一劳永逸地优化它。只要试着创建一个视图,看看它是否有区别。另一方面,您可以轻松地将state_abbr='ACT'移动到第一个括号内的SQL语句以及所有GROUP BY子句中,从而减少嵌套动态视图的数量。您需要向我们显示表和索引定义。诊断慢速查询需要完整的表和索引定义,而不仅仅是描述或解释。也许您的表定义不好。可能索引没有正确创建。也许你在你认为你有的专栏上没有索引。如果看不到表和索引定义,我们就说不出来。正如Andy Lester所提到的,我们需要看到表定义,也许还有索引定义。然而,最重要的部分是explain语句的输出。请你把它放在你的问题中好吗?注释:
DATE(j.start\u DATE)=当前的\u DATE
如果你想使用
start\u DATE
end\u DATE
上的任何索引,应该在不使用
DATE()
函数的情况下重写。注释2:根本不需要三级嵌套。可以重写内部
所有\u作业
派生表,而无需嵌套。如果另一个嵌套也可以删除,这也会有所帮助。
*************************** 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)