1

I am wondering how MySQL finds the rows in a table when searching like so:

select * from table where field = 'text';

Does it use a particular search algorithm? Is it practically the fastest way to look up information in a table? Or would building a search macro using another algorithm (like Boyer-Moore) work faster?

4

3 回答 3

3

如果字段上有索引,那么数据库通常使用 b-tree 进行索引搜索。如果没有索引,则扫描整个表。本文介绍了 MySql 中使用的一些技术

http://dev.mysql.com/doc/refman/5.5/en/index-btree-hash.html

许多小时的工作已经投入到优化 MySql 上。利用已经完成的工作,并拒绝尝试重新做

于 2012-06-18T21:22:39.843 回答
1

如果您对它如何找到这些记录感兴趣,请尝试使用 EXPLAIN 关键字执行:

EXPLAIN select * from table where field = 'text';

我建议您查看这篇文章,以更好地了解后台发生的事情。

如果您能够自己编写更快的东西,我会感到非常惊讶。您可以查看在相关表上创建索引以加快选择速度。

于 2012-06-18T21:20:25.313 回答
1

对于该查询,它只能搜索该表的每个条目并将其字段列与该字符串进行比较。

不需要 Boyer-Moore,因为它要求的是完全相等,而不是询问该字段是否包含该字符串。

于 2012-06-18T21:17:56.573 回答