热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据库索引如何使搜索更快-Howdodatabaseindicesmakesearchfaster

Iwasreadingthroughrailstutorial(http:ruby.railstutorial.orgbookruby-on-rails-tutorial#side

I was reading through rails tutorial (http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sidebar-database_indices) but confused about the explanation of database indicies, basically the author proposes that rather then searching O(n) time through the a list of emails (for login) its much faster to create an index, giving the following example:

我正在阅读rails教程(http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sidebar-database_indices),但对数据库指标的解释感到困惑,基本上作者提出的不是搜索O( n)通过电子邮件列表(登录)的时间,创建索引要快得多,给出以下示例:

To understand a database index, it’s helpful to consider the analogy of a book index. In a book, to find all the occurrences of a given string, say “foobar”, you would have to scan each page for “foobar”. With a book index, on the other hand, you can just look up “foobar” in the index to see all the pages containing “foobar”. source: http://ruby.railstutorial.org/chapters/modeling-users#sidebar:database_indices**

要理解数据库索引,考虑书籍索引的类比是有帮助的。在一本书中,为了找到给定字符串的所有出现,比如说“foobar”,你必须扫描每一页的“foobar”。另一方面,使用书籍索引,您只需在索引中查找“foobar”即可查看包含“foobar”的所有页面。来源:http://ruby.railstutorial.org/chapters/modeling-users#sidebar:database_indices**

So what I understand from that example is that words can be repeated in text, so the "index page" consists of unique entries. However, in the railstutorial site, the login is set such that each email address is unique to an account, so how does having an index make it faster when we can have at most one occurrence of each email?

所以我从这个例子中理解的是,单词可以在文本中重复,因此“索引页面”由唯一条目组成。但是,在railstutorial网站中,登录设置为每个电子邮件地址对于一个帐户是唯一的,那么当我们每个电子邮件最多只出现一次时,如何使索引更快?

Thanks

3 个解决方案

#1


4  

Indexing isn't (much) about duplicates. It's about order.

索引不是(很多)关于重复。这是关于订单。

When you do a search, you want to have some kind of order that lets you (for example) do a binary search to find the data in logarithmic time instead of searching through every record to find the one(s) you care about (that's not the only type of index, but it's probably the most common).

当你进行搜索时,你希望有某种顺序让你(例如)进行二进制搜索,以对数时间查找数据,而不是搜索每条记录以找到你关心的那些(这是不是唯一的索引类型,但它可能是最常见的)。

Unfortunately, you can only arrange the records themselves in a single order.

不幸的是,您只能在一个订单中自行安排记录。

An index contains just the data (or a subset of it) that you're going to use to search on, and pointers (or some sort) to the records containing the actual data. This allows you to (for example) do searches based on as many different fields as you care about, and still be able to do binary searching on all of them, because each index is arranged in order by that field.

索引仅包含您要用于搜索的数据(或其子集),以及包含实际数据的记录的指针(或某种类型)。这允许您(例如)基于您关心的多个不同字段进行搜索,并且仍然能够对所有字段进行二进制搜索,因为每个索引按该字段按顺序排列。

#2


4  

Because the index in the DB and in the given example is sorted alphabetically. The raw table / book is not. Then think: How do you search an index knowing it is sorted? I guess you don't start reading at "A" up to the point of your interest. Instead you skip roughly to the POI and start searching from there. Basically a DB can to the same with an index.

因为DB和给定示例中的索引按字母顺序排序。原始表/书不是。然后想一想:你如何搜索已知排序的索引?我想你不会开始阅读“A”,直到你感兴趣的程度。相反,你大致跳过POI并从那里开始搜索。基本上DB可以与索引相同。

#3


1  

It is faster because the index contains only values from the column in question, so it is spread across a smaller number of pages than the full table. Also, indexes usually include additional optimizations such as hash tables to limit the number of reads required.

它更快,因为索引仅包含来自相关列的值,因此它分布在比完整表少的页面上。此外,索引通常还包括其他优化,例如哈希表,以限制所需的读取次数。


推荐阅读
author-avatar
魔帝君
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有