Skip to content

Commit 4283ef6

Browse files
committed
a bit reworked structure; rarely used indexes
1 parent f66bda4 commit 4283ef6

File tree

5 files changed

+68
-40
lines changed

5 files changed

+68
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ psql
1111

1212
In psql, type:
1313
```
14-
\i index.psql
14+
\i start.psql
1515
```
1616

1717
– it will open interactive menu.

index.psql

Lines changed: 0 additions & 39 deletions
This file was deleted.

indexes/rare.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--https://github.com/dataegret/pg-utils/tree/master/sql
2+
3+
SELECT
4+
pg_stat_user_indexes.schemaname||'.'||pg_stat_user_indexes.relname as tablename,
5+
indexrelname,
6+
pg_stat_user_indexes.idx_scan,
7+
(coalesce(n_tup_ins,0)+coalesce(n_tup_upd,0)-coalesce(n_tup_hot_upd,0)+coalesce(n_tup_del,0)) as write_activity,
8+
pg_stat_user_tables.seq_scan,
9+
pg_stat_user_tables.n_live_tup,
10+
pg_size_pretty(pg_relation_size(pg_index.indexrelid::regclass)) as size
11+
from pg_stat_user_indexes
12+
join pg_stat_user_tables
13+
on pg_stat_user_indexes.relid=pg_stat_user_tables.relid
14+
join pg_index
15+
ON pg_index.indexrelid=pg_stat_user_indexes.indexrelid
16+
where
17+
pg_index.indisunique is false
18+
and pg_stat_user_indexes.idx_scan::float/(coalesce(n_tup_ins,0)+coalesce(n_tup_upd,0)-coalesce(n_tup_hot_upd,0)+coalesce(n_tup_del,0)+1)::float<0.01
19+
and (coalesce(n_tup_ins,0)+coalesce(n_tup_upd,0)-coalesce(n_tup_hot_upd,0)+coalesce(n_tup_del,0))>10000
20+
order by 4 desc,1,2;
21+
File renamed without changes.

start.psql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
\echo Menu:
2+
\echo ' 1 – Basic Node Information (master/replica, lag, DB size, tmp files)'
3+
\echo ' 2 – '
4+
\echo ' 3 - Slowest Queries'
5+
\echo ' i1 - Unused/Redundant Indexes'
6+
\echo ' i2 - Rarely Used Indexes'
7+
\echo ' q – Quit from this tool'
8+
\echo
9+
\echo 'Type your choice and press <Enter>:'
10+
\prompt d_step_unq
11+
\set d_stp '\'' :d_step_unq '\''
12+
select
13+
:d_stp::text = '1' as d_step_is_1,
14+
:d_stp::text = '2' as d_step_is_2,
15+
:d_stp::text = '3' as d_step_is_3,
16+
:d_stp::text = 'i1' as d_step_is_i1,
17+
:d_stp::text = 'i2' as d_step_is_i2,
18+
:d_stp::text = 'q' as d_step_is_q \gset
19+
\if :d_step_is_1
20+
\i ./general/basic.sql
21+
\prompt 'Press <Enter> to continue…' d_dummy
22+
\i ./start.psql
23+
\elif :d_step_is_2
24+
\echo ' 2 is chosen!'
25+
\prompt 'Press <Enter> to continue…' d_dummy
26+
\i ./start.psql
27+
\elif :d_step_is_3
28+
\echo ' 3 is chosen!'
29+
\prompt 'Press <Enter> to continue…' d_dummy
30+
\i ./start.psql
31+
\elif :d_step_is_i1
32+
\i ./indexes/redundant.sql
33+
\prompt 'Press <Enter> to continue…' d_dummy
34+
\i ./start.psql
35+
\elif :d_step_is_i2
36+
\i ./indexes/rare.sql
37+
\prompt 'Press <Enter> to continue…' d_dummy
38+
\i ./start.psql
39+
\elif :d_step_is_q
40+
\echo 'Bye!'
41+
\echo
42+
\else
43+
\echo 'ERROR: Unkown option!'
44+
\i ./start.psql
45+
\endif
46+

0 commit comments

Comments
 (0)