对于一个表中有一百万条记录的数据库,哪种查询速度更快?为什么?如何优化查询?第一个:
SELECT id,column1,column2 FROM `table1` WHERE MATCH(column1)
AGAINST('string1 string2 string3 ' in boolean mode) ORDER BY column2 ASC LIMIT 10第二个:
SELECT id,column1,column2 FROM `table1` WHERE column1 LIKE %string1%
OR column1 LIKE %string2% OR column1 LIKE %string3% OR column2 LIKE %string1%
OR column2 LIKE %string2% OR column2 LIKE %string3% OR column3 LIKE %string1% OR
column3 LIKE %string2% OR column3 LIKE %string3% ORDER BY column2 ASC LIMIT 10发布于 2012-03-28 02:46:05
Match a是一个全文搜索。如果您为MySQL创建了全文搜索索引,它将比类似的'%...%‘快得多,因为在这种情况下将没有索引。
在'%...%‘上不能有任何索引的原因,是因为在字符串的每个字母上创建索引并不快。
另外,这两个查询具有不同的功能,全文索引扫描单词,并且只能用于单词。你不能用"match a“(布尔模式下的‘mi’)来搜索字符串"emil”。这是行不通的。当然,就像这样。
发布于 2012-03-28 02:46:03
MATCH...REPLACE的读取速度会更快,但插入起来会更长。你必须为它创建一个索引。如果你想使用它,你也会有很多要求(它只适用于MyISAM表)
您应该阅读以下内容:http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
下面是我谈到的限制:http://dev.mysql.com/doc/refman/5.0/en/fulltext-restrictions.html
发布于 2012-03-28 02:42:09
like '%foo%'类型匹配使得不可能使用索引,并且每次DB都必须执行全表扫描。全文版本应该要快得多。
https://stackoverflow.com/questions/9895573
复制相似问题