I have a MySQL table of 10 million rows and 3 columns, in following format:
id time num
ca65e871-d758-437e-b76f-175234760e7b 2020-11-14 23:08:05.553770 11112222222
...
For running the first query below, I indexed the table on (num, time)
and it works very fast (<5 milliseconds on 10 million rows table):
SELECT COUNT(*)
FROM TABLE_NAME
WHERE time >= '2020-11-14 23:08:05.553752' AND num = 11112222222
However I also need to execute count(distinct)
on the same table with between
clause, something like this:
SELECT COUNT(DISTINCT num)
FROM TABLE_NAME
WHERE time >= '2020-11-14 23:08:05.553752'
AND num BETWEEN (11112222222 - 30)
AND (11112222222 + 30)
This turns out to be significantly slower, around 200 milliseconds. Is there a way to speed the execution time of the second query on the same table?