Storing multiple choice values in database

前端 未结 9 1939
猫巷女王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 22:11

    Problems:

    1. You lose join capability (obviously).
    2. You have to reparse the list on each page load / post back. Which results in more code client side.
    3. You lose all pretenses of trying to keep database integrity. Just imagine if you decide to REMOVE a language later on... What's the sql going to be to fix all of your user profiles?
    4. Assuming your various profile options are stored in a lookup table in the DB, you still have to run "30 queries" per profile page. If they aren't then you have to code deploy for each little change. bad, very bad.
    5. Basing a design decision on something that "won't happen" is an absolute recipe for failure. Sure, the business people said they won't ever do that... Until they think of a reason they absolutely must do it. Today. Which will be promptly after you finish coding this.
    6. As I stated in a comment, 30 queries for a low use page is nothing. Don't sweat it, and definitely don't optimize unless you know for darn sure it's necessary. Guess how many queries SO does for it's profile page?
    0 讨论(0)
  • 2020-12-10 22:13

    You might not be missing anything now, but when you're requirements change you might regret that decision. You should store it normalized like your first instinct suggested. That's the correct approach.

    What you're suggesting is a classic premature optimization. You don't know yet whether that join will be a bottleneck, and so you don't know whether you're actually buying any performance improvement. Wait until you can profile the thing, and then you'll know whether that piece needs to be optimized.

    If it does, I would consider a materialized view, or some other approach that pre-computes the answer using the normalized data to a cache that is not considered the book of record.

    More generally, there are a lot of possible optimizations that could be done, if necessary, without compromising your design in the way you suggest.

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

    I generally stay away at the solution you described, you asking for troubles when you store relational data in such fashion.

    As alternative solution: You could store as one bitmasked integer, for example: 0 - No selection 1 - English 2 - Spanish 4 - German 8 - French 16 - Russian --and so on powers of 2

    So if someone selected English and Russian the value would be 17, and you could easily query the values with Bitwise operators.

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