问题
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)
clause on one column given the condition on other column, like this:
SELECT COUNT(DISTINCT time)
FROM TABLE_NAME
WHERE time >= '2020-11-14 23:08:05.553752' AND num = 11112222222
This runs for around 20 milliseconds which is also very fast, but I was wondering is there a way to make it even faster (maybe similar to the execution time of the first query - less than 5 milliseconds) ?
来源:https://stackoverflow.com/questions/65005378/speeding-up-countdistinct-of-one-column-based-on-condition-on-other-column-in