freescout-help-desk / freescout

FreeScout — Free self-hosted help desk & shared mailbox (Zendesk / Help Scout alternative)
https://freescout.net
GNU Affero General Public License v3.0
2.95k stars 489 forks source link

Multiple query execution while searching particular term within search-box #4116

Closed deepakthemegrill closed 2 months ago

deepakthemegrill commented 3 months ago

Within search-box when I searched for "deactivate" term than it take approx 30 sec to load result image

After going through slow query logs I found when searching for "deactivate" term than it execute two queries were both query take approx 12-13 sec to complete execution total 26 sec both query take to process

select count(*) as aggregate from conversations inner join threads on conversations.id = threads.conversation_id left join customers on conversations.customer_id = customers.id where conversations.mailbox_id in (6, 1, 2, 5, 3, 4) and (conversations.subject like '%deactivate%' or conversations.customer_email like '%deactivate%' or conversations.id = 0 or conversations.id = 0 or customers.first_name like '%deactivate%' or customers.last_name like '%deactivate%' or threads.body like '%deactivate%' or threads.from like '%deactivate%' or threads.to like '%deactivate%' or threads.cc like '%deactivate%' or threads.bcc like '%deactivate%') group by conversations.id;

image

select conversations.* from conversations inner join threads on conversations.id = threads.conversation_id left join customers on conversations.customer_id = customers.id where conversations.mailbox_id in (6, 1, 2, 5, 3, 4) and (conversations.subject like '%deactivate%' or conversations.customer_email like '%deactivate%' or conversations.id = 0 or conversations.id = 0 or customers.first_name like '%deactivate%' or customers.last_name like '%deactivate%' or threads.body like '%deactivate%' or threads.from like '%deactivate%' or threads.to like '%deactivate%' or threads.cc like '%deactivate%' or threads.bcc like '%deactivate%') group by conversations.id order by last_reply_at desc limit 50 offset 0;

image

There is also some duplication or repetition. I don't know how this both query are related to each-other because result on first query is not being used in second.

I hope so their is place of optimization which can gradually reduce the search time.

freescout-help commented 3 months ago

If someone have an idea how to optimize it please share it or submit PR.