我有以下查询,它给了我正确的结果,但代价超过3秒。处理结果非常慢。是否有任何优化技术可以应用于此查询以改善性能/响应时间,例如< 1秒。
select dept, location , count(*) total from
(
select empName, dept, location from companyTable t1 where (position = 'manager' or position = 'supervisor' )and dateTime in
(
select max(dateTime) from companyTable t2 where t2.empName = t1.empName and location = 'Dallas' and dateTime like '2016:09:05%'
)
union all
select empName, dept, location from companyTable t10 where (position = 'manager' or position = 'supervisor')and dateTime in
(
select min(dateTime) from companyTable t2 where t2.empName = t10.empName and location <> 'Dallas' and dateTime like '2016:09:05%'
)
)
group by dept, empName having count(empName) > 1;
SCHEMA
CREATE TABLE companyTable
( dept varchar(32),location varchar(30),empName varchar(32), position varchar(30), dateTime time,
Primary key (dept, empName ,location ,position) );
EXPLAIN QUERY PLAN
2|0|0|SCAN TABLE companyTable AS t1
2|0|0|EXECUTE CORRELATED LIST SUBQUERY 3
3|0|0|SEARCH TABLE companyTable AS t2 USING INDEX sqlite_autoindex_companyTable_1 (dept=?)
4|0|0|SCAN TABLE companyTable AS t10
4|0|0|EXECUTE CORRELATED LIST SUBQUERY 5
5|0|0|SEARCH TABLE companyTable AS t2 USING INDEX sqlite_autoindex_companyTable_1 (dept=?)
1|0|0|COMPOUND SUBQUERIES 2 AND 4 (UNION ALL)
0|0|0|SCAN SUBQUERY 1
0|0|0|USE TEMP B-TREE FOR GROUP BY