优化select语句

时间:2015-08-07 10:38:53

标签: mysql

有人可以帮我优化以下查询吗?此查询执行全表扫描。我想避免全表扫描。这两个表都有startdate和enddate的索引

select sr.id, sr.serviceTypeId,  sr.petLocationId  
  from ServiceRule sr
  where   (sr.startDate<=1443312000 and (sr.endDate>=1434067200 or sr.endDate=0)) 
  or (sr.id in (select distinct serviceRuleId from ServiceException 
  where (startDate>=1434096000 and startDate<=1443340800) 
  or (startDate<1434096000 and (endDate between 1434096000 and 1443340800)) 
  or (startDate<1434096000 and endDate>1443340800) 
or ((1434096000 between startDate and endDate) and (1443340800 between startDate and endDate))))

enter image description here

尝试使用连接。执行时间低于1秒。

0 个答案:

没有答案