百万级就得几分钟,这子查询怎优化?
会稽梁山伯 2020-01-02 01:40:30 select
(
select count(tli.overspeed)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.overspeed > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as overspeed,
(
select count(tli.hurriedlySpeedUp)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.hurriedlySpeedUp > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as hurriedlySpeedUp,
(
select count(tli.hurriedlySpeedDown)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.hurriedlySpeedDown > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as hurriedlySpeedDown,
(
select count(tli.fatigue)
from
TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.fatigue > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as fatigue,
(
select count(tli.leftLight)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.leftLight > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as leftLight,
(
select count(tli.rightLight)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.rightLight > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as rightLight,
(select count(tli.farLight)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.farLight > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as farLight,
(select count(tli.nearLight)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.nearLight > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as nearLight,
(select count(tli.brake)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.brake > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as brake,
(case when(
select sum(tli.timeInterval)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
)> 0 then (
select sum(tli.timeInterval)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) else 0 end) as totalTime,
(case when(
select sum(tli.timeInterval)
from TerminalLocationInfo tli
where HOUR(tli.actionTime) < 5
and tli.truckId = tk.id
and tli.driverId = d.id
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
)> 0 then (
select sum(tli.timeInterval)
from TerminalLocationInfo tli
where HOUR(tli.actionTime) < 5
and tli.truckId = tk.id
and tli.driverId = d.id
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) else 0 end) as totalNightTime,
(
select count(tli.night)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and HOUR(tli.actionTime) < 5
and tli.driverId = d.id
and tli.night > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as night,
(
select count(tli.acc)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and tli.acc > 0
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as acc,
(
select count(tli.id)
from TerminalLocationInfo tli
where tli.truckId = tk.id
and tli.driverId = d.id
and str_to_date(tli.actionTime,'%Y-%m-%d') = str_to_date(TLI.actionTime,'%Y-%m-%d')
) as totalCount
from
Truck tk
left join TerminalLocationInfo TLI on TLI.truckId = tk.id
left join Company C on C.id = tk.companyId
left join Driver d on d.id = TLI.driverId
where
TLI.actionTime >= "2018-01-01 00:00:00"
and
TLI.actionTime <= "2018-01-01 23:59:59"
and
TLI.actionTime >= "2019-01-01"
group by tk.id,d.id
order by TLI.actionTime