会话比查询数据库更快吗?

发布于 2024-09-09 04:15:56 字数 174 浏览 11 评论 0原文

例如,用户正在登录,系统正在存储有关他们的信息,例如:出生日期,那么从会话中获取此信息更快,还是查询数据库更快?

我的想法是,用户只需登录一次并且会话始终存在,但是如果我查询数据库,那么如果用户重新加载页面,系统需要一次又一次查询,而不是从数据库获取数据临时“地点”。

我使用 PHP 和 MySQL。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ花落谁相伴 2024-09-16 04:15:56

对于几乎所有语言和数据库来说,是的。用户会话通常仅存储在内存中,只需查找即可获取它。数据库访问通常涉及与不同进程的某种套接字通信。相比之下还是比较重的。

For almost any language and database, yes. A user session is usually just stored in memory, and getting a hold of it is just a matter of looking it up. A database access usually involves some socket communication with a different process. Rather heavy in comparison.

月隐月明月朦胧 2024-09-16 04:15:56

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

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

登录页面

// database query run once only at this page
// if user exits, you store it into session else redirect with an error message

How are you doing it all? The way to do is to check the user credentials at the login page and yes there you do have to make a query to check if the user specified criteria match in the database. If they do, you store them in session and then you continue based on that session.

So, it is not about comparison, you have to make to query the database once at the login page and use session afterwards.

Login Page

// database query run once only at this page
// if user exits, you store it into session else redirect with an error message
以为你会在 2024-09-16 04:15:56

实际上,卡尔的回答并不完全正确,说“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 列表的列表)

Actually, Carl's answer is not fully correct, and saying "MySql" doesn't help either.

You see, database systems like mysql have "storage engines". These usually write to files, but there are some which write to memory (MEMORY), others write memory but keep a file backup (MyISAM) and a few to /dev/null (BLACKHOLE).

So it all depends on the storage engine:

  • MyISAM - Default engine as of MySQL 3.23 with great performance
  • MEMORY - Hash based, stored in memory, useful for temporary tables
  • InnoDB - Supports transactions, row-level locking, and foreign keys
  • BerkeleyDB - Supports transactions and page-level locking
  • BLACKHOLE - /dev/null storage engine (anything you write to it disappears)
  • EXAMPLE - Example storage engine
  • ARCHIVE - Archive storage engine
  • CSV - CSV storage engine
  • ndbcluster - Clustered, fault-tolerant, memory-based tables
  • FEDERATED - Federated MySQL storage engine
  • MRG_MYISAM - Collection of identical MyISAM tables
  • ISAM - Obsolete storage engine

(list from PhpMyAdmin Egines list)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文