Speeding up group by and max query
(Timescale cofounder here)

It sounds like you are trying to perform a "last point" query, e.g., find the latest record submitted by each (device, sensor).  The challenge with the query as above is that the database doesn't know when it has found the last device/sensor, so needs to scan back to the start of time to conclude there are no new devices around.

Much better is the following approach, which performs a LATERAL SCAN from a list of devices against the time-series records:

https://docs.timescale.com/latest/using-timescaledb/reading-data#last-point

Good luck, and you might also find our Slack group at https://slack.timescale.com useful for these types of questions.