Consider the following table:
|-----------------------------------------------------|
| raffle |
|----|---------|----------|-----|---------------------|
| id | shuffle | user_id | ... | notify_at |
|----|---------|----------|-----|---------------------|
| 1 | 4D6G8Z1 | 542 | ... | 2019-12-01 14:00:00 |
| 2 | 64G264D | 6 | ... | 2019-12-28 14:00:00 |
| 3 | 4IPF93D | 58 | ... | 2020-01-01 14:00:00 |
| 4 | D25LF03 | 58 | ... | 2020-01-14 14:00:00 |
| 5 | G04LDWE | 684 | ... | 2020-03-02 13:00:00 |
In this table, most requests are not done to the id
column, but to the user_id
and notify_at
, which is a 64-bit timestamp (no 2038 Bug):
SELECT *
FROM [raffle]
WHERE [user_id] = ?
AND [notify_at] = ?
The table grows by the minute, but that is not the problem, but rather, the records for the notify_at
in the current month are most accessed than the rest. In 10.000.000 records, an index of the user_id
and notify_at
sums 160MB, which only 1% of these are heavily accessed.
Is there a way to optimize an index (or any other strategy) to make retrieval of the records for the current month snappier (as in, "trying to use the index instead of sweeping the whole table for records)?
Update 1: I'm asking this way because the table holds many notifications. This would grow large over time, and the SQL query would only take those in the current month:
SELECT *
FROM raffle
WHERE user_id = 542
AND notify_at > '2020-01-01 00:00:00'
AND notify_at < '2020-01-31 23:59:59'
As you can see, the index would also grow larger.