I’m using the following SQL statement:
SELECT
SUM(t.Points) AS `Points`,
CONCAT(s.Firstname, " ", s.Surname) AS `Name`
FROM transactions t
INNER JOIN student s
ON t.Recipient_ID = s.Frog_ID
GROUP BY t.Recipient_ID
The query takes 21 seconds to run. Bizarrely, even if I LIMIT 0, 30 it still takes 20.7 seconds to run!
If I run an EXPLAIN on this statement, the results are as follows:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s ALL PRIMARY NULL NULL NULL 877 Using temporary; Using filesort
1 SIMPLE t ALL NULL NULL NULL NULL 135140 Using where
A transaction takes the following form:
Transaction_ID Datetime Giver_ID Recipient_ID Points Category_ID Reason
1 2011-09-07 36754 34401 5 6 Gave excellent feedback on the new student noteboo...
There are 130,000 rows in the transactions table.
A student takes the following form:
Frog_ID UPN Firstname Surname Intake_Year
101234 K929221234567 Madeup Student 2010
There are 835 rows in the student table.
Indexes


Is there a way I can make this query more efficient?
You both join using
Recepient_IDand group by it, yet it’s not indexed, so I assume this is the problem.Try to add
transactions.Recepient_IDas an index.