如果没有对手头的问题进行更详细的描述,我认为不可能说的比您对数据库工作原理的理解还不够多。
为简单起见,想象一个二叉搜索树。现在使用您的谓词(您的 where 条件)并考虑一种构建二叉搜索树的方法,它可以帮助您获取查询结果。作为一个简单的示例 - 考虑以下查询:
SELECT * FROM myTable WHERE some_value = 10
“some_value”上的简单二叉搜索树(即 - 二叉搜索树 - 对于树中的每个节点,“some_value”低于当前行的其他行将移至左侧,而具有更高 - 或等于的行- 值向右) - 在这种情况下可以提供帮助。利用这种二叉搜索树,现在可以简单地从树的根开始,根据当前节点的值沿着树的左或右路径向下 - 直到达到值“10” - 然后继续遵循正确的路径,直到找到更高的值。
您可以想象这将如何用于查询,例如
SELECT * FROM myTable WHERE some_value BETWEEN 10 AND 20
同样,同样的简单二叉搜索树可以轻松回答此查询。当然,人们可以很容易地陷入更复杂的例子——但此时我猜你在想:这很好,但是我如何创建这个二叉搜索树呢?
答案是索引——在这种情况下:
CREATE INDEX idx_myTable__some_value ON myTable(some_value);
这将告诉 MySQL 在表“myTable”上在列(按指定顺序)“some_value”上创建索引 - 并且该索引将命名为“idx_myTable__some_value”。
I think this is about as far into this topic as I can go in a simple answer like this. Let me however state that the above is an oversimplification - there's alot more to be said about this - to start off with, the actual index type being used is typically not even a binary search tree, but a B-tree (or, more likely, a B+-tree). Wikipedia has some decent articles about this, and the actual manual for MySQL should cover this quite well as well.