Pretty new to postgres and seem to have an issue here and I am not sure which direction to go in.
I have a query that is causing some performance issues and I cannot work out how to optimize it.
The query is actually quite simple:
SELECT transactions.* FROM transactions
LEFT OUTER JOIN companies ON "companies"."id" = "transactions"."company_id"
WHERE companies.code ILIKE '%777%'
ORDER BY transactions.id desc LIMIT 10
I want to find all transactions that are for a company that has 777 in the name. There are around 20million transactions in the database and around 200 companies.
The query currently times out even though we have a LIMIT of 10. I believe this is because the ILIKE is not using an index so it's horribly slow.
Would this be an instance where you may want to add the WHERE filter to the JOIN instead? I have tested this and it works lightning-quick IF a record is found. If no record is found it again times out.
Is there a type of Index we should be looking at which would improve the speed here?