Performance of LIKE queries on multmillion row tables, MySQL

雨燕双飞 提交于 2019-12-09 04:21:45

问题


From anybody with real experience, how do LIKE queries perform in MySQL on multi-million row tables, in terms of speed and efficiency, if the field has a plain INDEX?

Is there a better alternative (that doesn't filter results out, like the FULLTEXT 50% rule) for perform database field searches on multi-million row tables?

EXAMPLE:

Schema (comments table)

id (PRIMARY) title(INDEX) content time stamp

Query

SELECT * FROM 'comments' WHERE 'title' LIKE '%query%'

回答1:


From anybody with real experience, how do LIKE queries perform in MySQL on multimillion row tables, in terms of speed and effiency, if the field has a plain INDEX?

Not so well (I think I had some searches in the range of 900k, can't say I have experience in multimillion row LIKEs).

Usually you should restrict the search any way you can, but this depends on table structure and application use case.

Also, in some Web use cases it's possible to actually improve performances and user experience with some tricks, like indexing separate keywords and create a keyword table and a rows_contains_keyword (id_keyword, id_row) table. The keyword table is used with AJAX to suggest search terms (simple words) and to compile them to integers -- id_keywords. At that point, finding the rows containing those keywords becomes really fast. Updating the table one row at a time is also quite performant; of course, batch updates become a definite "don't".

This is not so unlike what is already done by full text MATCH..IN BOOLEAN MODE if using only the + operator:

SELECT * FROM arts WHERE MATCH (title) AGAINST ('+MySQL +RDBMS' IN BOOLEAN MODE);

You probably want an InnoDB table to do that:

Boolean full-text searches have these characteristics:

  • They do not automatically sort rows in order of decreasing relevance. ...
  • InnoDB tables require a FULLTEXT index on all columns of the MATCH() expression to perform boolean queries. Boolean queries against a MyISAM search index can work even without a FULLTEXT index, although a search executed in this fashion would be quite slow. ...
  • They do not use the 50% threshold that applies to MyISAM search indexes.

Can you give more information on the specific case?




回答2:


LIKE will do a full table scan if you have a % at the start of the pattern.

You can use FULLTEXT in Boolean (rather than natural language) mode to avoid the 50% rule.

Boolean full-text searches have these characteristics:

They do not use the 50% threshold.

http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html




回答3:


I recommend you to restrict your query by other clauses also (date range for example), because a LIKE '%something' guarantees you a full table scan




回答4:


With Workbench, use EXPLAIN before your SELECT to test different conditions use of LIKE, with and without INDEX, with wildcard in different parts of your search term. You will get your own conclusion based on your tests, because each case is a specific case.



来源:https://stackoverflow.com/questions/11418932/performance-of-like-queries-on-multmillion-row-tables-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!