Files
nexus/knowledgebase/csd-wiki/ICSD/Fix-problematic-indexes-of-long_text_xxxxxxxxx-to-avoid-the-performance-issue-when-adding-request-comments_688988255.md

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:

  1. Navigate to database xservices_ems, set search_path to the right schema.
  2. 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';
    
  3. 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';
    
  4. Rename the index with is_deleted=false.
    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';
    
    Run the generated "Alter index" commands.
  5. Rename the index with the wrong name but correct indexdef to the right name.
    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}';
    
    Run the generated "Alter index" commands.
  6. Build the CREATE UNIQUE INDEX sql.
    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';
    
    Run the generated "CREATE UNIQUE INDEX" commands.
  7. DROP the index with the wrong name.
    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';
    
    Run the generated "DROP INDEX" commands.
  8. Check the index.
    select indexdef from pg_indexes where  tablename ~ 'long_text_\d{9}' and indexdef like '%UNIQUE%' and schemaname = 'maas_admin';
    
  9. 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.