【发布时间】:2018-07-09 23:41:13
【问题描述】:
我在 Access 中有三个表:
employees
----------------------------------
id (pk),name
times
----------------------
id (pk),employee_id,event_time
time_notes
----------------------
id (pk),time_id,note
我想从时间表中获取每个员工记录的记录,其中 event_time 紧接在某个时间之前。这样做很简单:
select employees.id, employees.name,
(select top 1 times.id from times where times.employee_id=employees.id and times.event_time<=#2018-01-30 14:21:48# ORDER BY times.event_time DESC) as time_id
from employees
不过,我还想知道 time_notes 表中是否有匹配的记录:
select employees.id, employees.name,
(select top 1 time_notes.id from time_notes where time_notes.time_id=(select top 1 times.id from times where times.employee_id=employees.id and times.event_time<=#2018-01-30 14:21:48# ORDER BY times.event_time DESC)) as time_note_present,
(select top 1 times.id from times where times.employee_id=employees.id and times.event_time<=#2018-01-30 14:21:48# ORDER BY times.event_time DESC) as last_time_id
from employees
这确实有效,但速度太慢了。如果员工表中有 100 条记录,我们会说 10 秒或更长时间。这个问题是 Access 特有的,因为我不能像在 MySQL 或 SQL Server 中那样使用其他子查询的 last_time_id 结果。
我正在寻找有关如何加快速度的提示。要么是不同的查询,要么是索引。东西。
【问题讨论】:
-
这个问题可能更适合Code Review
-
谢谢。我也只是把它贴在那里还是可以移动?