1

如何优化此查询以获得相同的结果,而无需花费很长时间?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'
    )
4

1 回答 1

0

这是您查询的另一个版本

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);

如果EmployeeId是主键,employees那么它已经被索引,如果不是并且没有被索引,你可能还需要为此添加索引

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..并查看优化器如何使用索引以及优化器可能扫描以检索记录的行数

于 2015-05-21T06:36:03.557 回答