Commit 4b3117e4 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add column for unread message counts to the usr table (bug #854403)



The unread message counts are expensive to query in postgres when
notification_internal_activity gets big.  This results in very slow
logins (the first time the query is run).  This patch adds a new
column for each user to store the unread message count.

The counts are updated using a trigger on insert, update and delete
of the notification_internal_activity table.

Change-Id: Ifb24c6892567658584f45825437bb7a8f20cc310
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 12d5900f
......@@ -141,6 +141,7 @@
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="false" />
<FIELD NAME="showhomeinfo" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" />
<FIELD NAME="logintries" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="unread" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" />
<!-- If adding a datetime column, you should also update the relevant
functions in htdocs/auth/user.php and htdocs/lib/user.php to convert the
value to a timestamp when a record is retrieved -->
......
......@@ -2790,5 +2790,12 @@ function xmldb_core_upgrade($oldversion=0) {
add_field($table, $field);
}
if ($oldversion < 2012021000) {
$table = new XMLDBTable('usr');
$field = new XMLDBField('unread');
$field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, 0);
add_field($table, $field);
}
return $status;
}
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2012011300;
$config->version = 2012021000;
$config->release = '1.5.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
......@@ -37,6 +37,48 @@ function xmldb_notification_internal_upgrade($oldversion=0) {
);
}
if ($oldversion < 2012021000) {
// Populate the unread count on the usr table
if (is_postgres()) {
execute_sql('
UPDATE {usr} SET unread = n.unread FROM (
SELECT usr, SUM(1 - read) AS unread FROM {notification_internal_activity} GROUP BY usr
) n WHERE {usr}.id = n.usr;'
);
}
else if (is_mysql()) {
execute_sql('
UPDATE {usr} u, (SELECT usr, SUM(1 - "read") AS unread FROM {notification_internal_activity} GROUP BY usr) n
SET u.unread = n.unread
WHERE u.id = n.usr'
);
}
// Create triggers to maintain the unread count
db_create_trigger(
'update_unread_insert',
'AFTER', 'INSERT', 'notification_internal_activity', '
IF NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_update',
'AFTER', 'UPDATE', 'notification_internal_activity', '
IF OLD.read = 0 AND NEW.read = 1 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = NEW.usr;
ELSEIF OLD.read = 1 AND NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_delete',
'AFTER', 'DELETE', 'notification_internal_activity', '
IF OLD.read = 0 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = OLD.usr;
END IF;'
);
}
return true;
}
......@@ -59,6 +59,36 @@ class PluginNotificationInternal extends PluginNotification {
return insert_record('notification_internal_activity', $toinsert, 'id', true);
}
public static function postinst($prevversion) {
if ($prevversion == 0) {
// Add triggers to update user unread message count when updating
// notification_internal_activity
db_create_trigger(
'update_unread_insert',
'AFTER', 'INSERT', 'notification_internal_activity', '
IF NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_update',
'AFTER', 'UPDATE', 'notification_internal_activity', '
IF OLD.read = 0 AND NEW.read = 1 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = NEW.usr;
ELSEIF OLD.read = 1 AND NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_delete',
'AFTER', 'DELETE', 'notification_internal_activity', '
IF OLD.read = 0 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = OLD.usr;
END IF;'
);
}
}
/**
* this method is only implemented in internal & is used for the header
*/
......
......@@ -28,5 +28,5 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2011112300;
$config->release = '1.0.2';
$config->version = 2012021000;
$config->release = '1.0.3';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment