Last active
July 24, 2024 16:08
-
-
Save VincentAntoine/913a511ec132a0c73df92902e15d9728 to your computer and use it in GitHub Desktop.
Sums the number of index scans and sequential scans performed on TimescaleDB hypertables to identify potentially missing indexes (tables with a significant number of sequential scans).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
ht.schema_name, | |
ht.table_name, | |
COUNT(DISTINCT ck.table_name) AS nb_of_chunks, | |
SUM(stat.seq_scan) AS seq_scan, | |
SUM(stat.idx_scan) AS idx_scan, | |
SUM(stat.idx_scan) - SUM(stat.seq_scan) AS difference, | |
CASE WHEN SUM(stat.idx_scan) < SUM(stat.seq_scan) THEN 'Missing index ?' ELSE 'OK' END AS missing_index | |
FROM _timescaledb_catalog.hypertable ht | |
JOIN _timescaledb_catalog.chunk ck ON ht.id = ck.hypertable_id | |
JOIN pg_stat_all_tables stat ON stat.schemaname = ck.schema_name AND stat.relname = ck.table_name | |
GROUP BY 1, 2 | |
ORDER BY difference DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment