I have a series of related tables and I'm trying to use a LEFT JOIN to pull it all together into a report.
Here's my query:
SELECT Aircraft.name as aircraft_name, Questions.question as question, Responses.answer as answer
FROM Responses
LEFT JOIN Events
ON Responses.category1 = Events.id
LEFT JOIN Classes
ON Events.class_id = Classes.id
LEFT JOIN Aircraft
ON Classes.aircraft_id = Aircraft.id
LEFT JOIN Questions
ON Responses.question_id = Questions.id
WHERE Aircraft.Type = 'Fighter'
AND Questions.type = 'Measure'
ORDER BY Questions.sortorder
LIMIT 0,50
Responses is a 500,000 record table. The other tables are relatively small (1-500 records). I've confirmed that all of the table's keys are indexed as either unique (primary keys) or normal (foreign keys). An EXPLAIN query returns this:
Responses ALL 523039 Using temporary; Using filesort
Events eq_ref PRIMARY,id PRIMARY 4 Responses.category1 1
Classes eq_ref PRIMARY,id PRIMARY 4 Events.class_id 1
Aircraft eq_ref PRIMARY,id PRIMARY 4 Classes.aircraft_id 1 where used
Questions eq_ref PRIMARY,id PRIMARY 4 Responses.question_id 1 where used
Which, I think, means that the query is scanning all 500,000 records in the Responses table against the indexes in the related tables. I've done similar joins in the past, but never against sets of data this large. I would have expected my WHERE clause to have limited the number of responses significantly, but as a precaution, I added the LIMIT statement to ensure that the bottleneck was actually in the query analysis. Am I doing something wrong, or is this type of join just impossible in this situation?
I really appreciate any help or direction anyone can provide. I'm stuck.