You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: duplicate_indexes/readme.md
+72-3Lines changed: 72 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,10 @@
1
1
# Check for Duplicate Indexes
2
2
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
4
4
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.
6
8
7
9
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!
8
10
@@ -23,6 +25,8 @@ Some notes:
23
25
24
26
* 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.
25
27
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
+
26
30
## Example output
27
31
28
32
```
@@ -36,5 +40,70 @@ encompassing index definition | CREATE INDEX index_foo_on_bar_and_baz ON public.
36
40
enc index attributes | 2 3
37
41
```
38
42
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:
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
+
```
40
108
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