Is there a faster way to select the distinct count of users from a table? Perhaps using row_number, partitioning, or cross apply?
I just can't think of it right now.
Example:
Table UsageLog
UserId Date StoreNumber
Alice 200901 342
Alice 200902 333
Alice 200902 112
Bob 200901 112
Bob 200902 345
Charlie 200903 322
Here's my current query:
select count(distinct userID), date
from
UsageLog
where
date between 200901 and 200902
group by date
My actual table has millions of rows and all columns are actually integers.
Is there a faster way to get the list of users?
Edit:
I already have nonclustered indexes on all individual columns. For some reason, the execution plan shows that I am still doing a table scan. I guess I should create a clustered index on Date. I'll see how that works...