Database design - boolean attribute or new table

后端 未结 4 1820
太阳男子
太阳男子 2021-01-20 17:53

Let\'s say I have a database table that I want to filter based on a boolean attribute (for example, \"flagged\" attribute). Is it better to just add a \"flagged\" attribute

4条回答
  •  时光说笑
    2021-01-20 18:49

    If that is all you need1, then just add the simple field.

    But, you'll have to be careful about how you index it. Unless the values are seriously skewed2, you'll end-up with an index with a horrible selectivity and clustering factor3, and you'll actually be better off doing full table scans4.

    If you filter by the flag in conjunction with other fields, make a composite index, which will then have a much better chance of being decently selective.


    1 I.e. you don't need additional data that somehow "describes" or "augments" each of the two possible Boolean values.

    2 One value is in deep minority (the number of rows containing false is much smaller than the number of rows containing true, or vice verse), and you happen to filter on just that value.

    3 The link is for Oracle, but the general principle applies to all DBMSes.

    4 Which a decent DBMS will do for you automatically, even in the presence of the index. A hypothetical "stupid" DBMS would just blindly use the index and perform even worse than the full table scan.

提交回复
热议问题