3.1 KiB
3.1 KiB
Fix-problematic-indexes-of-long_text_xxxxxxxxx-to-avoid-the-performance-issue-when-adding-request-comments_688988255
Problem
Adding request comments takes 10~40 seconds with millions of records. Can also cause high database load and/or high database CPU.
Cause
This issue may be caused by incorrect indexes. This topic gives steps to fix this type of indexes.
Solution
Follow these steps to remove the fix the index:
- Navigate to database xservices_ems, set search_path to the right schema.
- Check if there are indexes that are built on fields (entity_id, field_id), but have no benefit on the SQL query mentioned here as it has is_deleted=false in the index WHERE condition. If the results are empty, then you can ignore the next steps.
select indexname, indexdef from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%USING btree (entity_id, field_id)%is_deleted = false%' and schemaname = 'maas_admin'; - Check the indexes before the operation.
select indexdef from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' and schemaname = 'maas_admin'; - Rename the index with is_deleted=false.
Run the generated "Alter index" commands.
select 'ALTER INDEX '||indexname||' RENAME TO '||indexname||'0;' from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' AND indexdef like '%USING btree (entity_id, field_id)%' and indexdef like '%is_deleted = false%' and schemaname = 'maas_admin'; - Rename the index with the wrong name but correct indexdef to the right name.
Run the generated "Alter index" commands.
select 'ALTER INDEX '||indexname||' RENAME TO '||tablename||'_entity_id_field_id_idx;' FROM pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' AND indexdef like '%USING btree (entity_id, field_id)' AND indexname !~ '^long_text_\d{9}_entity_id_field_id_idx$' and tablename ~ 'long_text_\d{9}'; - Build the CREATE UNIQUE INDEX sql.
Run the generated "CREATE UNIQUE INDEX" commands.
select 'CREATE UNIQUE INDEX IF NOT EXISTS '||tablename||'_entity_id_field_id_idx'||' on '||tablename||' USING btree (entity_id, field_id);' from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' AND indexdef like '%USING btree (entity_id, field_id)%' and indexdef like '%is_deleted = false%' and schemaname = 'maas_admin'; - DROP the index with the wrong name.
Run the generated "DROP INDEX" commands.
select 'DROP INDEX '||indexname||';' from pg_indexes where tablename ~ 'long_text_\d{9}' and indexname !~ '^long_text_\d{9}_entity_id_field_id_idx$' and indexdef like '%UNIQUE%' AND indexdef like '%USING btree (entity_id, field_id)%' and schemaname = 'maas_admin'; - Check the index.
select indexdef from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' and schemaname = 'maas_admin'; - Analyze the table.
select 'ANALYZE '||tablename||';' from pg_indexes where tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' and schemaname = 'maas_admin'; Run the generated "ANALYZE" commands.