When is it better to store flags as a bitmask rather than using an associative table?

后端 未结 9 973
滥情空心
滥情空心 2020-12-02 09:31

I’m working on an application where users have different permissions to use different features (e.g. Read, Create, Download, Print, Approve, etc.). The list of permissions i

相关标签:
9条回答
  • 2020-12-02 10:16

    Your queries will run faster using a flags enumeration (bitmask), because you won't need to include a join to the associated table in order to make sense of the value.

    0 讨论(0)
  • 2020-12-02 10:19

    Personally, I would use an associative table.

    A bitmask field is very difficult to query and join on.

    You can always map this to your C# flags enum and if performance becomes and issue refactor the database.

    Readability over premature optimization ;)

    0 讨论(0)
  • 2020-12-02 10:29

    There is no definitive answer, so do what works for you. But here is my catch:

    Use option 1 if

    • You expect permissions to grow to many
    • If you might need to do a permission check in the database stored procedures itself
    • You do not expect millions of users so that records in the table do not grow massively

    Use option 2 if

    • Permissions are going to be limited to handful
    • You expect millions of users
    0 讨论(0)
提交回复
热议问题