会话比查询数据库更快吗?
例如,用户正在登录,系统正在存储有关他们的信息,例如:出生日期,那么从会话中获取此信息更快,还是查询数据库更快?
我的想法是,用户只需登录一次并且会话始终存在,但是如果我查询数据库,那么如果用户重新加载页面,系统需要一次又一次查询,而不是从数据库获取数据临时“地点”。
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于几乎所有语言和数据库来说,是的。用户会话通常仅存储在内存中,只需查找即可获取它。数据库访问通常涉及与不同进程的某种套接字通信。相比之下还是比较重的。
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.
你做得怎么样?方法是在登录页面检查用户凭据,是的,您必须进行查询以检查用户指定的条件是否在数据库中匹配。如果存在,您将它们存储在会话中,然后基于该会话继续。
所以,这不是比较,你必须在登录页面查询数据库一次,然后使用会话。
登录页面
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
实际上,卡尔的回答并不完全正确,说“MySql”也没有帮助。
你看,像mysql这样的数据库系统都有“存储引擎”。这些通常写入文件,但有些写入内存(MEMORY),其他写入内存但保留文件备份(MyISAM),还有一些写入/dev/null(BLACKHOLE)。
所以这一切都取决于存储引擎:
(来自 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:
(list from PhpMyAdmin Egines list)