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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:47:27+00:00 2026-05-16T06:47:27+00:00

I have this SQL Server query SELECT count(distinct [IP]) as GlobalUniqueIPcount, –RangeUniqueIPcount (SELECT count(distinct

  • 0

I have this SQL Server query

SELECT count(distinct [IP]) as GlobalUniqueIPcount, 
    --RangeUniqueIPcount
    (SELECT count(distinct [IP]) FROM [tblSequence] WHERE SiteID = @siteID AND ([Timestamp] > DATEADD(dd, -@days, (LEFT(GETDATE(),12))))) as RangeUniqueIPcount,
    --RangeUrlUniqueIPcount
    (SELECT count(distinct [IP]) FROM [tblSequence] WHERE SiteID = @siteID AND ([Timestamp] > DATEADD(dd, -@days, (LEFT(GETDATE(),12)))) AND Url = @Url) as RangeUrlUniqueIPcount,
    --RangeUniquePageviews
    (SELECT count (distinct url + SessionGuid) FROM [tblSequence] WHERE SiteID = @siteID AND ([Timestamp] > DATEADD(dd, -@days, (LEFT(GETDATE(),12))))) as RangeUniquePageViews,
    --RangeUrlUniquePageviews
    (SELECT count (distinct url + SessionGuid) FROM [tblSequence] WHERE SiteID = @siteID AND ([Timestamp] > DATEADD(dd, -@days, (LEFT(GETDATE(),12)))) AND Url = @Url) as RangeUrlUniquePageViews,  
    --GlobalUniquePageViews
    (SELECT count (distinct url + SessionGuid) FROM [tblSequence] WHERE SiteID = @siteID) as GlobalUniquePageViews
FROM [tblSequence] WHERE SiteID = @siteID

I have more than 1,000,000 rows and it performs like crap.
What to do – please help.

Thanks a lot

  • 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-16T06:47:27+00:00Added an answer on May 16, 2026 at 6:47 am

    No wonder it runs slow: you have 5 correlated subqueries, 2 of which are unnecessary and 3 can be re-written. Try this.

    Also, you need an index on one of these, can’t say exactly which

    1. (SiteID, Timestamp, Url) with INCLUDE on (IP, SessionGuid)
      • (SiteID, Timestamp) with INCLUDE on (IP, SessionGuid, Url)
      • (SiteID) with INCLUDE on (IP, Url, SessionGuid, Timestamp)

    It depends on whether the 1st tow would be used RangeMatch and URLmatch. My guess is number 2 or 3 will be needed. It matters for index size.

    Count will ignore NULLs when * is not used.

    SELECT
        count(distinct [IP]) as GlobalUniqueIPcount, 
    
        --RangeUniqueIPcount
        count (distinct CASE
                 WHEN RangeMatch = 1 
                 THEN IP ELSE NULL
               END ) AS RangeUniqueIPcount,
    
        --RangeURLUniqueIPcount
        count (distinct CASE
                 WHEN RangeMatch = 1  AND UrlMatch = 1
                 THEN IP ELSE NULL
               END ) AS RangeURLUniqueIPcount,
    
        --RangeUniquePageviews
        count (distinct CASE
                 WHEN RangeMatch = 1 
                 THEN url + SessionGuid ELSE NULL
               END ) RangeUniquePageViews,
    
        --RangeUrlUniquePageviews
        count (distinct CASE
                 WHEN RangeMatch = 1 AND UrlMatch = 1
                 THEN url + SessionGuid ELSE NULL
               END ) RangeUrlUniquePageViews,
    
        --GlobalUniquePageViews
        count (distinct url + SessionGuid) as GlobalUniquePageViews
    FROM
      (SELECT
        *, 
        CASE WHEN  Url = @Url THEN 1 ELSE 0 END AS UrlMatch,
        CASE WHEN [Timestamp] > DATEADD(dd, -@days, (LEFT(GETDATE(),12))) THEN 1 ELSE 0 END AS RangeMatch
      FROM
         [tblSequence]
      WHERE SiteID = @siteID
      ) foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to specify a full file name when doing… May 16, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer you can always set the port in your ssh config:… May 16, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer It seems to (sort of) work in Helios (3.6). It… May 16, 2026 at 2:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
I have RO access on a SQL View. This query below times out. How
I have a really weird issue with an SQL query I've been working with
In SQL Server 2005 I have a table cm_production that lists all the code
I have the following Query and i need the query to fetch data from
hi i tried a select query that contains an left outer join on SQL
I have a query that looks like this, and which is getting a percentage:
We have a legacy database which is a sql server db (2005, and 2008).
In my DB, I have a table that was created from an Excel sheet,
What is the correct way to write this JPA query? I am just guessing

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.