临时数据存储在会话数据中与存储在 Sql 数据库中

发布于 2024-07-23 05:32:45 字数 90 浏览 7 评论 0原文

我想知道使用 PHP 中的 $_SESSION 变量在会话中存储临时数据(与该会话相关)或从 SQL 数据库存储和检索哪个更有效?

感谢您的时间。

I am wondering which is more efficient, to store temporary data (related to that session) in a session using the $_SESSION variable in PHP or store and retrieve from an SQL database?

Thank you for your time.

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

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

发布评论

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

评论(5

<逆流佳人身旁 2024-07-30 05:32:45

请记住,会话变量由存储机制支持,也就是说,当请求完成时,会话处理程序将写入会话,默认情况下,这是写入文件。 在下一个请求时,它将从该文件(或会话处理程序使用的任何其他文件)中拉回。

如果您在每个请求上读取和写入这些数据,只需坚持使用 $_SESSION 变量,连接、查询和更新数据库的开销不会比默认的 $_SESSION 更快。

如果您正在运行多个负载平衡服务器并且需要在它们之间共享会话数据,您可能只想使用数据库支持的会话。 在这种情况下,如果您发现数据库会话的开销显着降低了站点速度,您可能会考虑在 Web 服务器和数据库之间使用 memcached。

Keep in mind the session variable is backed by a storage mechanism, that is, when the request finishes the session gets written by the session handler, by default this is to a file. On the next request it is pulled back from that file (or whatever else the session handler uses).

If you're reading and writing this data on every request, just stick with a the $_SESSION variables, the overhead of connecting, querying and updating a database will not be faster than the default $_SESSION.

You'll probably only ever want to use a database backed session if you are running multiple load-balanced servers and need to share the session data between them. In this case, if you find the overhead of the database sessions to be slowing down your site to a noticeable degree you might consider sticking memcached between your web server and the database.

酒绊 2024-07-30 05:32:45

我对从数据库或文件读取不太了解,但我不认为“数据库访问比其他人慢”是真的。 我从学校课程中了解到,与 I/O 访问相比,网络延迟可以忽略不计。 如果我们使用数据库进行会话,我们有一些优势:

我们不必担心许多服务器,因为没有文件系统不同。

我还认为向数据库存储数据或从数据库读取数据比文件系统更容易。

此外,如果我们使用共享托管,将会话存储在数据库中对于安全性来说是一个主要优势。

如果我错了,请纠正我。 我还有很多东西要学。 谢谢。

I don't know much about reading from a database or a file, but I don't think that "DB access is slower than others" is true. I've learned from my school lessons that Network latency is negligible compared to the I/O access. And if we use DB for sessions, we have some advantages:

We don't have to worried about many servers because there is no file system different.

I also think that storing/reading something to/from a database is more easier than a file system.

Also, if we are using shared hosting, storing sessions in a database is a major advantage for the security.

if i were wrong, please correct me. i still have many things to learn. Thanks.

穿透光 2024-07-30 05:32:45

这实际上取决于您打算存储的数据量和您打算处理的流量。 如果数据很少,并且站点不需要扩展到一台 Web 服务器之外,请务必使用默认会话处理程序,将会话数据写入 Web 服务器的文件系统。

如果您需要扩展到超过一个盒子,建议您将会话数据存储到内存数据库(例如 memcached)或常规数据库中。 您可以覆盖 PHP 中的会话处理程序并编写使用 $_SESSION 时您自己的实现存储到数据库。

It really depends on the volume of data you intend to store and the amount of traffic you intend to handle. If the data is minimal and the site does not need to scale beyond one web server, by all means use the default session handler which writes the session data into the filesystem of the webserver.

If you need to scale beyond one box, it is recommended that you store your session data into either a memory database such as memcached or regular database. You can override the session handler in PHP and write your own implementation to store to database when using $_SESSION.

最终幸福 2024-07-30 05:32:45

什么更有效取决于您要存储的数据量以及您计划如何处理临时数据。 我有时会在文件存储中存储 5 兆的会话数据,这是一个可怕的性能杀手。 但 5 兆兆的状态是一个可怕的数字,你真的不应该到达那里。

无论如何,您可以将 PHP 会话配置为存储在数据库中表 并获得两全其美的效果。

不过,如果数据不能正确表征用户会话,那么您不应该使用会话,而应使用某些模型对象。

What's more efficient will depend on the amount of data you want to store and what do you plan to do with the temporary data. I've sometimes stored 5 megs in session data in file storage and it was an awful performance killer. But 5 megs of state is an awful lot and you really shouldn't get there.

Anyhow you can configure PHP's sessions to be stored in a database table and get the best of both worlds.

Still, if the data is not properly characteristic of a user session, then you should not use sessions and use some model object instead.

时光倒影 2024-07-30 05:32:45

PHP 会话比数据库访问更快。 但 PHP 会话存在一些已知问题

如果您想要真正快速的访问时间,同时避免 PHP 会话管理的陷阱,您可能需要考虑 memcached。

PHP sessions are faster than DB access. But PHP sessions have some known issues.

You may want to look at memcached if you want really fast access times, while avoiding the pitfalls of PHP session management at the same time.

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