flag_shih_tzu可以处理的最大标志量是多少? [英] what is the maximum amount of flags flag_shih_tzu can handle?

查看:147
本文介绍了flag_shih_tzu可以处理的最大标志量是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用flag_shih_tzugem,我想知道它可以处理的最大标志量是多少,还是取决于int。长度在标志列?

我需要它来处理64个标志。

可以吗?

解决方案我是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]

如果您还想在同一查询中检查其他标志,则应将条件连锁在一起(按照优化方式),如下所示:

< pre $
Foo.chained_flags_with(:not_is_d16,:is_d1,:is_d4,:not_is_d11,:is_d14)#=>符合条件的Foo对象数组$

现在为巨大的警告!如果您想一起使用这4列,它们需要位于SQL WHERE子句的不同部分,并因此处于不同的活动记录关系中。



重要

链式标记只能与来自同一列的标记链接。


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个标志,并将我的标志分成我需要的任意列。



建议:组合标志在同一列上一起查询,以充分利用按位算术。


I'm using "flag_shih_tzu" gem and I want to know what is maximum amount of flags that it can handle, or is it depend on the int. length in the flags column?
I need it to handle 64 flags.
can it?

解决方案

I am the maintainer of flag_shih_tzu.

Best Practice: Each column used for flags should have at most 16 flags set, for performance reasons. You will find that performance suffers too much with columns holding more than 16 flags.

Workaround: A single table can have multiple flag columns.

I would create a design as follows:


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

Now when you have an instance of Foo:


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

Now you can retrieve foo like this:


Foo.not_is_d16 # => [foo]

And if you want to also check other flags in the same query you should chain conditions together (in a bitwise optimized manner) as follows:


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

Now for the giant caveat! If you want to use the 4 columns together they will need to be in separate parts of the SQL WHERE clause, and thus in different active record relations.

Important Chained flags can only be chained with flags from the same column.


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

Personally, I never go past 8 flags per column, and split my flags into as many columns as I need.

Recommendation: Combine flags for properties which will be queried together on the same column, to make best use of the bitwise arithmetic.

这篇关于flag_shih_tzu可以处理的最大标志量是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆