Page 1 of 1

Updating sessions

Posted: Sun Nov 15, 2009 5:09 am
by Daz
I have a db table to store sessions for the sole purpose of admins being able to see active visitors on the site. One of the cells is a timestamp field.

Code: Select all

IF ( UPDATE_SESSIONS )     {
     Remove sessions older than ten minutes
} ELSE {
    There is no current session - INSERT one
}
Question #1
If the page is refreshed too fast (so that both page calls have the same timestamp) the 2nd page errors for trying to INSERT a duplicate. Is there anyway to avoid this other than putting a try/catch around the INSERT statement?

Question #2
Are there any real benefits to change MySQL's type to MEMORY for the sessions table?

Re: Updating sessions

Posted: Tue Nov 17, 2009 7:04 am
by iankent
Question #1 - The best way to avoid it would be to use WHERE NOT EXISTS, e.g.

Code: Select all

INSERT INTO table (a,b,c) VALUES (x,y,z) WHERE NOT EXISTS (SELECT a FROM table WHERE a=x AND b=y AND c=z);
That way it'll only insert the values if the SELECT query returns no results

Question #2 - Not a clue sorry :)
edit: actually... if you use MEMORY it'll be faster running SELECTS but your server will require more RAM than it currently does (with lots of users, that could mean a lot of extra RAM). It also means if your mysql server is restarted for any reason, or if your server crashes/loses power, all contents of the table are gone. Having said that, performance improvements on a SELECT from memory table probably isn't going to be all that noticeable, so personally I wouldn't bother, but it'll depend on your requirements.

hth

Re: Updating sessions

Posted: Tue Nov 17, 2009 9:20 am
by Daz
Exactly what I was looking for -thanks!