DbSession's Query Count

Hey guys,

maybe I’m missing the bigger picture here, but I feel the DbSession class is not really well thought through? Right now there are at least three database queries on every page while in theory a lot of the time one would be enough.

There are two queries for the same session record on opening (DbSession::readSession) and closing (DbSession:: writeSession) the session. With minimum refactoring the second call should only be needed in case DbSession::readSession didn’t return any data? Or maybe not even then (record not found => insert new)?

And then DbSession:: writeSession saves the session data on EVERY request, even though there might be no change at all? Am I missing something? Even "expire" could be updated only once every 30 seconds or so?

Before I write an extended class for DbSession I wanted to know if there is something I’m not thinking of.

Thank you for your time.


The class works as a backend for standard session handler. It writes the same way as other handlers, such as files, do.

Right, now wouldn’t it be a good idea to reduce all session related database queries by more than half with a simple wrapper method?

I just wrote this down real quick, maybe I’m missing something, but this should reduce the calls to one SELECT query on session start and an UPDATE query only if session data changed or the session has not been updated for a minute. Would you care to comment? Is this a viable solution?

Thank you!

Probably yes. Would you like to make a pull request to our GitHub? https://github.com/yiisoft/yii2

I’ll take a closer look at the code then and run some tests. I just wanted to make sure I’m not missing something before I put more time in it. Will add a pull request once I’m certain it’s not breaking any functionality.