text: -- Generated by collect_sql on 2017-11-20 19:44 UTC
  
  CREATE INDEX cases_closed
  ON cases_case(org_id, assignee_id, opened_on DESC)
  WHERE closed_on IS NOT NULL;
  
  CREATE INDEX cases_messageaction_messages_idx ON cases_messageaction USING GIN ("messages");
  
  CREATE INDEX cases_open
  ON cases_case(org_id, assignee_id, opened_on DESC)
  WHERE closed_on IS NULL;
  
  CREATE INDEX contacts_contact_org_uuid ON contacts_contact(org_id, uuid)
  
  CREATE INDEX msgs_archived
  ON msgs_message(org_id, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE AND is_archived = TRUE;
  
  CREATE INDEX msgs_flagged
  ON msgs_message(org_id, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE AND is_archived = FALSE AND is_flagged = TRUE;
  
  CREATE INDEX msgs_flagged_inc_archived
  ON msgs_message(org_id, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE AND is_flagged = TRUE;
  
  CREATE INDEX msgs_inbox
  ON msgs_message(org_id, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE AND is_archived = FALSE AND has_labels = TRUE;
  
  CREATE INDEX CONCURRENTLY msgs_message_org_modified_on
  ON msgs_message(org_id, has_labels, modified_on, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE;
  
  CREATE INDEX msgs_message_org_unhandled ON msgs_message(org_id) WHERE is_handled = FALSE;
  
  CREATE INDEX msgs_messageaction_messages_idx ON msgs_messageaction USING GIN ("messages");
  
  CREATE INDEX msgs_outgoing_org_partner_created
  ON msgs_outgoing(org_id, partner_id, created_on DESC);
  
  CREATE INDEX msgs_unlabelled_inbox
  ON msgs_message(org_id, created_on DESC)
  WHERE is_active = TRUE AND is_handled = TRUE AND is_archived = FALSE AND "type" = 'I' AND has_labels = FALSE;
  
  -- for fetching unsent notifications that need to be sent in order
  CREATE INDEX profiles_notification_created_on_unsent ON profiles_notification(created_on ASC) WHERE is_sent = FALSE;
  
  -- for displaying a user's latest notifications in a given org
  CREATE INDEX profiles_notification_org_user_created_on ON profiles_notification(org_id, user_id, created_on DESC);
  
  