Storing multiple choice values in database

前端 未结 9 1938
猫巷女王i
猫巷女王i 2020-12-10 21:24

Say I offer user to check off languages she speaks and store it in a db. Important side note, I will not search db for any of those values, as I will have some separate sear

相关标签:
9条回答
  • 2020-12-10 21:49

    Premature optimization is the root of all evil.

    EDIT: Apparently the context of my observation has been misconstrued by some - and hence the downvotes. So I will clarify.

    Denormalizing your model to make things easier and/or 'more performant' - such as creating concatenated columns to represent business information (as in the OP case) - is what I refer to as a "premature optimization".

    While there may be some extreme edge cases where there is no other way to get the necessary performance necessary for a particular problem domain - one should rarely assume this is the case. In general, such premature optimizations cause long-term grief because they are hard to undo - changing your data model once it is in production takes a lot more effort than when it initially deployed.

    When designing a database, developers (and DBAs) should apply standard practices like normalization to ensure that their data model expresses the business information being collected and managed. I don't believe that proper use of data normalization is an "optimization" - it is a necessary practice. In my opinion, data modelers should always be on the lookout for models that could be restructured to (at least) third normal form (3NF).

    0 讨论(0)
  • 2020-12-10 21:53

    You seem to be extremely worried about adding in a few extra lookup table joins. In my experience, the time it takes to actually transmit the HTML response and have the browser render it far exceed a few extra table joins. Especially if you are using indexes for your primary and foreign keys (as you should be). It's like you are planning a multi-day cross-country trip and you are worried about 1 extra 10 minute bathroom stop.

    The lack of long-term flexibility and data integrity are not worth it for such a small optimization (which may not be necessary or even noticeable).

    0 讨论(0)
  • 2020-12-10 21:56

    If you're not querying against them, you don't lose anything by storing them in a form like your initial plan. If you are, then storing them in the comma-delimited format will come back to haunt you, and I doubt that any speed savings would be significant, especially when you factor in the work required to translate them back.

    0 讨论(0)
  • 2020-12-10 21:57

    This type of storage has almost ALWAYS come back to haunt me. For one, you are not even in first normal form. For another, some manager or the other will definitely come back and say.. "hey, now that we store this, can you write me a report on... "

    I would suggest going with a normalized design. Put it in a separate table.

    0 讨论(0)
  • 2020-12-10 21:59

    Nooooooooooooooooo!!!!!!!!

    As stated very well in the above few posts.

    If you want a contrary view to this debate, look at wordpress. Tables are chocked full of delimited data, and it's a great, simple platform.

    0 讨论(0)
  • 2020-12-10 22:03

    Don't.

    • You don't search for them now
    • Data is useless to anything but this one situation
    • No data integrity (eg no FK)
    • You still have to change to "English,German" etc for display
    • "Give me all users who speak x" = FAIL
    • The list is actually a presentation issue

    It's your system, though, and I look forward to answering the inevitable "help" questions later...

    0 讨论(0)
提交回复
热议问题