-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchunk-stats.sql
More file actions
29 lines (29 loc) · 1.1 KB
/
chunk-stats.sql
File metadata and controls
29 lines (29 loc) · 1.1 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
/*
* Script: sql/timescale/chunk-stats.sql
*
* Purpose: Provides a high-level overview of hypertable chunk health.
*
* Description:
* This query is the #1 triage tool for TimescaleDB performance issues. It
* summarizes the number of chunks, total size, and average/min/max chunk
* size for each hypertable.
*
* Red Flags:
* - High `chunk_count` (e.g., > 500): Can lead to high query planning overhead.
* - Large variance between `min_chunk` and `max_chunk`: Suggests inconsistent data ingestion.
* - `avg_chunk_size` too large or small: Ideal size is ~25% of shared_buffers for the hot set.
*
* Safety:
* This script is read-only and uses TimescaleDB's information views, which are
* optimized for frequent access. Execution time is typically very fast.
*/
SELECT
hypertable_name,
count(*) AS chunk_count,
pg_size_pretty(sum(total_bytes)) AS total_size,
pg_size_pretty(avg(total_bytes)::bigint) AS avg_chunk_size,
pg_size_pretty(min(total_bytes)) AS min_chunk,
pg_size_pretty(max(total_bytes)) AS max_chunk
FROM timescaledb_information.chunks
GROUP BY hypertable_name
ORDER BY chunk_count DESC;