作者:魔帝君 | 来源:互联网 | 2023-08-01 16:54
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 个解决方案