sql - PostgreSQL MAX() inner query really slow -
i've been tasked optimising of sql queries ran on large datasets. have query below i'm trying optimise @ moment struggling it
the query takes around 40-45 minutes comeback answer not acceptable. take longer dataset grows within db.
i believe inner max() function dramatically slows down query?
i appreciate or advice in making query better , faster.
select positionview.equipmenttimestamp, positionview.name, positionview.inumber, positionview.datauserprovidername, positionview.latitude, positionview.longitude public.positionview positionview.outbound = false , positionview.referenceidentifier null , positionview.responsetype = 'xyz' , positionview.equipmenttimestamp >='2016-02-02 14:44:08.213' , positionview.equipmenttimestamp <='2016-02-05 14:44:08.213' , positionview.equipmenttimestamp =( select max(positionview2.equipmenttimestamp) positionview positionview2 positionview.inumber = positionview2.inumber , positionview2.outbound = false , positionview2.referenceidentifier null , positionview2.responsetype = 'xyz' , positionview2.equipmenttimestamp >='2016-02-02 14:44:08.213' , positionview2.equipmenttimestamp <='2016-02-05 14:44:08.213');
many in advance!
this kind of statement can done using postgres' distinct on()
operator (which faster equivalent solution using standard window function):
select distinct on (inumber) equipmenttimestamp, name, inumber, datauserprovidername, latitude, longitude public.positionview outbound = false , referenceidentifier null , responsetype = 'xyz' , equipmenttimestamp >='2016-02-02 14:44:08.213' , equipmenttimestamp <='2016-02-05 14:44:08.213' order inumber, equipmenttimestamp desc;
an index on (inumber,equipmenttimestamp)
help
Comments
Post a Comment