-- Events from last hourSELECT * FROM eventsWHERE _tp_time > now() - INTERVAL 1 HOUR;-- Specific date range (historical)SELECT * FROM table(events)WHERE _tp_time >= '2024-01-01' AND _tp_time < '2024-02-01';-- Events from todaySELECT * FROM table(events)WHERE to_date(_tp_time) = today();
-- Apply functionsSELECT upper(event_type) as event_type, amount * 1.1 as amount_with_tax, to_string(event_time) as time_strFROM events;-- Conditional expressionsSELECT order_id, CASE WHEN amount < 100 THEN 'small' WHEN amount < 1000 THEN 'medium' ELSE 'large' END as order_sizeFROM orders;
-- This processes bounded data and terminatesSELECT device_id, avg(temperature) as avg_tempFROM table(sensor_readings)GROUP BY device_idORDER BY avg_temp DESC;
-- Subquery for filteringSELECT * FROM eventsWHERE user_id IN ( SELECT user_id FROM table(premium_users));-- Derived tablesSELECT date, avg(daily_total) as avg_dailyFROM ( SELECT to_date(_tp_time) as date, sum(amount) as daily_total FROM table(transactions) GROUP BY date)GROUP BY date;
WITH high_value_users AS ( SELECT user_id, sum(amount) as total FROM table(transactions) WHERE _tp_time > now() - INTERVAL 30 DAY GROUP BY user_id HAVING total > 10000)SELECT e.event_type, count() as event_countFROM events eINNER JOIN high_value_users h ON e.user_id = h.user_idGROUP BY e.event_type;
-- Process first 1000 events then stopSELECT * FROM eventsLIMIT 1000;-- Limit per time windowSELECT count(*) as cntFROM eventsLIMIT 5 EMIT PERIODIC INTERVAL 1 SECOND;
-- Analyze yesterday's user activitySELECT event_type, count() as event_count, count(DISTINCT user_id) as unique_users, avg(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) as purchase_rateFROM table(events)WHERE to_date(_tp_time) = today() - 1GROUP BY event_typeORDER BY event_count DESC;
-- Stream only anomalous sensor readingsSELECT sensor_id, temperature, humidity, reading_time, CASE WHEN temperature > 100 THEN 'temp_high' WHEN temperature < -20 THEN 'temp_low' WHEN humidity > 95 THEN 'humidity_high' ELSE 'normal' END as alert_typeFROM iot_sensorsWHERE temperature > 100 OR temperature < -20 OR humidity > 95SETTINGS seek_to = 'earliest';
-- Get latest value per device in last 5 minutesSELECT device_id, any_last(temperature) as latest_temp, max(_tp_time) as last_readingFROM table(sensor_readings)WHERE _tp_time > now() - INTERVAL 5 MINUTEGROUP BY device_id;
Filter data as early as possible to reduce processing:
-- Good: Filter before processingSELECT user_id, count() FROM events WHERE event_type = 'purchase'GROUP BY user_id;-- Less efficient: Filter after aggregationSELECT user_id, cnt FROM ( SELECT user_id, event_type, count() as cnt FROM events GROUP BY user_id, event_type) WHERE event_type = 'purchase';
Choose appropriate query mode
Use streaming queries for real-time monitoring and alerts
Use table() for analytical queries and reporting
Consider data volume when choosing mode
Leverage column projection
Select only needed columns to reduce network and memory overhead:
-- Good: Select specific columnsSELECT user_id, event_type FROM events;-- Avoid: SELECT * when not neededSELECT * FROM events; -- Transfers all columns
Use LIMIT for exploration
When exploring data, use LIMIT to avoid overwhelming results: