#20 Saving one query per request.


Ref: http://www.xoops.org/modules/newbb/viewtopic.

Inside the method write() of class XoopsSessionHandler,
a QUERY decides what to run next, INSERT or UPDATE.
So there is always 2 SQL query per session write.

As INSERT will happen just once per session, so trying to
UPDATE first and if it fails then doing INSERT can be more
efficient way of handling the same.

This will save 1 SQL query for any further request.

I tried it on SQLite with this code, working fine.

$sql = sprintf("UPDATE %s SET sess_updated = %u,
sess_data = '%s' WHERE sess_id = '%s'", 'session', time(),
$sess_data, $sess_id);
//echo "Debug: $sql
sqlite_query($this->db, $sql);
if (sqlite_changes($this->db) != 1)
$sql = sprintf("INSERT INTO %s (sess_id, sess_updated,
sess_ip, sess_data) VALUES ('%s', %u, '%s', '%s')",
'session', $sess_id, time(), $_SERVER['REMOTE_ADDR'],
//echo "Debug: $sql
sqlite_query($this->db, $sql);
return true;


  • Jan Pedersen

    Jan Pedersen - 2004-05-05

    Logged In: YES

    I dislike the "try something and if it fails, assume the
    reason and do something else" - but if it works (as I cannot
    see a reason why it shouldn't) and Skalpa/Bunny clear it, I
    can put this one in

  • Skalpa Keo

    Skalpa Keo - 2004-05-12

    Logged In: YES

    OK with your way of thinking Mith, but here I believe to
    assume the number of modified rows equals the number of rows
    that matches the WHERE clause (here the query won't "fail",
    but will modify 0 row) is ok.
    However you made me doubt ;-), so we'll keep that one for
    2.2 so it's released as beta.

  • Jan Pedersen

    Jan Pedersen - 2004-05-13

    Logged In: YES

    OK, I'll take care of this one personally.

  • Jan Pedersen

    Jan Pedersen - 2004-10-14

    Logged In: YES

    Should we pop this into 2.0.9 beta? It's a minor change and
    since 2.0.9 will be a beta at first anyway, we might as well?

  • Jan Pedersen

    Jan Pedersen - 2005-04-08

    Logged In: YES

    Fixed in CVS and waiting for review before merging to the trunk.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks