-- I did a few things to significantly speed up this query (about 40% faster!): -- - Changed the CTE to a subquery-- - Added a compound index on (date, temperature, timestamp)-- - Bumped Postgres' work_mem to 320MBSELECTextract(epochfromdate)*1000astimestamp,highest_timestamp,highest,lowest_timestamp,lowestFROM(SELECTDISTINCTON(date)date,last_value(timestamp)OVERwAShighest_timestamp,last_value(temperature)OVERwAShighest,first_value(timestamp)OVERwASlowest_timestamp,first_value(temperature)OVERwASlowestFROM(SELECTtimestamp,temperature,dateFROMweather_dataWHEREsensor_id=(SELECTsensor_idFROMweather_sensorsWHEREsensor_name=$1)ANDtemperatureISNOTNULL)dWINDOWwAS(PARTITIONBYdateORDERBYtemperature,timestampROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING)ORDERBYdateDESCLIMIT548)tORDERBYtimestampASC;
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.