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.
最初我想请你忘记哈希密码或与密码相关的w / e,这个问题与保护密码等无关,我知道/了解应该如何做。
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
表用户:id,用户名,密码,哈希,电子邮件,组,访问权限,地址,电话,父母,ts_created,ts_update
Multiple tables, for example:
多个表,例如:
Table users: id, username, password, hash, email, group, access, ts_created, ts_update
表用户: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
表用户的信息: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
例如新字段: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.
如果你想要真正的sql例子让我知道,我会废弃一些东西来更新它。
2 个解决方案
#1
5
You may need even more tables depending on the data your going to store:
根据您要存储的数据,您可能需要更多表:
- What if you use a password policy in the future where a user cannot re-use a previously used password?
- 如果您将来使用密码策略而用户无法重复使用以前使用过的密码,该怎么办?
- Can a user have more than one email?
- 用户可以拥有多个电子邮件吗?
- Can a user belong to more than one group?
- 用户可以属于多个组吗?
- Can a user have more than one phone number?
- 用户可以拥有多个电话号码吗?
- 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.
存储认为这可能值得存储在它自己的单独表中,这意味着将来它应该更容易维护。您需要考虑系统将如何变化。至于性能,如前所述,只要您创建正确的索引并正确使用数据库,它就不应成为问题。
#2
4
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.
您建议的新字段可能会作为新列进入person表。
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):
使用2个(或更多)表并将它们连接在一起不会显着减慢你的速度 - 它甚至可以提高性能(具有良好的索引 - user_id上的唯一索引将是一个良好的开端):
- on SELECT, the speed difference will be negligible
- 在SELECT上,速度差异可以忽略不计
- 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)
- 在INSERT / UPDATE中,在大多数情况下,这将比单个表更好(例如,如果“users”表有很多读取,则对人的写入不会阻止它们 - 而对于一个表,可能会发生这种情况)
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.
此外,我个人发现使用两个较窄的表而不是单个宽表更容易(在代码和数据库管理中)。
#1
5
You may need even more tables depending on the data your going to store:
根据您要存储的数据,您可能需要更多表:
- What if you use a password policy in the future where a user cannot re-use a previously used password?
- 如果您将来使用密码策略而用户无法重复使用以前使用过的密码,该怎么办?
- Can a user have more than one email?
- 用户可以拥有多个电子邮件吗?
- Can a user belong to more than one group?
- 用户可以属于多个组吗?
- Can a user have more than one phone number?
- 用户可以拥有多个电话号码吗?
- 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.
存储认为这可能值得存储在它自己的单独表中,这意味着将来它应该更容易维护。您需要考虑系统将如何变化。至于性能,如前所述,只要您创建正确的索引并正确使用数据库,它就不应成为问题。
#2
4
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.
您建议的新字段可能会作为新列进入person表。
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):
使用2个(或更多)表并将它们连接在一起不会显着减慢你的速度 - 它甚至可以提高性能(具有良好的索引 - user_id上的唯一索引将是一个良好的开端):
- on SELECT, the speed difference will be negligible
- 在SELECT上,速度差异可以忽略不计
- 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)
- 在INSERT / UPDATE中,在大多数情况下,这将比单个表更好(例如,如果“users”表有很多读取,则对人的写入不会阻止它们 - 而对于一个表,可能会发生这种情况)
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.
此外,我个人发现使用两个较窄的表而不是单个宽表更容易(在代码和数据库管理中)。