I created view named HistoriaTras that means routes history.
CREATE VIEW historiatras AS
SELECT
DataRozpoczeciaTrasy.deviceid AS deviceid,
DataZakonczeniaTrasy.ts - DataRozpoczeciaTrasy.ts AS RoznicaCzasu,
DataRozpoczeciaTrasy.ts AS RozpoczecieTrasy, DataZakonczeniaTrasy.ts AS ZakonczenieTrasy
FROM DEVICE_DATA DataRozpoczeciaTrasy
JOIN DEVICE_DATA DataZakonczeniaTrasy ON DataRozpoczeciaTrasy.deviceid = DataZakonczeniaTrasy.deviceid AND mod(DataZakonczeniaTrasy.inputs,2)=0 AND DataZakonczeniaTrasy.eventid = 11 AND DataZakonczeniaTrasy.ts > DataRozpoczeciaTrasy.ts
WHERE mod(DataRozpoczeciaTrasy.inputs,2)=1
AND DataRozpoczeciaTrasy.eventid = 11
ts = timestamp
DataRozpoczeciaTrasy = date of start route
DataZakonczeniaTrasy = date of stop route
RoznicaCzasu = time between DataRozpoczeciaTrasy - DataZakonczeniaTrasy
If I make below SELECT everything works almost good but it take too much time ~ 27 seconds - and it do not contains all routes (because I make select from one day and for one device - 85758):
SELECT * FROM HistoriaTras Trasy
WHERE RoznicaCzasu = (SELECT MIN(Trasy1.RoznicaCzasu)
FROM HistoriaTras Trasy1
WHERE Trasy.RozpoczecieTrasy = Trasy1.RozpoczecieTrasy)
AND deviceid = 85758
AND RozpoczecieTrasy > '2017-11-05 00:00:00'
AND RozpoczecieTrasy < '2017-11-06 00:00:00'
If i delete:
AND deviceid = 85758 AND RozpoczecieTrasy > '2017-11-05 00:00:00' AND RozpoczecieTrasy < '2017-11-06 00:00:00'
It will take... too much time. I did not test it but it take too long time.
I have about 100 k rows in device_data table. Friends, please write me how can I optimize this select or view? I am using PostgreSQL 10.
I am pasting explain analyze of SELECT because analyze of VIEW is not allowed:
Nested Loop (cost=0.00..21281.66 rows=1 width=40) (actual time=3973.445..982716.756 rows=380 loops=1)
Join Filter: ((datazakonczeniatrasy.ts > datarozpoczeciatrasy.ts) AND (datarozpoczeciatrasy.deviceid = datazakonczeniatrasy.deviceid) AND ((datazakonczeniatrasy.ts - datarozpoczeciatrasy.ts) = (SubPlan 1)))
Rows Removed by Join Filter: 142880
-> Seq Scan on device_data datarozpoczeciatrasy (cost=0.00..5259.22 rows=1 width=16) (actual time=0.046..17.667 rows=380 loops=1)
Filter: ((eventid = '11'::numeric) AND (mod(inputs, '2'::numeric) = '1'::numeric))
Rows Removed by Filter: 97518
-> Seq Scan on device_data datazakonczeniatrasy (cost=0.00..5259.22 rows=1 width=16) (actual time=0.011..17.850 rows=377 loops=380)
Filter: ((eventid = '11'::numeric) AND (mod(inputs, '2'::numeric) = '0'::numeric))
Rows Removed by Filter: 97521
SubPlan 1
-> Aggregate (cost=10763.19..10763.20 rows=1 width=16) (actual time=34.834..34.834 rows=1 loops=28011)
-> Nested Loop (cost=0.00..10763.19 rows=1 width=16) (actual time=9.246..34.805 rows=112 loops=28011)
Join Filter: ((datazakonczeniatrasy_1.ts > datarozpoczeciatrasy_1.ts) AND (datarozpoczeciatrasy_1.deviceid = datazakonczeniatrasy_1.deviceid))
Rows Removed by Join Filter: 270
-> Seq Scan on device_data datarozpoczeciatrasy_1 (cost=0.00..5503.96 rows=1 width=16) (actual time=5.930..17.172 rows=1 loops=28011)
Filter: ((eventid = '11'::numeric) AND (datarozpoczeciatrasy.ts = ts) AND (mod(inputs, '2'::numeric) = '1'::numeric))
Rows Removed by Filter: 97897
-> Seq Scan on device_data datazakonczeniatrasy_1 (cost=0.00..5259.22 rows=1 width=16) (actual time=0.012..17.327 rows=377 loops=28407)
Filter: ((eventid = '11'::numeric) AND (mod(inputs, '2'::numeric) = '0'::numeric))
Rows Removed by Filter: 97521
Planning time: 0.699 ms
Execution time: 982717.118 ms
My table:
CREATE TABLE "public"."device_data" (
"deviceid" Bigint REFERENCES cars (deviceid),
"ts" Timestamp Without Time Zone,
"longitude" Double Precision,
"lattitude" Double Precision,
"speedgps" Numeric( 5, 0 ),
"heading" Numeric( 5, 0 ),
"altitude" Numeric( 5, 0 ),
"satelite" Numeric( 3, 0 ),
"eventid" Numeric( 3, 0 ),
"mileagegps" Numeric( 20, 0 ),
"inputs" Numeric( 5, 0 ),
"voltageanalog1" Numeric( 4, 2 ),
"voltageanalog2" Numeric( 4, 2 ),
"voltageanalog3" Numeric( 4, 2 ),
"voltageanalog4" Numeric( 4, 2 ),
"voltageanalog5" Numeric( 4, 2 ),
"outputs" Numeric( 3, 0 ),
"totaldistance" Numeric( 20, 0 ),
"totalfuel" Numeric( 20, 0 ),
"vehiclespeed" Numeric( 5, 0 ),
"enginespeed" Numeric( 5, 0 ),
"fuellevel" Numeric( 5, 0 ),
"fuelcons" Numeric( 5, 0 ),
"accelerator" Numeric( 3, 0 ),
"tachograph" Numeric( 5, 0 ),
"axleweight" Numeric( 5, 0 ),
"indicators" Numeric( 10, 0 ),
"drivercode" Numeric( 20, 0 ),
"wiretemp1" Double Precision,
"wiretemp2" Double Precision,
"wiretemp3" Double Precision,
"wiretemp4" Double Precision,
"wiretemp5" Double Precision,
"wiretemp6" Double Precision,
"fuelflag" Integer,
"gsmsignal" SmallInt,
"speedcan" Integer,
"gsmoperator" Bigint,
"totalidlefuel" Bigint,
"fuellevelperc" Integer,
"enginetemp" SmallInt,
"enginetotalhours" Bigint,
"engineidletime" Bigint,
"oiltemp" SmallInt,
"hydroiltemp" SmallInt,
"wirecode1" Numeric( 20, 0 ),
"wirecode2" Numeric( 20, 0 ),
"wirecode3" Numeric( 20, 0 ),
"wirecode4" Numeric( 20, 0 ),
"wirecode5" Numeric( 20, 0 ),
"wirecode6" Numeric( 20, 0 ),
"rapidpedalpress" Bigint,
"rapidaccel" Bigint,
"rapidbreak" Bigint,
"engineoverspeed" Bigint,
"torgue" SmallInt,
"drivetimeoverspeedlimit0x12" Bigint,
"drivetimeoverspeedlimit0x13" Bigint,
"driver1idcard" Numeric( 20, 0 ),
"driver2idcard" Numeric( 20, 0 ),
"x3d" SmallInt,
"y3d" SmallInt,
"z3d" SmallInt,
"axleweight1" Integer,
"axleweight2" Integer,
"axleweight3" Integer,
"axleweight4" Integer );