views:

44

answers:

2

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

A: 

Do IP SiteID, Timestamp, url, and SessionGuid already have indexes?

JohnB
Add some indexes as asked above, then have a look at execution plan - before and after this step.
lmsasu
Individual indexes will be useless. Needs to be covering or composite.
gbn
+3  A: 

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
gbn
Msg 156, Level 15, State 1, Line 23Incorrect syntax near the keyword 'distinct'.Msg 156, Level 15, State 1, Line 38Incorrect syntax near the keyword 'CASE'.
seo20
@seo20: Missing comma after URLMatch... fixed
gbn
Msg 156, Level 15, State 1, Line 34Incorrect syntax near the keyword 'CASE'.
seo20
@seo20: try again...
gbn