8000 Format code: newlines, spaces, indents, unneccessary brackets deleted by rin-nas · Pull Request #12 · dataegret/pg-utils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Format code: newlines, spaces, indents, unneccessary brackets deleted #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions sql/redundant_indexes.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@


WITH
index_data AS
(SELECT *,string_to_array(indkey::text,' ') as key_array,array_length(string_to_array(indkey::text,' '),1) as nkeys from pg_index)

WITH index_data AS (
SELECT
*,
string_to_array(indkey::text,' ') as key_array,
array_length(string_to_array(indkey::text,' '),1) as nkeys
FROM pg_index
)
SELECT
i1.indrelid::regclass::text,
pg_get_indexdef(i1.indexrelid) main_index,
pg_get_indexdef(i2.indexrelid) redundant_index,
pg_size_pretty(pg_relation_size(i2.indexrelid)) redundant_index_size
FROM
index_data as i1
JOIN
index_data as i2
ON i1.indrelid=i2.indrelid AND i1.indexrelid<>i2.indexrelid
WHERE
(regexp_replace(i1.indpred, 'location \d+', 'location', 'g') IS NOT DISTINCT FROM regexp_replace(i2.indpred, 'location \d+', 'location', 'g'))
AND
(regexp_replace(i1.indexprs, 'location \d+', 'location', 'g') IS NOT DISTINCT FROM regexp_replace(i2.indexprs, 'location \d+', 'location', 'g'))
AND
((i1.nkeys > i2.nkeys and not i2.indisunique) OR (i1.nkeys=i2.nkeys and ((i1.indisunique and i2.indisunique and (i1.indexrelid>i2.indexrelid)) or (not i1.indisunique and not i2.indisunique and (i1.indexrelid>i2.indexrelid)) or (i1.indisunique and not i2.indisunique))))
AND
i1.key_array[1:i2.nkeys]=i2.key_array
ORDER BY pg_relation_size(i2.indexrelid) desc,i1.indexrelid::regclass::text,i2.indexrelid::regclass::text;

i1.indrelid::regclass::text,
pg_get_indexdef(i1.indexrelid) main_index,
pg_get_indexdef(i2.indexrelid) redundant_index,
pg_size_pretty(pg_relation_size(i2.indexrelid)) redundant_index_size
FROM index_data as i1
JOIN index_data as i2 ON i1.indrelid = i2.indrelid AND i1.indexrelid <> i2.indexrelid
WHERE regexp_replace(i1.indpred, 'location \d+', 'location', 'g') IS NOT DISTINCT FROM regexp_replace(i2.indpred, 'location \d+', 'location', 'g')
AND regexp_replace(i1.indexprs, 'location \d+', 'location', 'g') IS NOT DISTINCT FROM regexp_replace(i2.indexprs, 'location \d+', 'location', 'g')
AND ( (i1.nkeys > i2.nkeys and not i2.indisunique)
OR ( i1.nkeys = i2.nkeys and ( (i1.indisunique and i2.indisunique and i1.indexrelid > i2.indexrelid)
or (not i1.indisunique and not i2.indisunique and i1.indexrelid > i2.indexrelid)
or (i1.indisunique and not i2.indisunique)
)
)
)
AND i1.key_array[1:i2.nkeys] = i2.key_array
ORDER BY
pg_relation_size(i2.indexrelid) desc,
i1.indexrelid::regclass::text,
i2.indexrelid::regclass::text;
0