我尝试查询我们的登录日志,以确定自2017年7月1日起多个用户在30秒内登录到同一IP地址的情况。我在自我加入时使用了datediff但是给出了登录表有大约220万条记录,查询性能很糟糕。任何人都可以建议比下面的SQL更有效的查询。
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