If customer put filter in the as of now query result is very slow. Do i need to create index on all the filter columns in a single index or individual columns. the big issue is sorting.
Below is the query
select it.conversation_id, it.conversation_source, it.full_name,
it.last_message_timestamp, it.conversation_read,
it.id as ticket_id, it.owner_id, it.status, it.priority,
it.tags, it.last_msg_content, it.last_msg_attachment,
it.last_msg_actor, it.last_msg_delivered, it.last_msg_read,
it.last_message_timestamp as message_created_at, it.subject,
it.closed_at, it.last_message_received_at
from inbox_ticket it
where
it.merchant_id = 115 and it.status <> 'assigned_to_bot'
and it.owner_id in (3486070,60,-1)
and it.last_message_timestamp >= '2023-07-01 00:00:00+05:30'
and it.last_message_timestamp <= '2023-07-31 00:00:00+05:30'
and it.last_message_timestamp < '2023-07-31 18:40:00+05:30'
order by it.last_message_timestamp desc, it.id desc
limit 25
Sorting includes two columns. Do i have to create index separately like index on (last_message_timestamp desc, id desc). Or All the filters + (last_message_timestamp desc, id desc)
I did not tried anything, since the data is huge. Currently it is 6.4 million
Currently if i do explain i get below result.
Limit (cost=4.18..4.19 rows=1 width=218) -> Sort (cost=4.18..4.19 rows=1 width=218) Sort Key: last_message_timestamp DESC, id DESC -> Index Scan using inbox_ticket_merchant_id_67656847 on inbox_ticket it (cost=0.14..4.17 rows=1 width=218) Index Cond: (merchant_id = 115) Filter: (((status)::text <> 'assigned_to_bot'::text) AND (last_message_timestamp >= '2023-07-01 00:00:00+05:30'::timestamp with time zone) AND (last_message_timestamp <= '2023-07-31 00:00:00+05:30'::timestamp with time zone) AND (last_message_timestamp < '2023-07-31 18:40:00+05:30'::timestamp with time zone) AND (owner_id = ANY ('{3486070,60,-1}'::integer[])))