SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get rows with a datetime within x seconds
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JoeS88
Starting Member

1 Posts

Posted - 11/29/2013 :  10:01:07  Show Profile  Reply with Quote
Hi,

We have a table that stores website statistics, It contains 12,294,806 rows. Some of the columns stored are Id (this is the primary key), DateTime and ClientIpAddress. I'm trying to write a query that finds all requests that occurred less than 5 seconds apart and came from the same IP address. So far I have this:


  SELECT ClientIpAddress
  FROM [Custom_Website_Data].[dbo].[Sta_WebStats] AS s1
  INNER JOIN [Custom_Website_Data].[dbo].[Sta_WebStats] AS s2
  ON s2.DateTime <= DateAdd(ss, 5, s1.DateTime)
  AND s2.DateTime >= s1.DateTime
  AND s1.ClientIpAddress = s2.ClientIpAddress


This works but it is very slow. Can anyone think of a faster way of doing it?

Thanks,

Joe

ScottPletcher
Constraint Violating Yak Guru

USA
369 Posts

Posted - 11/29/2013 :  12:15:51  Show Profile  Reply with Quote
You'll keep having performance issues with the table clustered as it currently is.

You need to drop the existing PK, and change the unique clustered index (and optionally PK) to ( DateTime, Id ) or even just ( DateTime ). If you really need to, you can add a nonclustered PK back on the Id alone.

That is, the critical thing is that the table be clustered by DateTime. Unless you need it for something else (very unlikely), you can remove the identity/id column, since it's not really critical to have a PK per se, and it somewhat hurts performance. But if you really want a PK, you can make the DateTime,Id combination the clustering pk or add a separate pk using id only.

Then the query above, and all your other queries using DateTime range(s), will run much faster.

Edited by - ScottPletcher on 11/29/2013 12:19:36
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/30/2013 :  01:46:29  Show Profile  Reply with Quote
Couple of alternatives

SELECT ClientIpAddress
  FROM [Custom_Website_Data].[dbo].[Sta_WebStats] AS s1
  WHERE EXISTS 
 (SELECT 1 FROM [Custom_Website_Data].[dbo].[Sta_WebStats]
  WHERE DateTime <= DateAdd(ss, 5, s1.DateTime)
  AND DateTime >= s1.DateTime
  AND s1.ClientIpAddress = ClientIpAddress
)


SELECT ClientIpAddress
  FROM [Custom_Website_Data].[dbo].[Sta_WebStats] AS s1
  CROSS APPLY (SELECT COUNT(1) AS Cnt
  FROM  [Custom_Website_Data].[dbo].[Sta_WebStats] 
  WHERE DateTime <= DateAdd(ss, 5, s1.DateTime)
  AND DateTime >= s1.DateTime
  AND s1.ClientIpAddress = ClientIpAddress
  )AS s2
WHERE Cnt > 0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000