Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

Starting Member

1 Posts

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

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?



Aged Yak Warrior

550 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

Very Important crosS Applying yaK Herder

52326 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
 (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
  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
Go to Top of Page
  Previous Topic Topic Next 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.08 seconds. Powered By: Snitz Forums 2000