Reputation: 1358
I have a MySQL select query with a large number of left joins, but only one of the joins is causing serious performance problems. This troublesome table has 2 BLOB fields in it, and when I left join on its primary key, the query takes over 20 times as long to run. Since this is the only table causing the problem, I assume that the BLOBs have something to do with it (even though they are not being selected or joined upon). Note that the number of rows in this table is not especially large relative to the other joined tables.
How can I speed up this query?
EDIT - here is the query (the problematic table is "submissions"):
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
Here is the EXPLAIN result for the "submissions" table:
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
Upvotes: 2
Views: 889
Reputation: 1955
Have you tried creating indexes on the non-PK fields that you're joining on, like submissions.action_id
and reward_events.action_id
? That should result in decreases in query time proportional to the size of the tables that you're joining on.
Upvotes: 1