-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.time-series-queries.sql
More file actions
53 lines (40 loc) · 1.47 KB
/
09.time-series-queries.sql
File metadata and controls
53 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- =====================================================
-- SENSOR CPU AVERAGE (FULL TABLE SCAN)
-- Demonstrates: Aggregation without indexes
-- =====================================================
SELECT sensor_id,
avg(cpu_usage)
FROM sensor_metrics
GROUP BY sensor_id;
-- =====================================================
-- RECENT DATA FILTER (TIME RANGE SCAN)
-- Demonstrates: Sequential scan on timestamp filter
-- =====================================================
SELECT *
FROM sensor_metrics
WHERE ts > now() - interval '2 months 20 days';
-- =====================================================
-- QUERY 3: SENSOR + TIME FILTER (COMPOSITE CONDITION)
-- Demonstrates: Multiple filters without index support
-- =====================================================
SELECT *
FROM sensor_metrics
WHERE sensor_id = 42
AND ts > now() - interval '2 months 20 days';
-- =====================================================
-- QUERY 4: ORDER BY PERFORMANCE TEST
-- Demonstrates: Sorting large dataset without index
-- =====================================================
SELECT *
FROM sensor_metrics
ORDER BY ts DESC
LIMIT 1000;
-- =====================================================
-- QUERY 5: TIME BUCKET AGGREGATION
-- Demonstrates: Grouping overhead on raw time-series data
-- =====================================================
SELECT date_trunc('minute', ts) AS minute_bucket,
avg(cpu_usage)
FROM sensor_metrics
GROUP BY minute_bucket
ORDER BY minute_bucket;