SQL 查询以查找访问者以及日期时间

SQL query to find the visitor together with the date time

我的访客日志 table 有 id、visitor、department、vtime 字段。

    id  |   visitor |           Visittime    |     Department_id
    --------------------------------------------------------------
    1         1           2019-05-07 13:53:50           1 
    2         2           2019-05-07 13:56:54           1
    3         1           2019-05-07 14:54:10           3
    4         2           2019-05-08 13:54:49           1
    5         1           2019-05-08 13:58:15           1 
    6         2           2019-05-08 18:54:30           2
    7         1           2019-05-08 18:54:37           2

而且我已经有了下面的索引

CREATE INDEX Idx_VisitorLog_Visitor_VisitTime_Includes ON VisitorLog 
    (Visitor, VisitTime) INCLUDE (DepartmentId, ID)

从上面table从用户界面,访客1和访客2以及访问开始时间和结束时间传递了4个过滤器。

访客1和访客2在什么部门,VisitTime差5分钟,需要过滤

输出喊是

    id  |   visitor |           Visittime    |     Department_id
    --------------------------------------------------------------
    1         1           2019-05-07 13:53:50           1 
    2         2           2019-05-07 13:56:54           1
    4         2           2019-05-08 13:54:49           1
    5         1           2019-05-08 13:58:15           1 

为此,我使用了以下查询,

;with CTE1 AS(
Select id,visitor,Visittime,department_id from visitorlog where visitor=1
)
,CTE2 AS(
Select id,visitor,Visittime,department_id from visitorlog where visitor=2
)
select * from CTE2 V2
Inner join CTE1 V1 on V2.department_id=V1.department_id and DATEDIFF(minute,V2.Visittime,V1.Visittime)between -5 and 5**

以上查询需要太多时间才能给出响应。因为在我的table中,有将近2000万条记录可用

任何人都可以根据我的要求提出正确的方法。 提前致谢

这是一个完全修订的答案,基于您上面的附加信息。

查看上面的数据文件和您想要的结果后,这似乎是提供结果的最简洁的方式。首先,我们需要一个不同的索引:

create index idx_POC_visitorlog on visitorlog
    (visitor, Department_id, Visittime) include(id);

有了这个索引,我们可以将查询限制为只查询传入的两个ID。为了模拟这一点,我创建了变量来保存它们的值。本次查询returns你要找的数据

DECLARE @Visitor1 int = 1,
    @Visitor2 int = 2

;with t as (
    select Department_id,
        dateadd(minute, -5, visittime) as EarlyTime,
        dateadd(minute, 5, Visittime) as LateTime,
        id
    from visitorlog
    where visitor = @Visitor1
    ),
v as (
    select v.id, 
        t.id as tid
    from visitorlog v
    INNER JOIN t 
        ON v.visitor = @Visitor2
        AND v.Department_id = t.Department_id
        and v.Visittime BETWEEN t.EarlyTime and t.LateTime
    )
SELECT *
FROM visitorlog vl
WHERE ID IN (
    SELECT v.id
    FROM v
    UNION
    SELECT v.tid
    FROM v
    )
ORDER BY visittime;

如果您的 SQL 服务器版本支持 LAG and LEAD 功能,请尝试按如下方式重写查询:

with t as (
  select
    *,
    dateadd(minute, 5,
      lag(Visittime) over(partition by Department_id order by Visittime)) lag_visit_time,
    dateadd(minute, -5,
      lead(Visittime) over(partition by Department_id order by Visittime)) lead_visit_time
  from visitorlog
  where visitor in(1, 2)
)
select
  id, visitor, visittime, department_id
from t
where lag_visit_time >= Visittime or lead_visit_time <= Visittime;

这个索引叫做POC

结果:

+----+---------+----------------------+---------------+
| id | visitor |      visittime       | department_id |
+----+---------+----------------------+---------------+
|  1 |       1 | 2019-05-07T13:53:50Z |             1 |
|  2 |       2 | 2019-05-07T13:56:54Z |             1 |
|  4 |       2 | 2019-05-08T13:54:49Z |             1 |
|  5 |       1 | 2019-05-08T13:58:15Z |             1 |
|  6 |       2 | 2019-05-08T18:54:30Z |             2 |
|  7 |       1 | 2019-05-08T18:54:37Z |             2 |
+----+---------+----------------------+---------------+

Demo.