I have to run 2 simple queries on a very large table consisting approximately 20 million rows.
Table columns are id|user_id|earned_amount|created_at
Query 1:
select user_id, sum(earned_amount) as total_earning
from earning_history
where user_id=XX;
Query 2:
SELECT date(created_at) date, sum(earned_amount) as earning, count(id) as total_entry
FROM `earning_history`
where user_id=xx
GROUP by date
I have to run the second query more than the first one. So Im thinking about indexing the user_id
and created_at
column;
Without indexing, it takes about 6-7 seconds to perform the second query. My question is,
Should I index only user_id column? Or should I index both
user_id
andcreated_at
column?Should I use multiple column index like =>
ALTER TABLE earning_history ADD INDEX (user_id, created_at);
?