数据库设计,最佳实践,需要建议

发布于 2024-11-03 07:47:01 字数 338 浏览 1 评论 0原文

我有这个数据库,有一个名为“徽章”和“主题”的表,我在“主题”中有一个名为“is_canceled”布尔值的标志字段,现在出于某种原因有人建议我在“徽章”中有一个名为“ cancel_subject_count”,它与他所说的性能原因之一具有相同的值

"SELECT SUM(is_canceled) as cancel_count FROM subjects WHERE is_canceled = 1 AND badge_id = ".$some_badge_id

,最好直接取该值,而不是先对其进行求和,

你们觉得怎么样?我应该有这个字段吗?为什么?

谢谢

i have this database, with a table called "badges" and "subjects", i have a flag field in "subject" called "is_canceled" bool value, now for some reason someone suggested me to have a field in "badges" called "canceled_subject_count", which has the same value as

"SELECT SUM(is_canceled) as cancel_count FROM subjects WHERE is_canceled = 1 AND badge_id = ".$some_badge_id

one of the reason he said is performance, it is better to take the value straight rather having it to sum first

what do you guys think? should I have this field? and why?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别念他 2024-11-10 07:47:01

这称为非规范化。在初始数据库设计中这样做可能会为您节省以后的时间和精力,但很可能这是一个不成熟的优化。您最好添加 canceled_subject_count 字段,如果经过一些分析后,您在计算徽章的取消主题时发现测量到的性能瓶颈,那么您可以添加场地。

This is called denormalization. Doing this for the initial database design might save you time and effort later on, but chances are that it's a premature optimization. You're probably better off not adding the canceled_subject_count field and if after some profiling, you find a measured performance bottleneck when counting cancelled subjects for a badge, then you can add the field.

画中仙 2024-11-10 07:47:01

这实际上取决于软件的使用。这种优化在很大程度上取决于值更改的频率、有多少人正在查看它等。

如果它很少更改,则存储值并在发生更改时更新可能更明智。

如果总和不断变化,那么在每个查询中计算它会更聪明。

但除非您要满足大量请求,否则这笔费用几乎不会花费您任何费用,并且可能更容易保持数据完整性良好。

It really depends on the use of the software. This kind of optimization depends a lot on how often the value changes, how many people are viewing it, etc.

If it rarely changes, storing the value and updating when changes occur is probably smarter.

If the sum is changing constantly, it would be smarter just to calculate it with each query.

But unless you're serving to a massive amount of requests, this sum will cost you almost nothing and is probably easier to keep your data integrity in good shape.

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