Querying historical data in SCADA systems - Time & Space Complexity
When we ask a SCADA system for past data, the time it takes depends on how much data we want. We want to understand how this time grows as we ask for more history.
The question is: How does the system's work increase when we query more historical records?
Analyze the time complexity of the following code snippet.
// Query historical data for a sensor
function queryHistoricalData(sensorId, startTime, endTime) {
let data = [];
let records = database.getRecords(sensorId, startTime, endTime);
for (let record of records) {
data.push(process(record));
}
return data;
}
This code fetches all records for a sensor between two times and processes each record one by one.
- Primary operation: Looping through each historical record returned from the database.
- How many times: Once for every record between startTime and endTime.
As the time range grows, more records are returned and processed, so the work grows with the number of records.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 records | About 10 processing steps |
| 100 records | About 100 processing steps |
| 1000 records | About 1000 processing steps |
Pattern observation: The work grows directly with the number of records requested.
Time Complexity: O(n)
This means the time to get and process data grows in a straight line with how many records we ask for.
[X] Wrong: "Querying more time means the system takes the same time because it just asks once."
[OK] Correct: Actually, the system must handle each record returned, so more records mean more work and more time.
Understanding how data queries scale helps you explain system behavior clearly. This skill shows you can think about real-world system limits and performance.
"What if the system cached recent records? How would that change the time complexity when querying recent data?"