I'm attempting to query our login log to identify cases where multiple users have logged in to the same IP Address within 30 seconds since July 1, 2017. I'm using datediff on a self join but given the login table has approximately 2.2 million records the query performance is terrible. Can anyone suggest a more efficient query than the sql below.
SELECT TOP 1000 a.[id]
,A.[userId]
,AName.Firstname + ' ' + AName.LastName as name
,A.[username]
,A.[site]
,A.[loginTime]
,A.[ip]
,A.[agent]
,B.userid
,bname.Firstname + ' ' + bname.Lastname
,B.username
,B.ip
,B.loginTime
FROM LoginLog as A
Inner Join Loginlog As B on a.ip = B.ip and A.userId <> B.userId and DATEDIFF(SECOND, A.loginTime, B.loginTime) < 30 and DATEDIFF(SECOND, A.loginTime, B.loginTime) > 0
inner join Employees as AName on A.userId = AName.EmployeeNumber
outer join Employees as BName on B.userId = BName.EmployeeNumber
where a.userId in (*employee number list*)
and a.loginTime > '7/1/2017'
order by a.loginTime