A
A
Art4es2017-10-19 09:31:48
SQL Server
Art4es, 2017-10-19 09:31:48

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%"

It is clear that a million to a million joins and then the search goes on.
How can this query be optimized? Runs for a very long time

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Karetnikov, 2017-10-19
@art_karetnikov

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.

N
n-fom, 2017-10-19
@n-fom

Full Text Search

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question