Skip to content

Script to Generate All Indexes in a Database in SQL #342

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT
TABLE_NAME,
INDEX_NAME,
COLUMN_NAME,
NON_UNIQUE,
INDEX_TYPE
FROM
information_schema.STATISTICS
WHERE
TABLE_SCHEMA = 'University'
ORDER BY
TABLE_NAME,
INDEX_NAME,
SEQ_IN_INDEX;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT
TABLE_NAME,
INDEX_NAME,
COLUMN_NAME
FROM
information_schema.STATISTICS
WHERE
TABLE_SCHEMA = 'University'
ORDER BY
TABLE_NAME,
INDEX_NAME,
SEQ_IN_INDEX;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SELECT
t.relname AS table_name,
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class t
JOIN
pg_index ix ON t.oid = ix.indrelid
JOIN
pg_class i ON i.oid = ix.indexrelid
JOIN
pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
WHERE
t.relkind = 'r'
AND t.relnamespace = 'public'::regnamespace
ORDER BY
t.relname,
i.relname;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT
schemaname,
tablename,
indexname,
indexdef
FROM
pg_indexes
WHERE
schemaname = 'public'
ORDER BY
tablename,
indexname;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SELECT
t.name AS table_name,
ind.name AS index_name,
col.name AS column_name,
ind.is_unique,
ind.type_desc AS index_type
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE
t.is_ms_shipped = 0
ORDER BY
t.name, ind.name, ic.key_ordinal;