mysql select query optimization and how limit works in mysql

我只是一个虾纸丫 提交于 2019-12-04 12:18:43

问题


I am using mysql database which has only one table "data" with 17,151257 rows.This table has a column string. i want to print all the rows in which string column contains a particular query string (stored in "entered_query" variable), so i used following:

SELECT DISTINCT * from data WHERE string LIKE '%".$entered_query."%' limit 10

As obvious above query is taking too much time to execute.

I have read that indexing can be used but how in this case ?
I have also thought of dividing whole data into 10 different rows and then executing 10 parallel queries using
perl DBI.

Now I have following questions :

  1. How to reduce the execution time ?
  2. I have heard that "LIKE" in mysql query avoids the indexing so is there any better alternative for above query ?
  3. When we use limit 10 in mysql query then does mysql stops executing as soon as it finds first 10 results or first it searches whole data for given query then it returns first 10 results

回答1:


Regular indexing can't be used to improve that query. MySQL indexes are B-trees, which means they can find a prefix of the indexed column very quickly. But since your LIKE query has % at the beginning, there's no unique prefix to search for. So every row has to be scanned to match against the pattern.

However, MySQL also supports full-text searching. This creates an index of all the words in the column, and it can find these words quickly. See the documentation for details.

If you use LIMIT 10, it will stop scanning as soon as it finds the first 10 rows that satisfy the conditions. Unless you also use ORDER BY -- then it has to find all the rows so that it can sort them before selecting the first 10.



来源:https://stackoverflow.com/questions/17540857/mysql-select-query-optimization-and-how-limit-works-in-mysql

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