PHP,将数据存储到会话和数据库中哪个更好?

时间:2022-05-07 12:47:54

I am building a website that shows recommend pages for users.

我正在建立一个网站,为用户显示推荐页面。

Users only click next button to look at different recommend web pages.

用户只需点击next按钮查看不同的推荐网页。

I wanna check every single activities that users can make.

我想检查用户可以做的每一项活动。

So, I will check page views that users actually see.

因此,我将检查用户实际看到的页面视图。

1. there is a 'user_pageview' column for each users.
2. the numbers will increase every time if users click next button.

I can send a query to update data every single time.

我可以每次发送一个查询更新数据。

However, I believe this step will be annoying to my server.

但是,我相信这一步会让我的服务器感到厌烦。

So, I am thinking to save the data into session for a while and save it later.

因此,我正在考虑将数据保存到会话中一段时间,稍后再保存。

What do you think?

你怎么认为?

3 个解决方案

#1


3  

Yes it will be "annoying your server".
The same way you are "annoying" your car by riding it.

是的,它会“打扰你的服务器”。就像你骑着车“骚扰”你的车一样。

A database server's intention is to operate the data - store, retrieve, filter, etc.
There is nothing essentially wrong with storing data in the database.

数据库服务器的目的是操作数据存储、检索、筛选等。在数据库中存储数据本质上没有问题。

There may be a problem, yes, with index rebuilding (if any).
However, there would be a problem with tracking user session too.

的确,索引重建可能存在问题(如果有的话)。但是,跟踪用户会话也存在问题。

So, I won't bother with session until I have certain problem with database (which most likely won't occur ever).

所以,在我对数据库有一定的问题之前(很可能不会发生),我就不需要讨论会话了。

#2


1  

I don't see what's wrong with incrementing user_pageview on every pageview, you could do

我看不出在每个pageview上递增user_pageview有什么问题,你可以这么做

update table views set user_pageview = user_pageview + 1 where page_id = 4

Your PHP script would have been connected to your DB anyway and that query alone shouldn't be killing your server.

你的PHP脚本应该已经连接到你的数据库了,而且这个查询不应该杀死你的服务器。

#3


0  

Saving it to the session will also 'annoy' your server since you will need to make a request to be able to save it into the session.

将其保存到会话也会“惹恼”您的服务器,因为您需要发出请求才能将其保存到会话中。

If you mean the db server, my question is: is it a problem. Do you notice the db server is becoming the bottleneck? I don't think so because the db server should easily be able to handle it.

如果您指的是db服务器,我的问题是:它是一个问题吗?您是否注意到db服务器正在成为瓶颈?我不这么认为,因为db服务器应该很容易处理它。

However I you really have db performance issues AND ONLY THEN you should find a way to relieve the db server by caching stuff. You could for example store it in the session (make sure you update the db before the session is destroyed) or by using a cache on the client side (local storage).

但是,我您确实存在db性能问题,只有这样,您才能找到一种通过缓存东西来缓解db服务器的方法。例如,可以将它存储在会话中(确保在会话被破坏之前更新db),或者在客户端使用缓存(本地存储)。

Again you should only look into this IF the db server becomes the bottleneck. And IF this is the case I would first try to check out WTH is going wrong with my db (why it is the bottleneck).

同样,只有当db服务器成为瓶颈时,才应该研究这个问题。如果是这种情况,我会首先检查我的db是否出了问题(为什么它是瓶颈)。

#1


3  

Yes it will be "annoying your server".
The same way you are "annoying" your car by riding it.

是的,它会“打扰你的服务器”。就像你骑着车“骚扰”你的车一样。

A database server's intention is to operate the data - store, retrieve, filter, etc.
There is nothing essentially wrong with storing data in the database.

数据库服务器的目的是操作数据存储、检索、筛选等。在数据库中存储数据本质上没有问题。

There may be a problem, yes, with index rebuilding (if any).
However, there would be a problem with tracking user session too.

的确,索引重建可能存在问题(如果有的话)。但是,跟踪用户会话也存在问题。

So, I won't bother with session until I have certain problem with database (which most likely won't occur ever).

所以,在我对数据库有一定的问题之前(很可能不会发生),我就不需要讨论会话了。

#2


1  

I don't see what's wrong with incrementing user_pageview on every pageview, you could do

我看不出在每个pageview上递增user_pageview有什么问题,你可以这么做

update table views set user_pageview = user_pageview + 1 where page_id = 4

Your PHP script would have been connected to your DB anyway and that query alone shouldn't be killing your server.

你的PHP脚本应该已经连接到你的数据库了,而且这个查询不应该杀死你的服务器。

#3


0  

Saving it to the session will also 'annoy' your server since you will need to make a request to be able to save it into the session.

将其保存到会话也会“惹恼”您的服务器,因为您需要发出请求才能将其保存到会话中。

If you mean the db server, my question is: is it a problem. Do you notice the db server is becoming the bottleneck? I don't think so because the db server should easily be able to handle it.

如果您指的是db服务器,我的问题是:它是一个问题吗?您是否注意到db服务器正在成为瓶颈?我不这么认为,因为db服务器应该很容易处理它。

However I you really have db performance issues AND ONLY THEN you should find a way to relieve the db server by caching stuff. You could for example store it in the session (make sure you update the db before the session is destroyed) or by using a cache on the client side (local storage).

但是,我您确实存在db性能问题,只有这样,您才能找到一种通过缓存东西来缓解db服务器的方法。例如,可以将它存储在会话中(确保在会话被破坏之前更新db),或者在客户端使用缓存(本地存储)。

Again you should only look into this IF the db server becomes the bottleneck. And IF this is the case I would first try to check out WTH is going wrong with my db (why it is the bottleneck).

同样,只有当db服务器成为瓶颈时,才应该研究这个问题。如果是这种情况,我会首先检查我的db是否出了问题(为什么它是瓶颈)。