Course contentsShow
← Databases & SQL
Lesson 155 of 1,9532. Querying and Data RetrievalFree lesson

Streaming Data Lookups

Optimize queries against rapidly changing data streams with current-state filtering.

In this lesson, you will learn how to extract the most recent "current state" from a high-velocity stream of incoming data without slowing down your database.

The Challenge of the Moving Target

Imagine you are tracking the temperature of thousands of industrial sensors. These sensors send updates every few seconds. If you query a table of millions of rows to find the "current" temperature for a specific sensor, the database has to scan through massive amounts of historical noise just to find the single latest timestamp. When data is streaming in rapidly, the volume makes standard queries inefficient.

The Optimization Strategy: Latest-State Filtering

To optimize lookups against streaming data, we avoid scanning the entire history. Instead, we use a technique where we filter for the "current state" using a subquery that identifies the latest timestamp per entity_id before we perform any joining or filtering.

By isolating the most recent record first, we minimize the data the engine needs to process.

-- Querying the current status of streaming sensors
WITH LatestStatus AS (
    SELECT 
        sensor_id, 
        MAX(recorded_at) AS last_update
    FROM sensor_stream
    GROUP BY sensor_id
)
SELECT s.*
FROM sensor_stream s
INNER JOIN LatestStatus ls 
    ON s.sensor_id = ls.sensor_id 
    AND s.recorded_at = ls.last_update
WHERE s.status = 'CRITICAL';

Why this works

  1. Reduced Cardinality: The Common Table Expression (CTE) effectively acts as a filter that reduces the working set to exactly one row per sensor.
  2. Indexing: If you have a composite index on (sensor_id, recorded_at), the database can perform this lookup as a highly efficient index scan rather than a full table scan.
  3. Streamlined Joins: By joining the main table against this pre-filtered set, you ensure that your subsequent filtering (like checking for 'CRITICAL' statuses) happens only on the data that matters right now.

When querying streaming data, you must treat historical entries as "ghosts"—they exist, but your queries should ignore them by explicitly narrowing your focus to the latest temporal point before applying your logic.

Key Takeaway: To query streaming data efficiently, define the "current state" via a grouping CTE to isolate the latest record per entity before performing your main analysis.