views:

72

answers:

2

Hi,

Initially I would like to ask you to forget about hashing passwords or w/e related to passwords, this question is not related to securing passwords, etc and I do know/understand how this should be done.

What is the best approach to store the data in question, considering performance on read/write - to build one or more tables?

Single table, for example:

Table users: id, username, password, hash, email, group, access, address, phone, parents, ts_created, ts_update

Multiple tables, for example:

Table users: id, username, password, hash, email, group, access, ts_created, ts_update

Table user's information: id, user_id, address, phone, parents, ts_created, ts_update

What if your user's information fields may grow along the time - how should you deal with it ?

For example new fields: birthday_date, comments, situation

Will having 2 tables be slower on queries than having a single table ?

If having multiple tables in this case is only for maintaining a good design with separated data, does that mean it is not useful at all for performance reasons ?

If you want real sql examples let me know and I will scrap something to update this.

+1  A: 

Your multiple-table design looks sensible - one table contains data about the user, the other about the person; if you only need user data (e.g. for checking access rights), person data are irrelevant.

The new fields you propose would probably go into the person table as new columns.

Using 2 (or more) tables and joining them together won't slow you down significantly - it may even improve performance (with good indexing - a unique index on user_id would be a good start here):

  • on SELECT, the speed difference will be negligible
  • on INSERT/UPDATE, this will be better than a single table in most situations (e.g. if the "users" table has many reads, writes on persons won't block them - whereas with one table, it might happen)

Also, personally I find it much easier (both in code and in db administration) to work with two narrower tables than with a single wide table.

Piskvor
Thanks quiet handfull information, i see your point about the locks and everything but i wonder were would i ever get to see something like this happenning i mean over how many access would it start showing up as an issue ? Once again thanks a lot that helps me in the way that i will keep using my multiple tables styling ehhe :)
Prix
Well, that depends on many things - the actual queries, their frequency, even the server's hardware. In one case, we had about 100 reads/sec and 1 write/2 sec on this table (in addition to other table load); this was sufficient to cause noticeable slowdown; after changing the design to multiple tables, the performance improved.
Piskvor
+2  A: 

You may need even more tables depending on the data your going to store:

  1. What if you use a password policy in the future where a user cannot re-use a previously used password?
  2. Can a user have more than one email?
  3. Can a user belong to more than one group?
  4. Can a user have more than one phone number?
  5. Only one parent? Or two? Is the parent in the system? What information about the parent do you store?

Storing thinks like this may be worth to store in its own individual table, which means that in the future it should be a lot easier to maintain. You need to think of how the system will change. As for performance, as already outlined it shouldn't be a problem as long as you create the correct indexes, and use the database correctly.

BenW
Thanks. Well the tables i proposed in the Question were samples only the main idea was to understand better when you should split and have multiple tables against where it would or not affect the performance. I usually make a layout of my database before making anything else and usually use multiple tables but i've seen around many softwares and webapps that still prefer using a singles tables ... where it got me thinking wether i should keep my style to maintainbility or not for performance also from time to time i benchmark everything to see how it goes ... heheh im creppy :(
Prix
What if your information column keeps growing with sometimes even odd things ? would you go for a new table ? for example: table A users credential table B users information (id, user_id, address, phone, parents, birthday_date, comments, situation, ts_created, ts_update) then you get new fields (special food, best movie, best car, women type) i mean they are users information but sort of unrelated to the main users information would you go for a new table like users personal info or something alike or keep it with the table users informations ? how big would go or have u got with the columns
Prix
Wouldn't worry about splitting tables up, as long as you index correctly, and normalize your database it shouldn't be an issue. If anything it should help some of the more complicated queries you may use. Generally you and others working on it will find it much easier if you normalize the database properly and follow the 'book', single tables can very quickly get out of hand!
BenW
Once again the fields names are just sample you can expose it any way you prefer ... heheh i just found it easy to use a person as sample ...
Prix
Yeah always best to use simple examples :)As for your 2nd comment, it depends what the odd things are, in that case I may be inclined to add a UserPreferences table, but really... you should only add tables if the data may be duplicated, hence emails, addresses, phone numbers, if there is a chance you can have more than one per user.
BenW
BenW could you possible update your post with some good pratice of database normalization ? i guess with that your answer will be most likely my choice i wish to mark both since they had good info but yours helped me the most with this... Once again thanks a lot both of u, BenW and Piskvor :)
Prix
There are plenty of tutorials, guides, books out there about normalisation that will help a lot more than I could explain in a comment!. Best bet is to use Google and find something that makes sense to you, hope this helps
BenW