Skip to content

Commit e9ae28a

Browse files
committed
Script to Generate All Indexes in a Database in SQL
1 parent c4c0cbd commit e9ae28a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
SELECT
2+
TABLE_NAME,
3+
INDEX_NAME,
4+
COLUMN_NAME
5+
FROM
6+
information_schema.STATISTICS
7+
WHERE
8+
TABLE_SCHEMA = 'University'
9+
ORDER BY
10+
TABLE_NAME,
11+
INDEX_NAME,
12+
SEQ_IN_INDEX;
13+
14+
15+
SELECT
16+
TABLE_NAME,
17+
INDEX_NAME,
18+
COLUMN_NAME,
19+
NON_UNIQUE,
20+
INDEX_TYPE
21+
FROM
22+
information_schema.STATISTICS
23+
WHERE
24+
TABLE_SCHEMA = 'University'
25+
ORDER BY
26+
TABLE_NAME,
27+
INDEX_NAME,
28+
SEQ_IN_INDEX;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
SELECT
2+
schemaname,
3+
tablename,
4+
indexname,
5+
indexdef
6+
FROM
7+
pg_indexes
8+
WHERE
9+
schemaname = 'public'
10+
ORDER BY
11+
tablename,
12+
indexname;
13+
14+
15+
16+
SELECT
17+
t.relname AS table_name,
18+
i.relname AS index_name,
19+
a.attname AS column_name
20+
FROM
21+
pg_class t
22+
JOIN
23+
pg_index ix ON t.oid = ix.indrelid
24+
JOIN
25+
pg_class i ON i.oid = ix.indexrelid
26+
JOIN
27+
pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
28+
WHERE
29+
t.relkind = 'r'
30+
AND t.relnamespace = 'public'::regnamespace
31+
ORDER BY
32+
t.relname,
33+
i.relname;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SELECT
2+
t.name AS table_name,
3+
ind.name AS index_name,
4+
col.name AS column_name,
5+
ind.is_unique,
6+
ind.type_desc AS index_type
7+
FROM
8+
sys.indexes ind
9+
INNER JOIN
10+
sys.index_columns ic ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id
11+
INNER JOIN
12+
sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id
13+
INNER JOIN
14+
sys.tables t ON ind.object_id = t.object_id
15+
WHERE
16+
t.is_ms_shipped = 0
17+
ORDER BY
18+
t.name, ind.name, ic.key_ordinal;

0 commit comments

Comments
 (0)