PHP:会话与数据库 [英] php: sessions vs. database

查看:67
本文介绍了PHP:会话与数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库检索其成员(大约10个成员)的类.

I have a class that retrieves its memeber (more or less 10 members) from a database.

我的问题是:每次从数据库(MySQL)中获取它们,在会话的数组中仅保留一个ID或直接将它们存储在会话的数组中,效率更高吗?

My question is: is it more efficient to fetch them every time from the db (MySQL), leaving just an ID in the session's array or store them directly in the session's array?

性能方面的差异如此大吗? (假设数据库有100.000行)

And is the difference in performance terms so great? (given a database with say 100.000 rows)

推荐答案

考虑到您还是在会话中存储ID,因此会话是最有意义的.执行session_start()会加载您的会话信息,因此之后是否加载1或10个项目基本上是无关紧要的(除非它们确实很大,但是无论如何都将是一个问题).

Considering you're storing an ID in the session anyway, the session makes the most sense. Doing a session_start() loads your session information so whether you've loaded 1 or 10 items after that is largely irrelevant (unless they're really large but that'll be a problem in any case).

所以坚持会议.

如果您真正关心速度,请使用内存缓存,例如APC或内存缓存.担心文件系统或数据库中10个项目的速度令人分心.差异将很小到不相关.

If you get really concerned about speed use an in-memory cache like APC or memcache. Worrying about speed for 10 items from the filesystem or database is a distraction. The difference is going to be so minimal as to be irrelevant.

注意:以上假设有两件事:

Note: the above assumes two things:

  1. 该查询是高性能的(从100k中检索10行应在0.1秒内完成);和
  2. 您正在执行一个查询,而不是10.

这篇关于PHP:会话与数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆