我有这个SQL Server查询
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
我有超过1,000,000行,它的表现就像垃圾一样。 怎么做 - 请帮忙。
非常感谢
答案 0 :(得分:3)
难怪它运行缓慢:你有5个相关的子查询,其中2个是不必要的,3个可以重写。试试这个。
另外,你需要其中一个索引,不能确切地说明哪个
(SiteID, Timestamp, Url)
包含(IP, SessionGuid)
的INCLUDE
(SiteID, Timestamp)
在(IP, SessionGuid, Url)
(SiteID) with INCLUDE
(IP, Url, SessionGuid, Timestamp)
取决于是否使用第一个拖曳RangeMatch
和URLmatch
。我的猜测是需要2号或3号。这对索引大小很重要。
当未使用*
时,Count将忽略NULL。
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
答案 1 :(得分:0)
IP
SiteID
,Timestamp
,url
和SessionGuid
是否已有索引?