-- Estimate unique users per day
SELECTdate_trunc('day',timestamp)asday,distinct_count(hyperloglog(64,user_id))asunique_usersFROMeventsGROUPBYday;-- Combine counts across partitions
SELECTdistinct_count(rollup(hll))FROM(SELECThyperloglog(32,session_id)ashllFROMevents_2023UNIONALLSELECThyperloglog(32,session_id)FROMevents_2024)t;
T-Digest - 分位数近似
高精度百分位估算,针对尾部分位数(P95、P99)进行了优化。
-- Track response time percentiles
SELECTservice_name,approx_percentile(0.50,tdigest(100,response_time))asp50,approx_percentile(0.95,tdigest(100,response_time))asp95,approx_percentile(0.99,tdigest(100,response_time))asp99FROMapi_metricsGROUPBYservice_name;-- Store hourly percentiles in a materialized view
CREATEMATERIALIZEDVIEWhourly_percentilesASSELECTtime_bucket('1 hour',timestamp)ashour,tdigest(200,response_time)asdigestFROMrequestsGROUPBYhour;
UddSketch - 有界误差分位数
分位数估算使用指定的初始误差上界;桶数不足时实际误差上界可能增大,应通过结果的 error 检查。
-- CPU percentiles with a starting 1% error target
SELECThost_id,approx_percentile(0.95,uddsketch(100,0.01,cpu_percent))asp95_cpu,error(uddsketch(100,0.01,cpu_percent))asactual_errorFROMsystem_metricsGROUPBYhost_id;
计数器分析
计数器聚合 - 单调递增指标
处理单调递增的计数器,自动检测重置。
-- Request rate calculation
SELECTtime_bucket('5 min',timestamp)asbucket,rate(counter_agg(timestamp,request_count))asrequests_per_sec,delta(counter_agg(timestamp,request_count))astotal_requestsFROMmetricsGROUPBYbucket;-- Extrapolated rate for each hourly bucket
SELECTbucket,extrapolated_rate(summary,method=>'prometheus')ASbytes_per_secondFROM(SELECTtime_bucket('1 hour',timestamp)ASbucket,counter_agg(timestamp,bytes_sent,bounds=>time_bucket_range('1 hour',timestamp))ASsummaryFROMnetwork_statsGROUPBYtime_bucket('1 hour',timestamp))hourly;
仪表聚合 - 波动指标
用于分析上下波动的指标(如温度、内存使用量)。
-- Temperature change analysis
SELECTsensor_id,delta(gauge_agg(timestamp,temperature))astemp_delta,rate(gauge_agg(timestamp,temperature))astemp_rate_per_secFROMweather_dataGROUPBYsensor_id;
时间加权分析
时间加权平均
处理不规则采样数据,支持插值方法(LOCF 末次观测值保持、Linear 线性插值)。
-- Weighted average for irregular sensor readings
SELECTdevice_id,average(time_weight('LOCF',timestamp,sensor_value))asweighted_avg,average(time_weight('Linear',timestamp,sensor_value))aslinear_avgFROMiot_readingsGROUPBYdevice_id;-- Combining multiple time ranges
SELECTaverage(rollup(tw))FROM(SELECTtime_weight('LOCF',ts,val)astwFROMreadings_2023UNIONALLSELECTtime_weight('LOCF',ts,val)FROMreadings_2024)t;
数据可视化
LTTB 降采样
在保持视觉相似性的前提下对时序数据进行降采样,适用于图表展示。
-- Reduce 100K points to 1K for visualization
SELECTtime,valueFROMunnest((SELECTlttb(timestamp,price,1000)FROMstock_pricesWHEREsymbol='AAPL'));
ASAP 平滑
通过降噪生成易于阅读的图表,同时保留趋势特征。
-- Smooth daily data to weekly resolution
SELECTtime,valueFROMunnest((SELECTasap_smooth(date,daily_sales,52)FROMsales_dataWHEREdate>='2023-01-01'));
-- Step 1: Create aggregates
CREATETABLEdaily_summariesASSELECTdate_trunc('day',timestamp)asday,tdigest(200,response_time)asresponse_digest,stats_agg(response_time,request_size)asstatsFROMrequestsGROUPBYday;-- Step 2: Multiple analyses from same data
SELECTday,approx_percentile(0.50,response_digest)asmedian,approx_percentile(0.99,response_digest)asp99,average_y(stats)asavg_response,slope(stats)assize_correlationFROMdaily_summaries;
所有位于 experimental 模式(toolkit_experimental)中的函数可能会在版本间发生变化。生产环境中如需 API 稳定性保证,请使用稳定版函数。