简体   繁体   English

使用Rails和Mongoid为Heroku索引MongoDB

[英]Indexing a MongoDB on Heroku, with Rails and Mongoid

I have a Rails app running Mongoid on Heroku and I need to set up some indexes to make the database faster. 我有一个在Heroku上运行Mongoid的Rails应用程序,我需要设置一些索引来使数据库更快。 I've tried to read about it several places and I know Mongoid has some built-in functions for indexing, but I'm not sure on what to apply them and how often to index. 我试过在几个地方读过它,我知道Mongoid有一些用于索引的内置函数,但我不确定应用它们的内容以及索引的频率。

It is mostly my Design-model I want to index: 它主要是我想要索引的设计模型:

scope :full_member_and_show, where(full_member: true).and(show: true).desc(:created_at)
scope :not_full_member_and_show, where(full_member: false).and(show: true).desc(:created_at)

embeds_many :comments
belongs_to :designer

search_in :tags_array

attr_accessible :image,  :tags, :description, :title, :featured, :project_number, :show



field :width
field :height
field :description
field :title
field :tags, type: Array
field :featured, :type => Boolean, :default => false
field :project_number, :type => Integer, :default => 0
field :show, :type => Boolean, :default => true
field :full_member, :type => Boolean, :default => false
field :first_design, :type => Boolean, :default => false

What do I need to index, how exactly do I do it with Mongoid and how often should I do it? 我需要索引什么,我如何使用Mongoid完成它以及我应该多久进行一次?

ERROR UPDATE 错误更新

If try to index the below: 如果尝试索引以下内容:

index({ full_member: 1, show: 1 }, { unique: true })

It throws me this error: 它抛出了这个错误:

Invalid index specification {:full_member=>1, :show=>1}; should be either a string, symbol, or an array of arrays.

You don't need to index periodically: once you've added an index, mongo keeps that index up to date as the collection changes. 您不需要定期编制索引:一旦添加了索引,mongo会在集合更改时使该索引保持最新。 This is the same as an index in MySQL or Postgres (you may have been thinking of something like solr) 这与MySQL或Postgres中的索引相同(您可能一直在考虑像solr这样的东西)

What to index depends on what queries you'll be making against your dataset. 索引内容取决于您对数据集的查询。 Indexes do carry some overhead when you do updates and consume disk space so you don't want to add them when you don't need them. 当您进行更新并占用磁盘空间时,索引会带来一些开销,因此您不希望在不需要时添加它们。

You tell mongoid what indexes you want by index, for example 例如,您可以通过索引告诉mongoid您想要的索引

class Person
  include Mongoid::Document
  index :city
end

There are loads of examples in the mongoid docs for the various kinds of indexes mongo supports. mongoid docs中有很多关于mongo支持的各种索引的例子。

Then you run 然后你跑

rake db:mongoid:create_indexes

This determines what indexes you want (based in the calls to index in your model) and then ensures that they exist in the db, creating them if necessary. 这决定了您想要的索引(基于对模型中index的调用),然后确保它们存在于db中,并在必要时创建它们。 In development you'd run this after adding indexes to your models. 在开发中,您将在为模型添加索引后运行此操作。 In production it makes sense to run this as part of your deploy (you only need to if you've added indexes since the last deploy but it's way easier to just do it systematically) 在生产中,将其作为部署的一部分运行是有意义的(您只需要在上次部署后添加索引,但系统地更容易实现)

There's a lot of information about how mongo uses indexes in the documentation 有很多关于mongo如何在文档中使用索引的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM