Create Postgres Index for OR query

Posted on

Question :

This is the query I want to optimize:

SELECT *
    FROM invited_member_events
    WHERE (user_id = '422820341791064085'
    OR inviter_user_id = '422820341791064085')
    AND guild_id = '638685268777500672';

I created an index for the guild_id column, a second index with the columns user_id_inviter_user_id, a third index with the user_id column. And it does not use them, it only uses the guild_id one. Do you know how can I create an index so it works with the OR condition?

enter image description here

as you can see, only the guild_id index is used, not the other ones.

the other indexes:
enter image description here

Answer :

I added a idx_invited_member_events_inviter_user_id, and it seems to be working.

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *