I have a medium size database (400,000 rows at the time) containing a Measurement table with the following schema:
CREATE TABLE `Measurements` (
`timestamp` timestamp,
`timetick` INTEGER,
`Sensor1` REAL,
`Sensor2` REAL,
PRIMARY KEY(timestamp));
As timestamp increases (timestamp increases are not constant there are gaps and delays but timestamps are guaranteed to be monotonic), normally timetick increases too, but there cases where it resets to a small but unpredictable value. I need to find all such rows. I have used the following query (inspired by Finding the difference in rows in query using SQLite):
select r0,r1,a,b,rd,d from
(select M0.rowid as r0,
M1.rowid as r1,
M0.timestamp as a,
M1.timestamp as b,
min(M1.timestamp)-M0.timestamp as rd,
M1.timetick-M0.timetick as d
from Measurements M0,Measurements M1
where M1.timestamp>M0.timestamp group by M0.timestamp
) where d<0;
This works but takes hours, while the same job in python finishes in 30 seconds. Yet it is a a very common task, scientists calculate derivatives all the time and financial professionals calculate price differences. There should be an efficient way to do it.
I will appreciate your help and comments.