在数据库中存储主动更新的字段

发布于 2024-10-07 15:57:16 字数 442 浏览 3 评论 0原文

我有一个数据库,其中一个表存储文章。对于每一篇文章,我都需要存储其被查看的次数。现在,我的想法是将此值存储在数据库中,并在每次有人访问包含该文章的页面时更新它,我现在正在考虑两种替代方案:

  1. 在表 articles 中创建一个列 views并更新它。
  2. 创建一个单独的表 view_count,其中包含指向文章的 FK 指针以及该文章的浏览次数。

我的问题是:这两种方法在速度方面有什么区别吗?为什么?还有更好的选择吗?

我使用的数据库是 PostgreSQL。

注意,我没有考虑在数据库外部的单独文件中累积这些值并稍后插入的不同方式,因为 i) 我们没有获得尽可能多的点击 ii) 次要功能,如果它会需要这样的麻烦,我们宁愿放弃它并使用其他一些服务。

I have a database with one of the table storing articles. For every article I need to store the number of times it was viewed. Now, my idea is to store this value in the database and update it every time someone visits the page with that article and I am considering two alternatives now:

  1. Create a column views in the table articles and update it.
  2. Create a separate table view_count with FK pointer to the article and the number views for that article.

My question is: is there any difference between these two approaches in terms of speed and why? Are there any better alternatives?

The database I use is PostgreSQL.

Note, that I am not considering the different ways of accumulating those values in separate files, outside of database and later inserting because i) we do not get as many hits ii) the feature in minor and if it would require such a hassle we would rather abandon it and use some other service for that.

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

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

发布评论

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

评论(3

沉溺在你眼里的海 2024-10-14 15:57:16

两个提案的差异非常微妙,我是这样看待它们的:

同一个表

  • 将其保留在同一个表中将允许您以较低的成本检索它
  • 更新可能会锁定其他查询的数据库部分队列中需要减慢它们的速度,
  • 使表更宽,增加 I/O(它总是会增加表扫描的 I/O,但对于索引查找来说,情况就不那么简单了——当记录大小变得大于文件系统的块大小时,甚至索引也会增加)查找将必须执行 2 倍以上的 I/O;或 n 倍以上的 I/O,具体取决于记录大小/块大小,如果记录大小远小于块大小,则对索引查找取决于查询类型/磁盘上数据的顺序 - 如果从同一块中选择记录,您会感觉到性能受到影响,如果选择稀疏数据,您不会感觉到)

单独的表

  • 更新将发生在较小的表上,因此 I/O 较少(根据隔离级别,检查外键可能仍然需要锁,但应该更短,因为 I/O 较少)
  • 检索文章的数据需要一个联接,这将 增加 I/O
  • 如果视图计数是大多数其他记录中不存在的新功能,则

,您还可以节省空间,即使加入可能会增加所需的 I/O,但这可能会带来更好的性能注释
该评论适用,但很微妙:记录大小可能不会受到显着影响以影响所有查询。使用单独的表减少 I/O 的好处可能无法察觉。

The differences of two proposals are quite subtle, here's how I see them:

Same table

  • keeping it in the same table will allow you to retrieve it at lower cost
  • updates might be locking the parts of DB that other queries in the queue need slowing them down
  • making tables wider increases I/O (it always increase I/O for table scans, but for index lookups it is not so straight forward - when record size becomes bigger then the filesystem's block size then even the index lookups will have to do 2x more I/O; or nx more I/O depending on the record size/block size, if the record size is much less then the block size then the effect for the index lookups depends on the type of query/order of the data on the disk - if choosing records from the same block you will feel the performance hit, if choosing sparse data you will not feel it)

Separate tables

  • updates will happen on a smaller table so less I/O (locks might still be necessary to check foreign key, depending on isolation level, but should be shorter as there is less I/O)
  • retrieving data for an article requires a join which will increase I/O
  • if the view count is a new feature not present on most of your other records you are also saving space and even though join might increase required I/O, this might give better performance

Notes:
The comment apply, but are subtle: record size will probably not get significantly affected to influence all queries. The benefits of less I/O with separate table might not be perceivable.

月棠 2024-10-14 15:57:16

如果 articles 表获得大量写入,则由于写入时的表锁定,更新列可能会慢一些。

使用单独的表只是读取速度慢一点/重一点,但就更新而言是最轻的。但是在大量写入之后,您可以轻松地重建表,而不会造成太大的性能损失,因为它将是一个相当轻的表。

如果您的数据库没有高负载,那么我会采取添加列的简单选择。

If the articles table gets a lot of writes than updating the column might be a bit slower because of the table locks while writing.

Using a separate table is only a little bit slower/heavier to read but is lightest in terms of updates. But after loads of writes you can easily rebuild the table without much performance penalty since it will be quite a light table.

If your database doesn't have a high load than I would take the easy option of adding a column though.

悸初 2024-10-14 15:57:16

我还有另一种方法,即在当天晚些时候或当网站流量太少时进行更新。
我会以这种方式完成这项工作,当任何用户阅读文章时,用户 ID 将存储在一个新表中,稍后通过脚本我可以更新用户阅读该文章的次数。

I have one more approach, where I do the update later in the day or when the traffic to the site is too less.
I would do the job in this way, when ever any user reads the articles, there user id will be stored in a new table, and later through script I can update How many times the article have been read by the users.

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