flag_shih_tzu可以处理的最大标志数量是多少?

时间:2013-08-07 09:20:23

标签: ruby-on-rails ruby gem

我正在使用“flag_shih_tzu”gem,我想知道它可以处理的最大标志数量是多少,还是取决于int。标志栏中的长度?
我需要它来处理64个标志 可以吗?

1 个答案:

答案 0 :(得分:6)

我是flag_shih_tzu的维护者。

最佳实践:出于性能原因,用于标志的每列最多应设置16个标志。你会发现,当列数超过16个时,性能会受到太大影响。

解决方法:单个表可以有多个标志列。

我会按如下方式创建一个设计:


class Foo ...

  has_flags 1 => :is_a1,
            # ... snip ...
            16 => :is_a16,
            :column => 'flag_col_a'

  has_flags 1 => :is_b1,
            # ... snip ...
            16 => :is_b16,
            :column => 'flag_col_b'

  has_flags 1 => :is_c1,
            # ... snip ...
            16 => :is_c16,
            :column => 'flag_col_c'

  has_flags 1 => :is_d1,
            # ... snip ...
            16 => :is_d16,
            :column => 'flag_col_d'
end

现在有了Foo的实例:


foo = Foo.new
foo.is_d16 = false
foo.save

现在你可以像这样检索foo:


Foo.not_is_d16 # => [foo]

如果您还要检查同一查询中的其他标志,则应将条件链接在一起(按位优化方式),如下所示:


Foo.chained_flags_with(:not_is_d16, :is_d1, :is_d4, :not_is_d11, :is_d14) # => array of Foo objects matching the conditions

现在为巨人警告!如果要将4列一起使用,则需要将它们放在SQL WHERE子句的不同部分中,从而处于不同的活动记录关系中。

重要链式标志只能与同一列中的标记链接。


Foo.
  chained_flags_with(:not_is_a1, :is_a2).  # from flag_col_a
  chained_flags_with(:not_is_b3, :is_b4).  # from flag_col_b
  chained_flags_with(:not_is_c8, :is_c11). # from flag_col_c
  chained_flags_with(:not_is_d13, :is_d14) # from flag_col_d

就个人而言,我从不会超过每列8个标志,并将我的标志分成尽可能多的列。

推荐:组合将在同一列上一起查询的属性的标志,以充分利用按位算法。