Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:21:53+00:00 2026-05-12T05:21:53+00:00

Is there a faster way to select the distinct count of users from a

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:21:54+00:00Added an answer on May 12, 2026 at 5:21 am

    SELECT DISTINCT() is the way to go. The problem is that you are hitting the date index tipping point, so your plan goes for the clustered index scan instead. See the link for Kimberley L. Tripp article what a ‘tipping point’ is.

    You need a covering index:

    CREATE INDEX idx_UsageLog_date_user_id ON UsageLog(date) INCLUDE (userID);
    

    Clustered index will also work, but has other side effects as well. If the clustered index on date is OK with the rest of your data access patterns, then is better than the covering index I propose.

    Update:

    The reverse order index you tried on (userID, date) also works, will range seek each userID. In fact is better than the (date, userID) or (date) INCLUDE (userID) because it returns the userIDs pre-sorted so the DISTINCT does not introduce the additional sort.

    Still I recommend going over the link I posted to understand why ‘index on each individual columns’ was not helping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to stream video with opencv faster? i'm using Mat img;
My question, is there a faster way to the following query? I'm using ORACLE
I currently use: BufferedReader input = new BufferedReader(new FileReader(filename)); Is there a faster way?
I wondering if there is a faster (GPU time) way to draw a full-screen
Is there any way to replace titles faster then str_replace function? I've got a
i am using this query $query = SELECT SQL_CACHE * FROM `like` WHERE `id`
Is there a way to pass a value to a derived table query? In
Is there a faster way to perform this most simple query in terms of
I have a <select>. Using JavaScript, I need to get a specific <option> from
Given a directed, connected graph with only positive edge weights, are there faster algorithms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.