Jump to content

Is it worth storing database results in a session


Recommended Posts

Let's say I have the following query, all tables are properly indexed, the resultant data is needed for every initial load and ajax request, and the associated data rarely changes:

SELECT * FROM a JOIN b ON b.id=a.b_id JOIN c ON c.id=b.c_id JOIN d ON d.id=c.d_id JOIN e ON e.id=d.e_id JOIN f ON f.id=e.f_id WHERE a.something=123;

Instead of running the query every request, I decided to store the results in a session.  To know if the session was stale, I added a datetime column to one of the tables, and updated the value using the application whenever one of the six tables changes.  I then would just first execute SELECT updated FROM a  WHERE something=123;, and only use the big query if the updated value was different than the session value.

 

Recently found things not working as expected, and later found that I forgot to update the a.updated value when one of the column values changes in one of the joined tables.

 

Any advice from seasoned professionals whether it is worth trying to take steps such as these?  If so, would you recommend using triggers?  Don't think this would have even helped me on my earlier faux pas as the column which changed was also a new column, but probably better than using the application.  Or maybe I should be doing this completely some different way?

Link to comment
Share on other sites

My first advice would be "Don't use SELECT * ".

 

Every one of those ids in the joins will be duplicated so you are getting a lot of data you don't need and therefore slowing your query. Specify just the columns you needOnly

 

Wouldn't dream of doing so in practice, and only did so to limit the clutter for this post and focus the responses.  Guess it didn't work...

SELECT a.x, a.y, b.x AS b_x, c.c, e.bla, f.bla AS blabla FROM a JOIN b ON b.id=a.b_id JOIN c ON c.id=b.c_id JOIN d ON d.id=c.d_id JOIN e ON e.id=d.e_id JOIN f ON f.id=e.f_id WHERE a.something=123;
Edited by NotionCommotion
Link to comment
Share on other sites

I put results in a session for my product list.  I also put the query string in a session var.  The query string would change based on different vehicle selections by the client.  So I just check if the query string is different from the one in the session and only run the query if it is different.  In todays web market with so many users on mobile devices, it's really best to minimize load time in any logical method you can, this method is certainly one of those methods.

Link to comment
Share on other sites

To know if the session was stale, I added a datetime column to one of the tables, and updated the value using the application whenever one of the six tables changes.

 

 

the query cache, in the database engine, will do this for you.

 

for any particular query statement, if the tables referenced in the query haven't changed (insert, update, delete), the result is gotten from the query cache, assuming the result is already in the query cache. if any table referenced by the query statement has changed, the new data is retrieved and cached.

Link to comment
Share on other sites

 

 

the query cache, in the database engine, will do this for you

I agree, can even devote more memory to it if need to.

 

 

 

for any particular query statement, if the tables referenced in the query haven't changed (insert, update, delete), the result is gotten from the query cache, assuming the result is already in the query cache. if any table referenced by the query statement has changed, the new data is retrieved and cached.

 

Also agree.

When doing many new inserts constant it becomes useless, depends how often the data is changed.

So if doing complex queries such as sorting or searches might want to look into caching it yourself to lessen doing the query.

Link to comment
Share on other sites

the query cache, in the database engine, will do this for you.

 

for any particular query statement, if the tables referenced in the query haven't changed (insert, update, delete), the result is gotten from the query cache, assuming the result is already in the query cache. if any table referenced by the query statement has changed, the new data is retrieved and cached.

 

Wow!  Never knew that the DB would cache queries, but I guess it makes sense.

 

So, do I "check" the query cache to see if it was changed?

 

Or just do my big query every time, and rely on the DB to optimize the results?

Link to comment
Share on other sites

you just execute the query. the result you get back will either be from the query cache, if the result is in the cache and it is current, or the database engine will actually run the query against the database table(s) to get the result (and put it in the cache as well.)

 

the advantage of doing this at the database level will mean that common and current things on your site - configuration, menus, the current state of forum threads, ... will be in the cache and all the visitors viewing that common/current information will get results from the cache.

Edited by mac_gyver
Link to comment
Share on other sites

i don't know precisely, but everything about the query cache would be found in the documenation - http://dev.mysql.com/doc/refman/5.6/en/query-cache.html

 

it looks like checking the value of Qcache_hits, before and after a query is ran will tell you if the query got the result from the cache -

 

If a query result is returned from query cache, the server increments the Qcache_hits status variable

 

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.