我找到了info on how to optimize MySQL queries,但大多数提示似乎建议避免MySQL不是为了构建(例如,计算,验证等)而我的查询是非常直接但是加入了很多桌子。
是否有一种方法可以加快许多INNER JOINS
的简单查询?我如何修复下面的查询?
SELECT t_one.id FROM table_one t_one
INNER JOIN entr_to_state st
INNER JOIN entr_to_country ct
INNER JOIN entr_to_domain dm
INNER JOIN entr_timing t
INNER JOIN entr_to_weather a2w
INNER JOIN entr_to_imp_num a2i
INNER JOIN entr_collection c
WHERE t_one.type='normal'
AND t_one.campaign_id = c.id
AND t_one.status='running'
AND c.status='running'
AND (c.opt_schedule = 'continuous' OR (c.opt_schedule = 'schedulebydate'
AND (c.start_date <= '2011-03-06 14:25:52' AND c.end_date >= '2011-03-06 14:25:52')))
AND t.entr_id = t_one.id AND ct.entr_id = t_one.id
AND st.entr_id = t_one.id AND a2w.entr_id = t_one.id
AND (t_one.targeted_gender = 'male' OR t_one.targeted_gender = 'both')
AND t_one.targeted_min_age <= 23.1 AND t_one.targeted_max_age > 23.1
AND (ct.abbreviation = 'US' OR ct.abbreviation = 'any')
AND (st.abbreviation = 'CO' OR st.abbreviation = 'any')
AND t.sun = 1 AND t.hour_14 = 1
AND (a2w.weather_category_id = 1 OR a2w.weather_category_id = 0)
AND t_one.targeted_min_temp <= 46
AND t_one.targeted_max_temp > 46 GROUP BY t_one.id
答案 0 :(得分:2)
索引所有相关字段,当然,我相信你已经
然后通过运行EXPLAIN SELECT...
考虑将它们拆分为单独的查询,即缩小您要查找的记录,然后对那些记录而不是所有记录执行连接
即。
SELECT c.*, ....
FROM (SELECT x, y, z .... ) AS c
答案 1 :(得分:2)
您需要EXPLAIN SELECT查询,检查查询的哪些部分未在索引中使用,然后尝试索引这些部分。如果可能,请将查询分解为更小的部分。
如果您真的无法以任何方式优化基础数据库或查询,则可以使用具有快速访问所需数据的平面表。然后,只需连接主查询,即可根据需要更新平面表。