我有一个带有大量左连接的MySQL选择查询,但只有一个连接导致严重的性能问题。这个麻烦的表中有2个BLOB字段,当我在其主键上保持连接时,查询占用的时间超过20倍。由于这是唯一导致问题的表,我假设BLOB与它有关(即使它们没有被选中或加入)。请注意,此表中的行数相对于其他连接表而言并不是特别大。
如何加快此查询?
编辑 - 这是查询(有问题的表格是“提交”):
SELECT
actions.id,
actions.facebook_id,
actions.created_at,
actions.current_total_points,
actions.current_weekly_points,
submissions.id AS submission_id,
submissions.challenge_week_number AS submission_challenge_week_number,
submissions.challenge_number_in_week AS submission_challenge_number_in_week,
reward_events.id AS reward_event_id,
reward_events.reward_event_type_id,
reward_events.action_id,
reward_events.awarded_badge_type_id,
reward_events.for_week_number AS reward_event_for_week_number,
reward_events.challenge_number_in_week AS reward_event_challenge_number_in_week,
challenge_weeks.week_number
from actions
left join submissions ON submissions.action_id = actions.id
left join reward_events ON reward_events.action_id = actions.id
left join challenge_weeks ON challenge_weeks.start_date <= CAST(actions.created_at AS DATE) AND challenge_weeks.end_date >= CAST(actions.created_at AS DATE)
where actions.facebook_id = '12345678'
order by actions.id asc
以下是“提交”表格的EXPLAIN结果:
id bigint(11) unsigned NO PRI auto_increment
action_id bigint(11) NO
title varchar(255) YES
description varchar(255) YES
submission_type enum('alpha','beta','gamma') YES
filename varchar(255) YES
ip_address varchar(255) YES
community_release bit(1) YES
approved bit(1) YES
fullsize longblob YES
thumb longblob YES
modified_at timestamp YES CURRENT_TIMESTAMP
challenge_week_number tinyint(1) YES
challenge_number_in_week tinyint(1) YES
答案 0 :(得分:1)
您是否尝试在您加入的非PK字段上创建索引,例如submissions.action_id
和reward_events.action_id
?这应该会导致查询时间的减少与您加入的表的大小成比例。