Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#20 Saving one query per request.

XOOPS_2.2
closed
Jan Pedersen
5
2012-09-25
2004-03-09
Sudhaker Raj
No

Ref: http://www.xoops.org/modules/newbb/viewtopic.
php?topic_id=13891&forum=8#forumpost55849

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'],
$sess_data);
//echo "Debug: $sql
";
sqlite_query($this->db, $sql);
}
return true;

Discussion

  • Jan Pedersen
    Jan Pedersen
    2004-05-05

    Logged In: YES
    user_id=841117

    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
    user_id=882380

    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
    user_id=841117

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

     
  • Jan Pedersen
    Jan Pedersen
    2004-10-14

    Logged In: YES
    user_id=841117

    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
    user_id=841117

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