Answer the question
In order to leave comments, you need to log in
How to optimize a given query with Join?
There are 2 tables, let's say First (id, name, message) and Second (id, value, message), each with a million records
. There is a query:
select First.id, First.name, Second.value
from First
join Second
on First.id =Second.id
where First.message like "%smth%" or Second.message like "%smth%"
Answer the question
In order to leave comments, you need to log in
The problem is not in the join, but in the search bar. like "%smth%" means that the search will not use the index, which means it will iterate over the rows of the table. like "smth%" will give a much nicer picture if there is an index on messaqe (and it should be there in such a search). Full-text search is possible, but, of course, there are some pitfalls.
You can also make an index on name and use its / composite index in the search.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question