1

I'm planning to develop a PHP Web App, it will mainly be used by registered users(sessions)

While thinking about the DB design, I was contemplating that in order to give the best user experience possible there would be lots of options for the user to activate, deactivate, specify, etc.

For example:
- Options for each layout elements, dialog boxes, dashboard, grid, etc.
- color, size, stay visible, invisible, don't ask again, show everytime, advanced mode, simple mode, etc.

This would get like 100s of fields ranging from simple Yes/No or 1 to N values..., for each user.

So, is it having a field for each of these options the way to go?
or how do those CRMs or CMS or other Web Apps do it to store lots of 1-2 char long values?

Do they group them on Text fields separated by a special char and then "explode" them as an array for runtime usage?

thank you

2
  • Always think readability and maintainabilty for you and others in the future. Imagine a system with a hundred fields that takes 4 hours to set up. Sounds like a lot? Not really if changing any indvidual field or setup is a snap in the future. Now imagine a system that has extracted everything to 'key,value'. May be harder for some to maintain under the hood. It all depends on your needs for the application (how big, how long is it's life-span, etc.) Commented Nov 14, 2011 at 1:49
  • This is also a web app and unless you're writing google V2 or facebook V2 I doubt the gains in speed from things like bit solutions would be worth the abstraction. Commented Nov 14, 2011 at 1:51

3 Answers 3

4

How about something like this:

CREATE TABLE settings (
    user_id INT,
    setting_name VARCHAR(255),
    setting_value CHAR(2)
)

That way, to store a configuration setting for a user, you can do:

INSERT INTO settings (user_id, setting_name, setting_value),
    VALUES (1, "timezone", "+8")

And when you need to query a setting for a particular user, you can do:

SELECT setting_value FROM settings
    WHERE user_id = 1 AND setting_name = "timezone"
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm..., I like how this idea looks, seems versatile and clear. A table of settings values, nice, I'll get back in a moment to check in detail all the answers, thank you.
+1. This is sometimes called entity-attribute-value (EAV) or "table of tables" and is considered an anti-pattern by many people. In my view, however, there is no other sensible way to handle this type of situation. The values are independent and have no relational semantics. EAV has the particular advantage for this scenario of being much more agile should new types of settings be required. No table schema changes are needed, only new rows.
2

I would absolutely be inclined to have individual fields for each option. My rule of thumb is that each column holds exactly one piece of data whenever possible. No more, no less. As was mentioned earlier, the ease of maintenance and the ability to add / drop options down the road far outweighs the pain in the arse of setting it up. I would, however, put some thought into how you create the table(s). The idea mentioned earlier was to have a Settings table with 100 columns ( one for each option ) and one row for each user. That would work, to be sure. If it were me I would be inclined to break it down a bit further. You start with a basic User table, of course. That would hold the basics of username, password, userid etc. That way you can use the numeric userid as the key index for your Settings table(s). But after that I would try to break down the settings into smaller tables based on logical usage. For example, if you have 100 options, and 19 of those pertain to how a user views / is viewed / behaves in one specific part of the site, say something like a forum, then break those out into a separate table ie ForumSettings. Maybe there are 12 more that pertain to email preferences, but would not be used in other areas of the site / app. Now you have an EmailSettings table. Doing this would not only reduce the number of columns in your generic Settings table, but it would also make writing queries for specific tasks or areas of the app much easier, speed up the performance a tick, and make maintenance moving forward far less painful. Some may disagree as from a strictly data modeling perspective I'm pretty sure that the one Settings table would be indicated. But from a real world perspective, I have never gone wrong using logical chunks such as this.

Comments

0

From a pure data-model perspective, that would be the clearest design (though awful wide). Some might try to bitmask them into a single field for assumed space reasons, but the logic to encode/decode makes that not worthwhile, in my opinion. Also you lose the ability to index on them. Another option (I just saw posted) is to hold a separate table with an FK back to the user table. But then you have to iterate over the results to get the value you want to check for.

1 Comment

As @George Edison noted, the 'settings' table is an idea. And in his query he shows that indexes could work there too, if you want to check merely if that user has certain flags via a query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.