开发者

need advice on speeding up query

This query takes about 9 seconds and returns 2 records.

SELECT

s.description, s.improvement,
s.first_name, s.last_name, s.finding, s.action, s.share, 
s.learned, s.timestamp, d.title as department_title, 
group_concat(DISTINCT g.title SEPARATOR " | ") as strategic_goals,
group_concat(DISTINCT m.statement SEPARATOR " | ") as mission_references,
group_concat(DISTINCT meas.statement SEPARATOR " | ") as measure_statement,
group_concat(DISTINCT o.statement SEPARATOR " | ") as outcome_statement,
group_concat(DISTINCT i.title SEPARATOR " | ") as ilo_title,
group_concat(DISTINCT cv.title SEPARATOR " | ") as core_value_title,
y1.year as current_year_title, y2.year as previous_year_title,
u.file_name as file_name

FROM summary s

LEFT JOIN year y1 ON s.current_year_id = y1.id
INNER JOIN year y2 ON s.previous_year_id = y2.id

INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id
INNER JOIN goal g ON sge.goal_id = g.id

INNER JOIN outcome o ON s.id = o.summary_id
LEFT JOIN measure meas ON o.id = meas.outcome_id

INNER JOIN department d ON s.department_id = d.id
LEFT JOIN uploads u ON s.id = u.summary_id 

INNER JOIN mission_entries me ON s.id = me.summary_id
LEFT JOIN mission m ON me.mission_id = m.id

LEFT JOIN ilo_entries ie ON s.id = ie.summary_id
LEFT JOIN ilo i ON ie.ilo_id = i.id

INNER JOIN core_value_entries cve ON s.id = cve.summary_id
INNER JOIN core_value cv ON cve.core_value_id = cv.id

INNER JOIN executive_of_department eod ON s.department_id = eod.department_id

WHERE eod.executive_id = 3

GROUP BY s.id

I added the rest of the prima开发者_运维技巧ry keys. Query went from 9 seconds to 2 seconds.

Then I set fields that refer to other table's primary keys, as index fields. The query went from 2 seconds to 20 seconds, did I assign too many indexes?

What are some ways I could speed this up?


1. After setting indexes, etc. you can speed up a little by limiting the JOIN conditions. For example, instead of

LEFT JOIN uploads u ON s.id = u.summary_i

do

LEFT JOIN uploads u ON (s.id = u.summary_i AND s.id = 3)

The WHERE clause is evaluated after all tables have been joined. Preciese your JOIN condition when you can.


2. Did you try constructing a view from your select and perform a SELECT * FROM myView WHERE myView.id = 3 ?

UPDATE : read this blog comment.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜