Huge speedup at login: split the session building query to avoid the left
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 25 Sep 2007 14:39:04 +0000 (16:39 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 25 Sep 2007 14:39:04 +0000 (16:39 +0200)
join with logger.sessions.

This can reduce the SQL query time from 1.9s to 0.1s for people who have a
lot of sessions registered in logger.sessions

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
include/emails.inc.php
include/xorg/session.inc.php

index 325c5ab..f5b8514 100644 (file)
@@ -373,8 +373,8 @@ class Redirect
     public function get_broken_mx()
     {
         $res = XDB::query("SELECT  host, text
-            FROM  mx_watch
-            WHERE  state != 'ok'");
+                             FROM  mx_watch
+                            WHERE  state != 'ok'");
         if (!$res->numRows()) {
             return array();
         }
index 35de42e..b86436f 100644 (file)
@@ -241,18 +241,23 @@ function start_connexion ($uid, $identified)
     $res  = XDB::query("
         SELECT  u.user_id AS uid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie,
                 matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
-                UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
+                a.alias AS forlife, a2.alias AS bestalias,
                 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
                 FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version
           FROM  auth_user_md5   AS u
     INNER JOIN  auth_user_quick AS q  USING(user_id)
     INNER JOIN  aliases         AS a  ON (u.user_id = a.id AND a.type='a_vie')
     INNER JOIN  aliases         AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
-     LEFT JOIN  logger.sessions AS s  ON (s.uid=u.user_id AND s.suid=0)
-         WHERE  u.user_id = {?} AND u.perms IN('admin','user')
-      ORDER BY  s.start DESC
-         LIMIT  1", $uid);
+         WHERE  u.user_id = {?} AND u.perms IN('admin','user')", $uid);
     $sess = $res->fetchOneAssoc();
+    $res->free();
+    $res = XDB::query("SELECT  UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
+                         FROM  logger.sessions AS s
+                        WHERE  s.uid = {?} AND s.suid = 0
+                     ORDER BY  s.start DESC
+                        LIMIT  1", $uid);
+    $sess = array_merge($sess, $res->fetchOneAssoc());
+    $res->free();
     $suid = S::v('suid');
 
     if ($suid) {