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 7999661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:34:03+00:00 2026-06-04T15:34:03+00:00

I have a query that is taking 48 seconds to execute as follows: SELECT

  • 0

I have a query that is taking 48 seconds to execute as follows:

SELECT count(DISTINCT tmd_logins.userID) as totalLoginsUniqueLast30Days 
FROM tmd_logins
join tmd_users on tmd_logins.userID = tmd_users.userID 
where tmd_users.isPatient = 1 AND loggedIn > '2011-03-25' 
and tmd_logins.userID in 
    (SELECT userID as accounts30Days FROM tmd_users
    where isPatient = 1 AND created > '2012-04-29' AND computerID is null)

When I remove the DISTINCT keyword it takes less than 1 second, so it seems the bottle neck lies within that.

The database adds an entry to the tmd_logins table every time a user logs into the system. I am trying to get a total count of all users that are patients that have been created and logged in within a given period of time, such as in the last 30 days.

I have tried removing the DISTINCT keyword and adding group by tmd_logins.userID to the statement but the performance issue remains.

Table tmd_logins has about 300,000 records, tmd_users has about 40,000

Is there a better way of doing this?

  • 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-06-04T15:34:04+00:00Added an answer on June 4, 2026 at 3:34 pm

    The problem that you have is the execution plan. My guess is that the “in” clause might be confusing it. You might try:

    SELECT count(DISTINCT tmd_logins.userID) as totalLoginsUniqueLast30Days 
    FROM tmd_logins join
         tmd_users
         on tmd_logins.userID = tmd_users.userID join
         (SELECT distinct userID as accounts30Days
          FROM tmd_users
          where isPatient = 1 AND
                created > '2012-04-29' AND
                computerID is null
         ) t
         on tmd_logins.userID = t.accounts30Days
    where tmd_users.isPatient = 1 AND
          loggedIn > '2011-03-25' 
    

    That might or might not work. However, I’m wondering about the structure of the query itself. It would seem that UserID should be distinct in a table called tmd_users. If so, then you can wrap all your conditions into one:

    SELECT count(DISTINCT tmd_logins.userID) as totalLoginsUniqueLast30Days 
    FROM tmd_logins join
         tmd_users
         on tmd_logins.userID = tmd_users.userID 
    where tmd_users.isPatient = 1 AND
          loggedIn > '2011-03-25' and
          created > '2012-04-29' AND
          computerID is null
    

    If my guess is true, then this should definitely run faster.

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

Sidebar

Related Questions

I have a query that is taking way too long to execute (4 seconds)
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
We have a (large) SELECT query, that can take ~30 seconds to run. I
I have a query that I am executing in C# that is taking way
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a simple SQL query that when run from C# takes over 30
We have a query that's taking 3 hours to finish. This wasn't a problem
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a page that is taking 37 seconds to load. While it is
I have a query which is taking some serious time to execute on anything

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.