I’m working on an application where I need to monitor the latest IoT data being sent from a fleet of over 3000+ vehicles. Each vehicle is equipped with an IoT device that sends data to an Iotdata
table. To determine if a vehicle is actively sending data, I need to track the latest timestamp of the data it sends.
Problem: This approach is not working efficiently and is causing performance issues.
Requirement: I need a more efficient approach to achieve the following:
-
Track the latest timestamp of data sent from each vehicle.
-
Ensure minimal performance impact on the database.
-
Provide real-time or near-real-time status of each vehicle’s data transmission activity.
Questions:
-
What is a more efficient way to track and update each vehicle’s latest IoT data timestamp?
-
Are there any best practices or design patterns that could help optimise this process?
Any suggestions or insights would be greatly appreciated!
Current Approach:
-
There are two columns in the
Vehicles
table:iot_id
andtimestamp
. Both columns are foreign keys. -
I run a query every minute to fetch the latest timestamp from the
Iotdata
table and update thetimestamp
column in theVehicles
table.
I have attached the link for DDL information here: https://drive.google.com/file/d/1Ydh2Q4zJeN6x2W4p_VKW80utTr_rQ3pY/view?usp=sharing
2
Answers
I use an implementation of ULIDs (aka UUiD v7) in combination of TimeScale extension, from Nikolay Samokhvalov, based on this post: https://x.com/samokhvalov/status/1730107298369171943
Works really well with about 5k weather stations.
This query should be fast, when both columns are indexed:
If not, please share the results from
explain(analyze, verbose, buffers)
for this statement.About your data model, I would change it to a data warehouse schema, a star schema. But I don’t think that makes it much faster, just easier to use and maintain.