8

So for example, the user is logging in, and the system is storing informations about them example: birth date, so is faster to get this information from the session, or to query the database for it?

My idea was, that the user needs to login just once and the session is always there, but If I query the database, then if the user reloads the page, the system needs to query again and again, instead of getting the data from a temporary 'place'.

I use PHP and MySQL.

4

3 回答 3

10

对于几乎任何语言和数据库,是的。用户会话通常只存储在内存中,获取它只是查找它的问题。数据库访问通常涉及与不同进程的一些套接字通信。比较重。

于 2010-07-11T18:54:50.957 回答
1

你是怎么做到的?方法是在登录页面检查用户凭据,是的,您必须进行查询以检查用户指定的条件是否在数据库中匹配。如果是这样,您将它们存储在会话中,然后根据该会话继续。

因此,这不是比较,您必须在登录页面查询一次数据库,然后使用会话。

登录页面

// database query run once only at this page
// if user exits, you store it into session else redirect with an error message
于 2010-07-11T18:55:31.927 回答
0

实际上,卡尔的回答并不完全正确,说“MySql”也无济于事。

你看,像mysql这样的数据库系统有“存储引擎”。这些通常写入文件,但有一些写入内存 (MEMORY),其他写入内存但保留文件备份 (MyISAM),还有一些写入 /dev/null (BLACKHOLE)。

所以这一切都取决于存储引擎:

  • MyISAM - MySQL 3.23 的默认引擎,具有出色的性能
  • MEMORY - 基于哈希,存储在内存中,对临时表有用
  • InnoDB - 支持事务、行级锁定和外键
  • BerkeleyDB - 支持事务和页面级锁定
  • BLACKHOLE - /dev/null 存储引擎(您写入的任何内容都会消失)
  • 示例 - 示例存储引擎
  • ARCHIVE - 存档存储引擎
  • CSV - CSV 存储引擎
  • ndbcluster - 集群的、容错的、基于内存的表
  • FEDERATED - 联合 MySQL 存储引擎
  • MRG_MYISAM - 相同 MyISAM 表的集合
  • ISAM - 过时的存储引擎

(来自 PhpMyAdmin Egines 列表的列表)

于 2010-07-11T21:36:31.907 回答