为MySQL设计用户表
我正在为我当前的项目设计一个用户表。我想在用户表中添加一列,在其中可以存储用户是否被删除的信息。我正在考虑仅添加一个日期时间列,因此如果设置了日期,则意味着用户已在该日期和时间被删除。其他常见的方法是添加额外的删除标志 1 或 0。从性能/可扩展性的角度来看,我是否需要删除标志?
I'm designing a user table for my current project. I want to add a column to user table where I can store information whether user was deleted or not. I'm thinking about adding only one datetime column so if date is set that means user was deleted at this date and time. Other common approach is to add additional deletion flag 1 or 0. Do I need deletion flag looking at it from performance/scalability perspective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会添加deleteFlag 和lastUpdate 列。每当deleteFlag值更改时,您都可以将lastUpdate列设置为NOW()。这样您就会知道用户是否被删除/取消删除以及何时被删除/取消删除。添加两个这样的列不应影响性能。
I would add the deleteFlag and also a lastUpdate column. Whenever the deleteFlag value changes you can set the lastUpdate column to
NOW()
. So you will know if the user is deleted/undeleted and when. Adding two such columns should not affect performance.从性能和可扩展性的角度来看:
如果您对已删除的时间戳列建立索引,则随着应用程序的扩展,附加布尔列的存在不会产生太大影响。单列应该可以很好地工作,如果您厌倦了必须使用条件:
...始终确定哪些用户处于活动状态,您可能会考虑为此创建视图:
从易用性的角度 来看-use
我可能会使用额外的布尔列只是为了使查询更整洁。但是,它会增加应用程序代码的一些复杂性,以记住更新这两列。或者,您可以创建一个触发器来设置布尔值更新为
1
时的时间戳。From the perspective of performance and scalability:
The presence of an additional boolean column shouldn't make much difference as your application scales if you index your deleted timestamp column. The single column should work out just fine, and if you get tired of having to use the condition:
... all the time to determine which users are active, you might consider just creating views for that:
From the perspective of ease-of-use
I would probably use the additional boolean column just to make querying a little tidier. However, it adds a little complexity in your application code to remember to update both columns. Alternatively, you can create a trigger to set the timestamp when the boolean is updated to
1
.我会使用日期时间作为标志字段。如果您有很多用户,请使用非阻塞表更改:oak-online-alter-table
I would use datetime as flag field. And if you have a lot of users, use non-blocking table altering: oak-online-alter-table