Skip to content

Commit 545969f

Browse files
author
Gabrielle Roth
committed
Add more examples and some encouraging language
1 parent 270d0cc commit 545969f

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

duplicate_indexes/readme.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Check for Duplicate Indexes
22

3-
This is a script that checks for duplicate indexes in a more user-friendly fashion than our previous ones. Just run it in the database you want to check.
3+
Unneeded indexes, such as duplicates, take up disk space, require time to vacuum, and slow down update and insert operations
44

5-
This check returns results as a "duplicate index" and an "encompassing index." The theory is that you can drop the duplicate index in favor of the encompassing index.
5+
This script checks for duplicate indexes in a more user-friendly fashion than our previous ones. Just run it in the database you want to check.
6+
7+
Results are reported as a "duplicate index" and an "encompassing index." The theory is that you can drop the duplicate index in favor of the encompassing index.
68

79
Do not just follow this blindly, though! For example, if you have two identical indexes, they'll appear in the report as a pair twice: once with one as the duplicate and the other as encompassing, and then the reverse. (If there are three, the report shows all possible pairs.) Be sure you leave one!
810

@@ -23,6 +25,8 @@ Some notes:
2325

2426
* Your statistics may show that the "duplicate" index is being used; this is normal and not an argument to keep the duplicate. Postgres should switch to using the encompassing index once the duplicate is gone.
2527

28+
* A lot of folks react to this report with "How could this happen!?!" This is not a personal failing; if you're using an ORM for schema management, that's probably the source of most if not all of the duplicates. You may have to do some manual wrangling with your ORM to prevent them from re-occurring.
29+
2630
## Example output
2731

2832
```
@@ -36,5 +40,70 @@ encompassing index definition | CREATE INDEX index_foo_on_bar_and_baz ON public.
3640
enc index attributes | 2 3
3741
```
3842

39-
Since the multi-column index `index_foo_on_bar_and_baz` would be used for searches on the `bar` column, we can drop the individual index `index_foo_on_bar`.
43+
Since the multi-column index `index_foo_on_bar_and_baz` would be used for searches only on the `bar` column, we can drop the individual index `index_foo_on_bar`.
44+
45+
```
46+
-[ RECORD 2 ]-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
47+
table | public.movies
48+
dup index | index_movies_on_title
49+
dup index definition | CREATE INDEX index_movies_on_title ON public.movies USING btree (title)
50+
dup index attributes | 2
51+
encompassing index | index_movies_on_title_and_studio
52+
encompassing index definition | CREATE UNIQUE INDEX index_movies_on_title_and_studio ON public.movies USING btree (title, studio)
53+
enc index attributes | 2 3
54+
```
55+
56+
Same as example #1: the multi-column index `index_movies_on_title_and_studio` would be used for searches on just the `title` column, we can drop the individual index `index_movies_on_title`.
57+
58+
This next example shows what happens with multiple duplicate indexes:
59+
60+
```
61+
demo_db=# \d+ index_demo
62+
Table "public.index_demo"
63+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
64+
--------+---------+-----------+----------+----------------------------------------+----------+--------------+-------------
65+
id | integer | | not null | nextval('index_demo_id_seq'::regclass) | plain | |
66+
name | text | | | | extended | |
67+
Indexes:
68+
"idx_demo_name_uniq" UNIQUE, btree (name)
69+
"unique_name" UNIQUE CONSTRAINT, btree (name)
70+
"idx_demo_name" btree (name)
71+
```
72+
73+
```
74+
:::-->cat duplicate_indexes.txt
75+
-[ RECORD 1 ]-----------------+-------------------------------------------------------------------------------
76+
table | public.index_demo
77+
dup index | idx_demo_name
78+
dup index definition | CREATE INDEX idx_demo_name ON public.index_demo USING btree (name)
79+
dup index attributes | 2
80+
encompassing index | idx_demo_name_uniq
81+
encompassing index definition | CREATE UNIQUE INDEX idx_demo_name_uniq ON public.index_demo USING btree (name)
82+
enc index attributes | 2
83+
-[ RECORD 2 ]-----------------+-------------------------------------------------------------------------------
84+
table | public.index_demo
85+
dup index | idx_demo_name
86+
dup index definition | CREATE INDEX idx_demo_name ON public.index_demo USING btree (name)
87+
dup index attributes | 2
88+
encompassing index | unique_name
89+
encompassing index definition | CREATE UNIQUE INDEX unique_name ON public.index_demo USING btree (name)
90+
enc index attributes | 2
91+
-[ RECORD 3 ]-----------------+-------------------------------------------------------------------------------
92+
table | public.index_demo
93+
dup index | idx_demo_name_uniq
94+
dup index definition | CREATE UNIQUE INDEX idx_demo_name_uniq ON public.index_demo USING btree (name)
95+
dup index attributes | 2
96+
encompassing index | unique_name
97+
encompassing index definition | CREATE UNIQUE INDEX unique_name ON public.index_demo USING btree (name)
98+
enc index attributes | 2
99+
-[ RECORD 4 ]-----------------+-------------------------------------------------------------------------------
100+
table | public.index_demo
101+
dup index | unique_name
102+
dup index definition | CREATE UNIQUE INDEX unique_name ON public.index_demo USING btree (name)
103+
dup index attributes | 2
104+
encompassing index | idx_demo_name_uniq
105+
encompassing index definition | CREATE UNIQUE INDEX idx_demo_name_uniq ON public.index_demo USING btree (name)
106+
enc index attributes | 2
107+
```
40108

109+
Note that the UNIQUE CONSTRAINT shows up as its underlying index. You only need to keep one of these three indexes; usually that's one of the UNIQUE options.

0 commit comments

Comments
 (0)