Rails -- :id 属性需要数据库索引吗?
因此,当我关注 Michael Hartl 的 Ruby on Rails 教程时,我注意到在 users 表中,我们为 :email
属性添加了唯一索引,以提高 find
的效率> 方法,因此它不会逐行搜索。到目前为止,我们一直根据情况使用 find_by_email
和 find_by_id
进行搜索。然而我们从未为 :id
属性设置索引。 :id
是否会自动索引,因为它默认是唯一且连续的?或者情况并非如此,我应该为 :id
搜索添加索引吗?
So as I was following the Ruby on Rails Tutorial by Michael Hartl I noticed that in the users table we added a unique index for the :email
attribute to improve the efficiency of the find
method so it doesn't search row by row. So far we have been searching using both find_by_email
and find_by_id
depending on the case. Yet we never set up an index for the :id
attribute. Is :id
automatically indexed because it is by default unique and sequential in nature? Or is this not the case and should I add an index for :id
searching?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数数据库(包括 sqlite,它是 RoR 中的默认数据库)都会自动索引主键,Rails Migrations 默认情况下主键是
:id
。Most databases (sqlite included, which is the default db in RoR) automatically index the primary key, which with Rails Migrations is
:id
by default.