Rails ActiveRecord 表索引 - 何时应使用它们?
我一遍又一遍地听说,您应该向要进行联接的任何外键添加索引。我还听说您应该为要查询的字段建立索引。有没有人有关于何时以及何时不添加索引的相当详尽的列表或一组指南?
一定存在一定大小的表,在该大小处使用索引效率很低。每个表的索引数量必须有限制,否则为什么不为每个列添加一个索引呢?
您可以建议的任何资源都会非常有帮助。
提前致谢!
I have heard over and over that you should add indexes to any foreign key you will be doing joins on. I have also heard you should have indexes for fields you will do queries on. Does anyone have a fairly exhaustive list or set of guidelines around when and when not to add indexes?
There must be a size of table where it is inefficient to have indexes. There must be a limit to the number of indexes per table, or why not add an index to every column?
Any resources you can suggest would be very helpful.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不为每一列添加索引
索引可以加快查询速度,但会减慢插入和更新速度。找到最佳点部分取决于您的应用程序是否可能执行更多查询(例如图书馆目录)或更多更新(例如审核应用程序或日志)。
why not add an index to every column
Indexes speed up queries but can slow down inserts and updates. Finding the sweet spot will depend in part on whether your application is likely to do more queries (e.g. a library catalog) or more updates (e.g. an auditing application or log).
为您可能加入或排序的任何内容建立索引。在大多数情况下,多索引比少索引要好,因为大多数数据库涉及的读取多于写入,尤其是 Web 应用程序。如果您进行金融交易,情况会有所不同,但这是一个 Rails 应用程序。
Put an index on anything you're likely to join on or sort by. In most cases, it's better to err on the side of more indexes than less, simply because most databases involve more reads than writes, especially web applications. If you were doing financial transactions, it'd be different, but this is a Rails application.