I run a simple MySQL query to find the total time spent on the game:
SELECT userId, SUM(time) AS totalGameTime FROM game_attempts WHERE userId = 19599
EXPLAIN shows the following:
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE game_attempts ref userId_gameId userId_gameId 4 const 26880
PROFILER shows that most of the time spent on "Sending data":
Sending data 1.786524
Why does such a simple request take so long? Where to look for a bottleneck?
UPDATE. Time is INT (11), no transformations.
UPDATE A possible solution is to introduce an index (userId, time), which solves the problem by moving part of the data into the index tree. But this does not solve the more serious problem of why summing as many as 30,000 takes so long.
This question has no easy answer. The indices are correct; no lengthy conversions are required. It's just tuning a DB tuner - why does it take so long to put these 30,000 records and extract data?
It is important to say that the table uses the InnoDB engine and contains about 2 million records.
source share