为MySQL设计用户表

发布于 2025-01-03 06:43:47 字数 147 浏览 2 评论 0原文

我正在为我当前的项目设计一个用户表。我想在用户表中添加一列,在其中可以存储用户是否被删除的信息。我正在考虑仅添加一个日期时间列,因此如果设置了日期,则意味着用户已在该日期和时间被删除。其他常见的方法是添加额外的删除标志 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀お生予夺 2025-01-10 06:43:47

我会添加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.

屌丝范 2025-01-10 06:43:47

从性能和可扩展性的角度来看:

如果您对已删除的时间戳列建立索引,则随着应用程序的扩展,附加布尔列的存在不会产生太大影响。单列应该可以很好地工作,如果您厌倦了必须使用条件:

WHERE deleted_date IS NOT NULL

...始终确定哪些用户处于活动状态,您可能会考虑为此创建视图:

CREATE VIEW active_users AS
(
SELECT
  username,
  name,
  email
FROM users
WHERE deleted_date IS NOT NULL
);

从易用性的角度 来看-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:

WHERE deleted_date IS NOT NULL

... all the time to determine which users are active, you might consider just creating views for that:

CREATE VIEW active_users AS
(
SELECT
  username,
  name,
  email
FROM users
WHERE deleted_date IS NOT NULL
);

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.

生寂 2025-01-10 06:43:47

我会使用日期时间作为标志字段。如果您有很多用户,请使用非阻塞表更改: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文