Reputation: 8640
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')
Upvotes: 0
Views: 99
Reputation:
Try adding an index on user_id, reached
, then try the query:
SELECT user_id, reached, count(*)
FROM goals
GROUP BY user_id, reached
Upvotes: 2