[英]Sum one table and update other with result, or just do `sum` on select?
Might be a dummy question, but I'm creating a system in which users are not allowed to exceed X mounts of kb in pictures they can upload.可能是一个愚蠢的问题,但我正在创建一个系统,在该系统中,用户不能在他们可以上传的图片中超过 X 个 kb 安装量。
When uploading a picture I update the images
table with the size of the image in KB and other info.上传图片时,我会使用图片大小(以 KB 为单位)和其他信息更新
images
表。
Now, should I also keep track of the total size of each users images on the users
table?现在,我还应该跟踪
users
表上每个用户图像的总大小吗? Or should I just do a select sum(size) from images where user = xxx
every time I want to check the limit?或者我应该
select sum(size) from images where user = xxx
每次我想检查限制时select sum(size) from images where user = xxx
? Which might with every new upload?每次新上传可能会出现哪些情况?
What would be the best approach from a relational point of view?从关系的角度来看,最好的方法是什么?
You can use either method.您可以使用任一方法。
However, because you have a business rule related to the sum of the sizes, I might suggest that you use triggers to maintain the sum at the user level.但是,因为您有一个与大小总和相关的业务规则,我可能建议您使用触发器来维护用户级别的总和。 Although this has some additional overhead for
insert
s/ update
s/ delete
s, it has much less overhead when returning information about a user.虽然这对于
insert
s/ update
s/ delete
s 有一些额外的开销,但在返回有关用户的信息时它的开销要小得多。
This has a few other advantages as well:这还有其他一些优点:
users
table (well, you can do this in the most recent versions of MySQL).users
表中实现检查约束(好吧,您可以在最新版本的 MySQL 中执行此操作)。Storing the SUM in the users
table is one type of denormalization.将 SUM 存储在
users
表中是一种非规范化。
This can be worthwhile if you need to query the sum frequently, and it's too slow to do the aggregate query every time you need it.如果您需要经常查询总和,并且每次需要时都进行聚合查询太慢,那么这可能是值得的。
But you accept the risk that the stored sum in the users
table will become out of sync with the real SUM(size)
of the associated images.但是您接受这样的风险,即
users
表中存储的总和将与相关图像的实际SUM(size)
不同步。
You wouldn't think this would be difficult, but in practice, there are lots of edge case where the stored sum fails to be updated.您不会认为这会很困难,但在实践中,存在许多边缘情况,即存储的总和无法更新。 You will end up periodically running the aggregate query in the background, to overwrite the stored sum, just in case it has gotten out of sync.
您将最终在后台定期运行聚合查询,以覆盖存储的总和,以防万一它不同步。
Denormalization is more work for you as a coder, because you have to write code to correct for anomalies like that.作为一名编码人员,非规范化对你来说是更多的工作,因为你必须编写代码来纠正这样的异常。 Be conservative about how many cases of denormalization you create, because each one obligates you to do more work.
对于您创建的非规范化案例的数量要保守,因为每个案例都要求您做更多的工作。
But if it's very important that your query for the sum return the result faster than is possible by running the aggregate query, then that's what you have to do.但是,如果您对总和的查询比运行聚合查询更快地返回结果非常重要,那么这就是您必须做的。
In my experience, all optimizations come with a price.根据我的经验,所有优化都是有代价的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.