Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of like '%Query%' vs full text search CONTAINS query

I have a situation where I would like to search a single word.

For that scenario, which query would be good from a performance point of view?

Select Col1, Col2 from Table Where Col1 Like '%Search%' 

or

Select Col1, Col2 from Table Where Col1 CONTAINS(Col1,'Search') 

?

like image 822
dotnetguts Avatar asked Jul 09 '10 15:07

dotnetguts


People also ask

Is Full text search faster than like?

Assuming all that is correct, I'll go ahead and make the following statement: The fulltext index is in fact an index, which makes search faster. It is large, and has fewer search posibilities than LIKE would have, but it's way faster.

Which is faster like or contain?

Thus it is clear that CONTAINS is faster than LIKE.

Which is faster like or in SQL?

1 Answer. Using '=' operator is faster than the LIKE operator in comparing strings because '=' operator compares the entire string but the LIKE keyword compares by each character of the string.

Does nested query affect performance?

you can absolutely write a sub-query that performs horribly, does horrible things, runs badly, and therefore absolutely screws up your system. just as you can with any kind of query. i am addressing the bad advice that a sub-query is to be avoided because they will inherently lead to poor performance.


1 Answers

Full Text Searching (using the CONTAINS) will be faster/more efficient than using LIKE with wildcarding. Full Text Searching (FTS) includes the ability to define Full Text Indexes, which FTS can use. I don't know why you wouldn't define a FTS index if you intended to use the functionality.

LIKE with wildcarding on the left side (IE: LIKE '%Search') can not use an index (assuming one exists for the column), guaranteeing a table scan. I haven't tested & compared, but regex has the same pitfall. To clarify, LIKE '%Search' and LIKE '%Search%' can not use an index; LIKE 'Search%' can use an index.

like image 194
OMG Ponies Avatar answered Sep 21 '22 23:09

OMG Ponies