From 0331c8f714e7c1af6d4a871cd37f491180668855 Mon Sep 17 00:00:00 2001 From: brettp Date: Sat, 19 Mar 2011 03:48:37 +0000 Subject: Fixes #3164. Denormalized unread message counting query because it was making things so slow I couldn't test. git-svn-id: http://code.elgg.org/elgg/trunk@8763 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/messages/start.php | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'mod/messages') diff --git a/mod/messages/start.php b/mod/messages/start.php index 5c4081a7b..bf8272b1a 100644 --- a/mod/messages/start.php +++ b/mod/messages/start.php @@ -316,15 +316,38 @@ function count_unread_messages() { * @return int */ function messages_count_unread() { + $user_guid = elgg_get_logged_in_user_guid(); + $db_prefix = elgg_get_config('dbprefix'); + + // denormalize the md to speed things up. + // seriously, 10 joins if you don't. + $strings = array('toId', $user_guid, 'readYet', 0, 'msg', 1); + $map = array(); + foreach ($strings as $string) { + $id = get_metastring_id($string); + $map[$string] = $id; + } $options = array( - 'metadata_name_value_pairs' => array( - 'toId' => elgg_get_logged_in_user_guid(), - 'readYet' => 0, - 'msg' => 1 +// 'metadata_name_value_pairs' => array( +// 'toId' => elgg_get_logged_in_user_guid(), +// 'readYet' => 0, +// 'msg' => 1 +// ), + 'joins' => array( + "JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid", + "JOIN {$db_prefix}metadata msg_readYet on e.guid = msg_readYet.entity_guid", + "JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid", + ), + 'wheres' => array( + "msg_toId.name_id='{$map['toId']}' AND msg_toId.value_id='{$map[$user_guid]}'", + "msg_readYet.name_id='{$map['readYet']}' AND msg_readYet.value_id='{$map[0]}'", + "msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'", ), - 'owner_guid' => elgg_get_logged_in_user_guid() + 'owner_guid' => $user_guid, + 'limit' => 0 ); + $num_messages = elgg_get_entities_from_metadata($options); if (is_array($num_messages)) { -- cgit v1.2.3