索引如何加速搜索? [重复]

时间:2022-09-20 14:15:57

This question already has an answer here:

这个问题在这里已有答案:

How do indexes help in speeding up searching for data based on specific criteria?

索引如何帮助加速基于特定条件的数据搜索?

If there's a table with 6 columns and none of them are indexed, the program has to check all table rows.

如果有一个包含6列的表并且没有索引,则程序必须检查所有表行。

Indexing involves creating another separate table with just two columns, the id and the column you want indexed.

索引涉及创建另一个单独的表,其中只有两列,即要索引的id和列。

What I don't understand, is how does that help the application do faster searches? It doesn't read the entire 6 column table, but it still has to read the entire 2 column table, right? Which has the same number of rows...

我不明白,这是如何帮助应用程序进行更快速的搜索?它不会读取整个6列表,但它仍然必须读取整个2列表,对吧?哪个行数相同......

2 个解决方案

#1


3  

It functions a lot like an index in a book. We don't read the entire index to find the entry we want, and once we find the entry, we don't keep reading the index for other instances of that same entry. Once we find the entry, we don't have to read the entire book, just jump to the entry we want. These operations are in normal table lookups and indexing saves us time much in the same way a book index would.

它的功能很像书中的索引。我们不会读取整个索引来查找我们想要的条目,一旦找到该条目,我们就不会继续读取该条目的其他实例的索引。一旦找到条目,我们就不必阅读整本书,只需跳转到我们想要的条目即可。这些操作在普通的表查找中,索引节省了我们的时间,就像书索引一样。

#2


3  

Creating an index basically creates either an on-disk hash table or an on-disk search tree (usually some sort of B Tree).

创建索引基本上创建了磁盘上的哈希表或磁盘上的搜索树(通常是某种B树)。

Searching a hash table for an exact match is O(1) while searching either an exact match or closest matches in an ordered search tree is O(log(n)).

在哈希表中搜索完全匹配是O(1),而在有序搜索树中搜索精确匹配或最接近的匹配是O(log(n))。

This is in contrast to scanning the entire table, which is O(n).

这与扫描整个表格形成对比,即O(n)。

#1


3  

It functions a lot like an index in a book. We don't read the entire index to find the entry we want, and once we find the entry, we don't keep reading the index for other instances of that same entry. Once we find the entry, we don't have to read the entire book, just jump to the entry we want. These operations are in normal table lookups and indexing saves us time much in the same way a book index would.

它的功能很像书中的索引。我们不会读取整个索引来查找我们想要的条目,一旦找到该条目,我们就不会继续读取该条目的其他实例的索引。一旦找到条目,我们就不必阅读整本书,只需跳转到我们想要的条目即可。这些操作在普通的表查找中,索引节省了我们的时间,就像书索引一样。

#2


3  

Creating an index basically creates either an on-disk hash table or an on-disk search tree (usually some sort of B Tree).

创建索引基本上创建了磁盘上的哈希表或磁盘上的搜索树(通常是某种B树)。

Searching a hash table for an exact match is O(1) while searching either an exact match or closest matches in an ordered search tree is O(log(n)).

在哈希表中搜索完全匹配是O(1),而在有序搜索树中搜索精确匹配或最接近的匹配是O(log(n))。

This is in contrast to scanning the entire table, which is O(n).

这与扫描整个表格形成对比,即O(n)。