V
V
Vanya Huk2017-05-30 14:59:34
SQL
Vanya Huk, 2017-05-30 14:59:34

How to speed up LIKE in mysql?

How to speed up LIKE in mysql on text field? in the table of 100 records?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
F
Fortop, 2017-05-30
@Fortop

The most correct answer is not to use like at all.
And it's better to work with fulltext outside of mysql
The answer for the poor is to use only like with a prefix i.e. kind somestring%
None%somestring%

V
Vit, 2017-05-30
@fornit1917

if the query is for a strict match or type "query%", then a regular b-tree index will help (if you have varchar and the field length allows, of course). If "%query%" or "%query" then there is no way to speed it up. You can then set up a full-text index and use full-text search instead of LIKE.

R
Rsa97, 2017-05-30
@Rsa97

First, try to abandon the subquery.

SELECT `p`.`id`, `p`.`title`, `p`.`slug`, `p`.`label`, `p`.`price`, `p`.`old_price`, `p`.`category_id`, `p`.`picture_id`, `p`.`brand_id` 
  FROM `products` AS `p`
  LEFT JOIN `brands` AS `b` ON `b`.`id` = `p`.`brand_id`
  WHERE `p`.`title` LIKE '%лампа%' OR `b`.`title` LIKE '%лампа%' OR `b`.`tags` like '%лампа%'
  GROUP BY `products`.`id` order by `products`.`sales` desc, `products`.`views` desc 
  LIMIT 18

A
Adamos, 2017-05-30
@Adamos

select `id`, `title`, `slug`, `label`, `price`, `old_price`, `category_id`, `picture_id`, `brand_id` 
from `products` where 
`brand_id` IN (select id from `brands` where `title` like '%лампа%' or `tags` like '%лампа%') 
OR `title` like '%лампа%'
group by `products`.`id`
order by `products`.`sales` desc, `products`.`views` desc 
limit 18

S
shagguboy, 2017-05-30
@shagguboy

explain?

R
redya69, 2017-06-01
@redya69

Try sphinx - it's simple and convenient)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question