NOT IN 子查询执行时间太长

NOT IN subquery takes too long to execute

如何优化此查询以获得相同的结果,而不用花费很长时间? NOT IN 子查询需要很长时间。

SELECT DISTINCT EmployeeId FROM employees
    WHERE 
    status = 'Active' 
    && BranchId = '2' 
    && NOT EXISTS (
      SELECT * FROM attendance
      WHERE
      employees.EmployeeId = attendance.EmployeeId 
      && attendance.AttendanceDate = '2015-01-20'
    )
  )

SELECT EmployeeId FROM employees 
    WHERE 
    status = 'Active' 
    && BranchId = '2' 
    && NOT IN (
      SELECT EmployeeId FROM attendance WHERE AttendanceDate='2015-01-20'
    )

这是您查询的另一个版本

select
distinct e.EmployeeId FROM employees e
left join attendance a on e.EmployeeId = a.EmployeeId and a.AttendanceDate = '2015-01-20'
where
e.status='Active' 
and e.BranchId= '2' 
and a.EmployeeId is null

您还需要在表上应用一些索引,如

alter table employees add index st_br_idx(status,BranchId);
alter table AttendanceDate add index AttendanceDate_idx(AttendanceDate);

如果 EmployeeId 是外键则不需要添加索引,否则如果 索引还不存在,您可能还需要以下内容

alter table AttendanceDate add index EmployeeId_idx(EmployeeId);

如果 EmployeeIdemployees 中的主键,那么它已经被编入索引,如果没有并且没有被编入索引,您可能还需要为此添加索引

alter table employees add index EmployeeId_idx(EmployeeId);

您也可以在有以上索引后查看您的原始查询

SELECT DISTINCT e.EmployeeId FROM employees e 
WHERE 
e.status='Active' 
and e.BranchId= '2' 
and NOT EXISTS (
  SELECT 1 FROM 
  attendance a WHERE e.EmployeeId = a.EmployeeId 
  and a.AttendanceDate='2015-01-20'
)

要分析查询,请使用 explain select.. 并查看优化器如何使用索引 以及优化器为检索记录可能扫描的行数