存储用户设置 - 什么错用和QUOT;旗帜与QUOT;或QUOT;位"而不是一堆的bool的? [英] Storing User Settings - anything wrong with using "Flags" or "Bits" instead of a bunch of bools?

查看:173
本文介绍了存储用户设置 - 什么错用和QUOT;旗帜与QUOT;或QUOT;位"而不是一堆的bool的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的用户设置为我的MVC应用程序,而现在我有20〜布尔设置,用户可以切换。由于每个用户将总是有每个设置,我在想储存每个设置作为用户表一个布尔值。虽然作为应用需求的增长,这将变得笨拙。

I'm designing the User Settings for my MVC application, and right now I have ~20 boolean settings that the user can toggle. Since every user will always have every setting, I was thinking about storing each setting as a boolean on the User table. Though this would become unwieldy as the application requirements grow.

第一个问题 - 是有什么问题在这种情况下有你的桌子上一吨的列

First question - is there anything wrong with having a ton of columns on your table in this situation?

那么我认为在一个阵列使用标志,并存储设置一个比特的每个:

I then considered using Flags, and storing the settings as one bit each in an array:

[Flags]
public enum Settings
{
    WantsEmail = 1,
    WantsNotifications = 2,
    SharesProfile = 4,
    EatsLasagna = 8
}

和然后每个用户将在其用户行一个单一的设置列中,存储2 ^ 20的值,如果有20个设定。

And then each user will have a single "Settings" column in their User row, that stores a value of 2^20, if there are 20 settings.

我想用这个来指导我的努力:什么请问[国旗]枚举属性均在C#?

I'd use this to guide my efforts: What does the [Flags] Enum Attribute mean in C#?

这是比前一种方法更好?任何建议都欢迎。

Is this better than the former approach? Any suggestions are welcome.

推荐答案

这取决于什么应该考虑的原子从数据管理的角度来看。

It depends on what should be considered atomic from the data management perspective.


  • 如果你总是搜索,读取和/到数据库编写的所有设置在一起,那么整个组设置可以被认为是原子,并且可以一起存储在数据库中。

  • 然而,如果你不需要做任何的那些事情上设置的(如设置只是一个标志,而无需修改等),那么他们是不是原子(从数据管理的角度来看),所以一起将它们存储在同一个数据库中的字段会违反原子的原则,因此1NF。

  • If you always search for, read and write all the settings together from/to the database, then the whole set of settings can be considered atomic and can be stored together in the database.
  • However, if you need to do any of those things on a subset of settings (e.g. set just one flag without modifying others), then they are not atomic (from the data management perspective), so storing them together in the same database field would violate the principle of atomicity and therefore the 1NF.

请注意,某些数据库管理系统(如MS SQL Server)的是在存储布尔(每布尔字段只有一个位在理想情况下)非常有效。即使是那些不完美通常不会花每布尔多个字节(甲骨文,我看着你),这可能会成为一个问题,只有当你有用户数百万或数十亿美元。

Please note that some DBMSes (such as MS SQL Server) are very efficient at storing Booleans (just one bit per Boolean field under ideal circumstances). Even those that are less than perfect will typically not spend more than one byte per Boolean (Oracle, I'm looking at you), which may become a problem only if you have millions or billions of users.

这篇关于存储用户设置 - 什么错用和QUOT;旗帜与QUOT;或QUOT;位"而不是一堆的bool的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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