Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
JoeS88
Starting Member
1 Post |
Posted - 2013-11-29 : 10:01:07
|
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
Aged Yak Warrior
550 Posts |
Posted - 2013-11-29 : 12:15:51
|
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. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-30 : 01:46:29
|
Couple of alternativesSELECT 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 s2WHERE Cnt > 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|