Skip to content

Instantly share code, notes, and snippets.

@jonbartels
Last active February 7, 2023 15:18
Show Gist options
  • Save jonbartels/38ffbb101ea32f981cc9950a21ec6809 to your computer and use it in GitHub Desktop.
Save jonbartels/38ffbb101ea32f981cc9950a21ec6809 to your computer and use it in GitHub Desktop.
SQL to generate CREATE INDEX statements for Mirth Connect metadata columns for Postgres
--see https://github.com/nextgenhealthcare/connect/issues/4320
-- this generates create index statements for the metadata columns in MC excluding the default columns
-- the user SHOULD NOT BLINDLY CREATE ALL INDEXES
-- MC is update/insert heavy and having indexes can slow that performance.
-- These indexes should be used only on metadata colums which are searched frequently, on large tables, and where constraining by other indexed columns is not practical
-- Further, if you're searching on large tables you should run the damn pruner. see https://gist.github.com/MichaelLeeHobbs/40b4b7cf70ecbe30b73eed763367e626
SELECT t.table_name, c.column_name, format('CREATE INDEX CONCURRENTLY metadata_hax_%1$s_%2$s ON %1$s("%2$s");', t.table_name, c.column_name) as create_stmt
FROM information_schema.tables t
INNER JOIN information_schema.columns c ON c.table_name = t.table_name AND c.table_schema = c.table_schema
WHERE t.table_schema='public'
AND t.table_type='BASE TABLE'
AND t.table_name LIKE 'd_mcm%'
AND c.column_name NOT IN ('metadata_id', 'message_id', 'SOURCE', 'TYPE')
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment