Tuesday 4 September 2018

How to optimize a MySQL query with SUM (condition)

I have the following MySQL query.

  SELECT user_id
         SUM(reached = 'Y') AS reached_count,
         SUM(reached = 'N') AS not_reached_count
    FROM goals
GROUP BY user_id

In table goals I have around 2 million entries. The query takes around 45 seconds to execute.
The heavy part seems to be SUM(reached = 'Y') which is taking quite long. I compared it with COUNT(*) which was indeed much faster, but was not distinguishing between Y and N.
EDIT: reached is of type ENUM('Y','N')

Try adding an index on user_id, reached, then try the query:
SELECT user_id, reached, count(*)
FROM goals
GROUP BY user_id, reached

0 comments:

Post a Comment