|_. id |_. select_type |_. table |_. type |_. possible_keys |_. key |_. key_len |_. ref |_. rows |_. Extra |
| 1 | PRIMARY | tc_pos | ref | position_deviceid_dateadd | position_deviceid_dateadd | 4 | const | 126758 | Using index condition |
| 3 | DEPENDENT SUBQUERY | next | ref | position_deviceid_dateadd | position_deviceid_dateadd | 4 | const | 126758 | Using index condition; Using where |
select sum(distance) from (select
ST_Distance_Sphere(
point(points.longtitude, points.latitude),
point(
lead(longtitude) over( order by points.date),
lead(latitude) over( order by points.date)
)
) as distance
from points
where date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc) as t;select sum(distance) from (select
ST_Distance_Sphere(
point(points.longtitude, points.latitude),
(select
point(next.longtitude, next.latitude)
from
points as next
where
next.date > points.date
and
next.date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc limit 1)) as distance
from points
where date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc) as t;