optimize tables for search using LIKE clause in MySQL

后端 未结 4 607
一个人的身影
一个人的身影 2021-01-15 03:10

I am building a search feature for the messages part of my site, and have a messages database with a little over 9,000,000 rows, and and index on the sender,

相关标签:
4条回答
  • 2021-01-15 03:48

    You should either use full-text indexes (you said you can't), design a full-text search by yourself or offload the search from MySQL and use Sphinx/Lucene. For Lucene you can use Zend_Search_Lucene implementation from Zend Framework or use Solr.

    Normal indexes in MySQL are B+Trees, and they can't be used if the starting of the string is not known (and this is the case when you have wildcard in the beginning)

    Another option is to implement search on your own, using reference table. Split text in words and create table that contains word, record_id. Then in the search you split the query in words and search for each of the words in the reference table. In this way you are not limitting yourself to the beginning of the whole text, but only to the beginning of the given word (and you'll match the rest of the words anyway)

    0 讨论(0)
  • 2021-01-15 03:58

    '%EXAMPLE_QUERY%'; is a very very bad idea .. am going to give you some

    A. Avoid wildcards at the start of LIKE queries use 'EXAMPLE_QUERY%'; instead

    B. Create Keywords where you can easily use MATCH

    0 讨论(0)
  • 2021-01-15 04:08
    select * from emp where ename like '%e';
    

    gives emp_name that ends with letter e.

    select * from emp where ename like 'A%';
    

    gives emp_name that begins with letter a.

    select * from emp where ename like '_a%';
    

    gives emp_name in which second letter is a.

    0 讨论(0)
  • 2021-01-15 04:13

    If you want to stick with using MySQL, you should use FULL TEXT indexes. Full text indexes index words in a text block. You can then search on word stems and return the results in order of relevance. So you can find the word "example" within a block of text, but you still can't search efficiently on "xampl" to find "example".

    MySQL's full text search is not great, but it is functional.

    http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

    0 讨论(0)
提交回复
热议问题