Cloud Native 7 min read

How Materialized Views Turn Log Queries from Seconds to Milliseconds

Facing painfully slow log queries under high concurrency, the team enabled SLS materialized views and, through three real‑world cases, reduced query latency from thousands of milliseconds to sub‑second levels, achieving up to 89‑fold performance gains while keeping storage costs negligible.

Alibaba Cloud Observability
Alibaba Cloud Observability
Alibaba Cloud Observability
How Materialized Views Turn Log Queries from Seconds to Milliseconds

Backend engineers often encounter severe latency when querying massive log datasets, especially under high‑concurrency SDK calls that trigger dynamic conditions. In one large‑scale business team, the original queries took up to 4100 ms, causing timeouts and alarm storms.

Case 1: High‑Concurrency SDK Bombardment

Pain point: Rapid, concurrent requests with constantly changing filter criteria caused average query times of 4100 ms, leading to a vicious cycle of slow queries, thread‑pool backlog, and further contention.

Original SQL (simplified):

select column1, column2, column3,
       (timestamp - timestamp % 3600) as time_slot,
       count(*) as cnt,
       avg(metric_val) as avg_lat
from log
group by column1, column2, column3, time_slot

After enabling a materialized view: Query time dropped to 46 ms, an 89× speed‑up, and response time remained stable regardless of SDK concurrency or query condition changes.

Case 2: De‑duplicated Statistics

Counting distinct hash values (a known resource‑hungry operation) previously required 16.8 s on average and often timed out when the time window widened.

Original SQL:

select project_id,
       count(1) as event_cnt,
       count(distinct hash_val) as issue_cnt
from log
group by project_id

Optimized with a materialized view: Execution time fell to 2.2 s, an 8× improvement, turning an “often unavailable” feature into a reliably fast query.

Case 3: Year‑over‑Year Latency Comparison

The team needed to compare operation‑log latency across multiple days (1, 2, 3, 7‑day offsets). The original query took 54.3 s, frequently timing out.

Original SQL (excerpt):

select time,
       diff[1] as day1,
       diff[2] as day2,
       diff[3] as day3,
       diff[4] as day7
from (
    select time,
           ts_compare(avg_latency, 86400, 172800, 604800) as diff
    from (
        select avg(latency) as avg_latency,
               date_trunc('hour', __time__) as time
        from log
        group by time
    )
) order by time

After materialization: The query completed in 958 ms, a 56× speed‑up, delivering near‑instant results for operational dashboards.

ROI and Cost Analysis

High utilization: The three views together served over 10,223 queries in a single day.

Negligible cost: Additional storage for pre‑computed results was less than 0.1 % of the raw log storage expense.

When to Use SLS Materialized Views

Queries that involve heavy de‑duplication (e.g., count(distinct)) or high‑precision percentile calculations ( approx_percentile).

Interactive dashboards where sub‑second response is required for a smooth user experience.

High‑concurrency scenarios where many automated monitors or SDKs query the same data simultaneously.

The accompanying diagram (shown below) summarizes the performance gains and the ideal application scenarios.

Performance summary diagram
Performance summary diagram
cloud nativePerformance TuningSLSmaterialized viewlog query optimization
Alibaba Cloud Observability
Written by

Alibaba Cloud Observability

Driving continuous progress in observability technology!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.