简体   繁体   English

PHP,这是将数据存储到会话和数据库之间的更好方法吗?

[英]PHP, which is the better way between storing data into session and database?

I am building a website that shows recommend pages for users. 我正在建立一个网站,为用户显示推荐页面。

Users only click next button to look at different recommend web pages. 用户只需单击下一步按钮即可查看不同的推荐网页。

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? 你怎么看?

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). 所以,在我遇到数据库问题之前我不会打扰会话(很可能永远不会发生)。

I don't see what's wrong with incrementing user_pageview on every pageview, you could do 我没有看到在每个网页浏览上增加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脚本无论如何都会连接到您的数据库,并且该查询本身不应该杀死您的服务器。

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. 我不这么认为,因为数据库服务器应该能够轻松地处理它。

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). 例如,您可以将其存储在会话中(确保在销毁会话之前更新数据库),或者在客户端使用缓存(本地存储)。

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). 如果是这种情况,我会首先尝试检查WTH是否与我的数据库出错(为什么它是瓶颈)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM