【问题标题】:Can DataMapper properties appear in multiple composite indexes?DataMapper 属性可以出现在多个复合索引中吗?
【发布时间】:2010-01-26 21:09:09
【问题描述】:

我发现这个问题已经在 DataMapper 的Ticket #58 中讨论过,显然早在 2007 年,但我在最新版本 (dm-core-0.10.2) 中找不到如何解决。我想定义两个复合索引,每个索引都部分基于某个属性。我希望我能做到这一点......

class Stat
  include DataMapper::Resource
  property :id,            Serial,
  property :collected_on,  Integer #yyyyMMddhhmm
  property :measure,       Integer
  property :dimension_one, Integer
  property :dimension_two, Integer
  property :source_id,     Integer
  index [:collected_on, :dimension_one, :dimension_two]
  index [:source_id, :collected_on]
end

正确的做法是什么?

【问题讨论】:

    标签: ruby indexing datamapper composite-index


    【解决方案1】:

    你可以这样做:

    class Stat
      include DataMapper::Resource
      property :id,            Serial,
      property :collected_on,  Integer, :index => [ :index_one, :index_two ]
      property :measure,       Integer
      property :dimension_one, Integer, :index => :index_one
      property :dimension_two, Integer, :index => :index_one
      property :source_id,     Integer, :index => :index_two
    end
    

    当然,您可以根据自己的喜好制作索引。索引可以是Array,或Symbol,如上所示,或者如果您想将属性单独放入索引中并且您不关心索引的名称,甚至可以只是true

    【讨论】:

    • 谢谢!我想这可以在我给出的具体示例中起作用,只要我将 :source_id 属性移到 :collected_on 属性上方,这样 :index_two 将是 [:source_id,:collected_on] 而不是 [:collected_on,:source_id].. . 但是,如果您需要两个订单的索引,您会怎么做?
    • 对于唯一索引,使用 :unique_index => 代替 :index =>。隐藏良好的文档可以在here找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多