Slow search by index query LIKE% MYSQL

守給你的承諾、 提交于 2019-12-01 18:09:26

The OR keyword drives MySQL's optimizer crazy.

You might try something like this.

SELECT name FROM table WHERE lang_index='en' AND name LIKE 'myname%'
UNION
SELECT name FROM table WHERE lang_index='en' AND enam LIKE 'myname%'

Or you might consider FULLTEXT searching if you can (if your table has MyISAM for access).

EDIT* It's hard to know exactly what's going on with these optimization things. Can you try this? This will see whether the language selection is fouling you up.

 SELECT name 
   FROM table 
  WHERE (name LIKE 'myname%' OR enam LIKE 'myname%')

Can you try this?

SELECT name FROM table WHERE lang_index='en' AND name LIKE 'myname%'
UNION ALL
SELECT name FROM table WHERE lang_index='en' AND enam LIKE 'myname%'

It won't give a perfect result -- it will have duplicate name items -- but it will skip a DISTINCT deduplicating step in your query.

You might also try this.

SELECT name 
  FROM table
 WHERE lang_index='en'
   AND id IN (
    SELECT id from table 
     WHERE (name LIKE 'myname%' OR enam LIKE 'myname%'))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!