Skip to main content
deleted 111 characters in body
Source Link
Laurenz Albe
  • 56.4k
  • 4
  • 50
  • 82

Your index is uselessprobably not used because its leading columnit does not appear inmatch the WHEREmost selective conditions. Imagine you have a list sorted by last name and (within all people withIf too many rows are returned from the same last name) by first name. Now try to use that list to efficiently find all "William". See – it doesn't workindex scan, a sequential scan is more efficient.

Try this index:

CREATE INDEX ON account_transactions (transaction_type, subsidiary_type, status, transacted_at);

If the condition of any of the leading three columns is not selective, omitting the column from the index is better. The important part is that transacted_at is last, because it is not used in a condition with = and is used in ORDER BY.

You can add WHERE conditions to the index like you did in your question.

Your index is useless because its leading column does not appear in the WHERE conditions. Imagine you have a list sorted by last name and (within all people with the same last name) by first name. Now try to use that list to efficiently find all "William". See – it doesn't work.

Try this index:

CREATE INDEX ON account_transactions (transaction_type, subsidiary_type, status, transacted_at);

If the condition of any of the leading three columns is not selective, omitting the column from the index is better. The important part is that transacted_at is last, because it is not used in a condition with = and is used in ORDER BY.

You can add WHERE conditions to the index like you did in your question.

Your index is probably not used because it does not match the most selective conditions. If too many rows are returned from the index scan, a sequential scan is more efficient.

Try this index:

CREATE INDEX ON account_transactions (transaction_type, subsidiary_type, status, transacted_at);

If the condition of any of the leading three columns is not selective, omitting the column from the index is better. The important part is that transacted_at is last, because it is not used in a condition with = and is used in ORDER BY.

You can add WHERE conditions to the index like you did in your question.

Source Link
Laurenz Albe
  • 56.4k
  • 4
  • 50
  • 82

Your index is useless because its leading column does not appear in the WHERE conditions. Imagine you have a list sorted by last name and (within all people with the same last name) by first name. Now try to use that list to efficiently find all "William". See – it doesn't work.

Try this index:

CREATE INDEX ON account_transactions (transaction_type, subsidiary_type, status, transacted_at);

If the condition of any of the leading three columns is not selective, omitting the column from the index is better. The important part is that transacted_at is last, because it is not used in a condition with = and is used in ORDER BY.

You can add WHERE conditions to the index like you did in your question.