Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top