确定用户是否查看过数据的最佳方法

时间:2021-10-03 16:32:40

What is the best method to determine if a user has viewed a piece of data, ie like an update to a comment. The two solutions I have thought about are these....

确定用户是否查看了一段数据的最佳方法是什么,即更新评论。我想到的两个解决方案是......

  1. Use a separate table that has a row for each user and the data id that is being viewed and inserting into when the item was last viewed.

    使用单独的表,每个用户都有一行,以及上次查看项目时正在查看和插入的数据ID。

  2. Use the same table and add a row for every user when the item is changed and delete that row when the user actually views the data.

    使用相同的表,并在项目更改时为每个用户添加一行,并在用户实际查看数据时删除该行。

Both methods solve the problem but in solution 2 the maximum rows for the table would at worst equal that of solution 1, no one viewed anything, and at best has 0 rows, everything has been viewed. I know in solution 2 you have no way to determine when it was viewed.

这两种方法都解决了这个问题,但在解决方案2中,表的最大行最差等于解决方案1的行,没有人查看任何内容,最多只有0行,所有内容都已被查看。我知道在解决方案2中你无法确定它何时被查看。

Thoughts?

Edit: I was using an update to a comment as an example. In the actual application, new users wouldn't be expected to view or read old data. It would mean nothing to them for they just joined.

编辑:我使用评论更新作为示例。在实际应用中,不希望新用户查看或读取旧数据。他们刚加入对他们没有任何意义。

4 个解决方案

#1


3  

It's got to be option 1:

它必须是选项1:

Table 1 (comment)

表1(评论)

  • comment_id
  • comment

Table 2 (comment_view)

表2(comment_view)

  • comment_id
  • user_id

Option 2 will not work because every new user will have every existing comment "marked read."

选项2不起作用,因为每个新用户都会将所有现有注释“标记为已读”。

#2


3  

do you need to know when or how many times they viewed a piece of data?

你需要知道他们查看一段数据的时间或次数吗?

If not I'd keep a table with a FK to the data-updated and a FK to the user. then a simple check to see if they viewed:

如果不是,我会将带有FK的表保存到数据更新,并将FK保留给用户。然后进行简单的检查,看看他们是否看过:

select count(*) from DataAlerts where dataid = 1 and userid = 1

when data is updated insert records for the data and users.

更新数据时,插入数据和用户的记录。

when a user views delete that user.

当用户查看删除该用户时。

#3


2  

Option 1 is the better approach here - generally, deleting something to indicate that something has happened is a strange way to do things in a database, and as ng.mangine indicated above, it won't even work (unless you add rows to that table every time there's a new user, but THAT is almost certain to be a bottleneck and perf issue - consider what happens when you have a million comments and a new user joins).

选项1是更好的方法 - 通常,删除某些内容以表明发生了某些事情是在数据库中执行操作的一种奇怪方式,并且如上所述ng.mangine,它甚至不起作用(除非您向其添加行)表每次有新用户时,这几乎肯定是一个瓶颈和性能问题 - 考虑当你有一百万条评论和一个新用户加入时会发生什么。

Option 1 is much clearer; only think about changing it if you encounter performance issues (or if you happen to know in advance that your traffic will require a more optimized strategy, like bit vectors on each topic representing read / unread for each system user).

选项1更清晰;如果您遇到性能问题(或者您事先知道您的流量需要更优化的策略,例如每个主题的位向量表示每个系统用户的读/未读),请考虑更改它。

#4


1  

Option 2 is an unusual way of doing things, which in the database world is a way of saying it might work but it's probably best to go with a more traditional approach like Option 1.

选项2是一种不寻常的做事方式,在数据库世界中这是一种说法可能有用的方式,但最好采用更传统的方法,如选项1。

#1


3  

It's got to be option 1:

它必须是选项1:

Table 1 (comment)

表1(评论)

  • comment_id
  • comment

Table 2 (comment_view)

表2(comment_view)

  • comment_id
  • user_id

Option 2 will not work because every new user will have every existing comment "marked read."

选项2不起作用,因为每个新用户都会将所有现有注释“标记为已读”。

#2


3  

do you need to know when or how many times they viewed a piece of data?

你需要知道他们查看一段数据的时间或次数吗?

If not I'd keep a table with a FK to the data-updated and a FK to the user. then a simple check to see if they viewed:

如果不是,我会将带有FK的表保存到数据更新,并将FK保留给用户。然后进行简单的检查,看看他们是否看过:

select count(*) from DataAlerts where dataid = 1 and userid = 1

when data is updated insert records for the data and users.

更新数据时,插入数据和用户的记录。

when a user views delete that user.

当用户查看删除该用户时。

#3


2  

Option 1 is the better approach here - generally, deleting something to indicate that something has happened is a strange way to do things in a database, and as ng.mangine indicated above, it won't even work (unless you add rows to that table every time there's a new user, but THAT is almost certain to be a bottleneck and perf issue - consider what happens when you have a million comments and a new user joins).

选项1是更好的方法 - 通常,删除某些内容以表明发生了某些事情是在数据库中执行操作的一种奇怪方式,并且如上所述ng.mangine,它甚至不起作用(除非您向其添加行)表每次有新用户时,这几乎肯定是一个瓶颈和性能问题 - 考虑当你有一百万条评论和一个新用户加入时会发生什么。

Option 1 is much clearer; only think about changing it if you encounter performance issues (or if you happen to know in advance that your traffic will require a more optimized strategy, like bit vectors on each topic representing read / unread for each system user).

选项1更清晰;如果您遇到性能问题(或者您事先知道您的流量需要更优化的策略,例如每个主题的位向量表示每个系统用户的读/未读),请考虑更改它。

#4


1  

Option 2 is an unusual way of doing things, which in the database world is a way of saying it might work but it's probably best to go with a more traditional approach like Option 1.

选项2是一种不寻常的做事方式,在数据库世界中这是一种说法可能有用的方式,但最好采用更传统的方法,如选项1。