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?
我想知道哪个更高效,在PHP中使用$ _SESSION变量存储临时数据(与该会话相关)或从SQL数据库存储和检索?
Thank you for your time.
感谢您的时间。
5 个解决方案
#1
21
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.
如果您在每个请求上读取和写入此数据,只需坚持使用$ _SESSION变量,连接,查询和更新数据库的开销将不会比默认的$ _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.
如果您正在运行多个负载平衡的服务器并且需要在它们之间共享会话数据,那么您可能只想使用数据库支持的会话。在这种情况下,如果您发现数据库会话的开销会使您的站点速度显着降低,您可能会考虑在Web服务器和数据库之间粘贴memcached。
#2
3
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:
我不太了解从数据库或文件中读取数据,但我不认为“数据库访问比其他人慢”是正确的。我从学校的课程中了解到,与I / O访问相比,网络延迟可以忽略不计。如果我们使用DB进行会话,我们有一些优势:
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.
如果我错了,请纠正我。我还有很多东西需要学习。谢谢。
#3
2
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.
这实际上取决于您要存储的数据量以及您打算处理的流量。如果数据最小且站点不需要扩展到一个Web服务器之外,则无论如何都要使用默认会话处理程序将会话数据写入Web服务器的文件系统。
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.
如果需要扩展到一个框以外,建议将会话数据存储到内存数据库(如memcached或常规数据库)中。您可以在PHP中覆盖会话处理程序,并在使用$ _SESSION时编写自己的实现以存储到数据库。
#4
1
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.
更有效的方法取决于您要存储的数据量以及您打算如何处理临时数据。我有时在文件存储中存储了5兆的会话数据,这是一个可怕的性能杀手。但5美元的州是非常多的,你真的不应该到那里。
Anyhow you can configure PHP's sessions to be stored in a database table and get the best of both worlds.
无论如何,您可以将PHP的会话配置为存储在数据库表中,并充分利用这两个方面。
Still, if the data is not properly characteristic of a user session, then you should not use sessions and use some model object instead.
但是,如果数据不是用户会话的正确特征,那么您不应该使用会话并使用某些模型对象。
#5
0
PHP sessions are faster than DB access. But PHP sessions have some known issues.
PHP会话比DB访问更快。但PHP会话有一些已知问题。
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.
如果您想要快速访问时间,可能需要查看memcached,同时避免PHP会话管理的陷阱。
#1
21
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.
如果您在每个请求上读取和写入此数据,只需坚持使用$ _SESSION变量,连接,查询和更新数据库的开销将不会比默认的$ _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.
如果您正在运行多个负载平衡的服务器并且需要在它们之间共享会话数据,那么您可能只想使用数据库支持的会话。在这种情况下,如果您发现数据库会话的开销会使您的站点速度显着降低,您可能会考虑在Web服务器和数据库之间粘贴memcached。
#2
3
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:
我不太了解从数据库或文件中读取数据,但我不认为“数据库访问比其他人慢”是正确的。我从学校的课程中了解到,与I / O访问相比,网络延迟可以忽略不计。如果我们使用DB进行会话,我们有一些优势:
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.
如果我错了,请纠正我。我还有很多东西需要学习。谢谢。
#3
2
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.
这实际上取决于您要存储的数据量以及您打算处理的流量。如果数据最小且站点不需要扩展到一个Web服务器之外,则无论如何都要使用默认会话处理程序将会话数据写入Web服务器的文件系统。
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.
如果需要扩展到一个框以外,建议将会话数据存储到内存数据库(如memcached或常规数据库)中。您可以在PHP中覆盖会话处理程序,并在使用$ _SESSION时编写自己的实现以存储到数据库。
#4
1
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.
更有效的方法取决于您要存储的数据量以及您打算如何处理临时数据。我有时在文件存储中存储了5兆的会话数据,这是一个可怕的性能杀手。但5美元的州是非常多的,你真的不应该到那里。
Anyhow you can configure PHP's sessions to be stored in a database table and get the best of both worlds.
无论如何,您可以将PHP的会话配置为存储在数据库表中,并充分利用这两个方面。
Still, if the data is not properly characteristic of a user session, then you should not use sessions and use some model object instead.
但是,如果数据不是用户会话的正确特征,那么您不应该使用会话并使用某些模型对象。
#5
0
PHP sessions are faster than DB access. But PHP sessions have some known issues.
PHP会话比DB访问更快。但PHP会话有一些已知问题。
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.
如果您想要快速访问时间,可能需要查看memcached,同时避免PHP会话管理的陷阱。