[英]Database Structure - two tables or one table?
I have one database table dealing with users login totals and another table dealing with individual login sessions. 我有一个处理用户登录总数的数据库表,另一个处理单个登录会话的表。 Should I keep these tables separate or should I go ahead and merge them? 我应该将这些表分开放置还是应该合并它们?
users_logins
users_id
successful_logins(total)
last_online
users_logins_sessions
users_id
session_id
ip_address
user_agent
last_activity(time-stamp)
This really depends on you, however I understand it as (making assumption here) that sessions are cleared? 这确实取决于您,但是我理解为(在此处进行假设)会话已清除? Typically in my applications sessions expire, and a new one is created, I am not sure how you manage that in your users_logins_sessions
as you don't give much more info on this, it could work either way. 通常,在我的应用程序会话中,会话会过期,并且会创建一个新会话,我不确定您如何在users_logins_sessions
管理,因为您没有为此提供更多信息,因此无论哪种方法都可以。
You should merge if your 'session' table never deletes entries, OR leave it alone the way it is, if the sessions expire / are deleted at intervals. 如果“会话”表从不删除条目,则应该合并;如果会话到期/每隔一段时间删除一次,则应按原样保留它。
I am also assuming the users_id
is used somewhere else if you keep them separate. 我还假设,如果您将users_id
使用, users_id
在其他地方使用。
You could lose user_logins, as I assume last_online and last_activity contain same value. 您可能会丢失user_logins,因为我假设last_online和last_activity包含相同的值。
You would however have to query the user_logins_sessions table to get the total for successful logins for a given user. 但是,您将不得不查询user_logins_sessions表以获取给定用户成功登录的总数。
SELECT COUNT(user_id) FROM user_login_sessions WHERE user_id = ?
If you only have the users_logins_sessions table, you can easily query for successful_logins and last_online. 如果只有users_logins_sessions表,则可以轻松查询success_logins和last_online。
SELECT COUNT(1) AS successful_logins
FROM users_logins_sessions
WHERE users_id = <user_id>;
SELECT MAX(last_activity) AS last_online
FROM users_logins_sessions
WHERE users_id = <user_id>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.