]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #2351 from annando/1602-performance-unread-group
authorTobias Diekershoff <tobias.diekershoff@gmx.net>
Sat, 13 Feb 2016 18:43:33 +0000 (19:43 +0100)
committerTobias Diekershoff <tobias.diekershoff@gmx.net>
Sat, 13 Feb 2016 18:43:33 +0000 (19:43 +0100)
Optimized query for unread group postings

include/group.php
mod/ping.php

index a1375e00dfa678f18368a407583d9d96e0625916..2b872f16a7ce95234bdae038d3546df1f1242147 100644 (file)
@@ -215,7 +215,7 @@ function mini_group_select($uid,$gid = 0) {
 
 /**
  * @brief Create group sidebar widget
- * 
+ *
  * @param string $every
  * @param string $each
  * @param string $editmode
@@ -234,7 +234,7 @@ function group_side($every="contacts",$each="group",$editmode = "standard", $gro
                return '';
 
        $groups = array();
-       
+
        $groups[] = array(
                'text'  => t('Everybody'),
                'id' => 0,
@@ -255,7 +255,7 @@ function group_side($every="contacts",$each="group",$editmode = "standard", $gro
        if(count($r)) {
                foreach($r as $rr) {
                        $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
-                       
+
                        if ($editmode == "full") {
                                $groupedit = array(
                                        'href' => "group/".$rr['id'],
@@ -264,7 +264,7 @@ function group_side($every="contacts",$each="group",$editmode = "standard", $gro
                        } else {
                                $groupedit = null;
                        }
-                       
+
                        $groups[] = array(
                                'id'            => $rr['id'],
                                'cid'           => $cid,
@@ -362,14 +362,13 @@ function groups_containing($uid,$c) {
  */
 function groups_count_unseen() {
 
-       $r = q("SELECT `group`.`id`, `group`.`name`, COUNT(`item`.`id`) AS `count` FROM `group`, `group_member`, `item`
-                       WHERE `group`.`uid` = %d
-                       AND `item`.`uid` = %d
-                       AND `item`.`unseen` AND `item`.`visible`
-                       AND NOT `item`.`deleted`
-                       AND `item`.`contact-id` = `group_member`.`contact-id`
-                       AND `group_member`.`gid` = `group`.`id`
-                       GROUP BY `group`.`id` ",
+       $r = q("SELECT `group`.`id`, `group`.`name`,
+                       (SELECT COUNT(*) FROM `item`
+                               WHERE `uid` = %d AND `unseen` AND
+                                       `contact-id` IN (SELECT `contact-id` FROM `group_member`
+                                                               WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
+                       FROM `group` WHERE `group`.`uid` = %d;",
+               intval(local_user()),
                intval(local_user()),
                intval(local_user())
        );
index 50d179595ef523ea1181a4a21c771b06e8575546..2eb94576b375b47e13277a2f1cd1b7aa36795e9b 100644 (file)
@@ -219,19 +219,21 @@ function ping_init(&$a) {
                                <home>$home</home>\r\n";
                if ($register!=0) echo "<register>$register</register>";
 
-               if ( count($groups_unseen) ) {
+               if (count($groups_unseen)) {
                        echo '<groups>';
-                       foreach ($groups_unseen as $it) {
-                               echo '<group id="' . $it['id'] . '">' . $it['count'] . "</group>";
-                       }
+                       foreach ($groups_unseen as $it)
+                               if ($it['count'] > 0)
+                                       echo '<group id="'.$it['id'].'">'.$it['count']."</group>";
+
                        echo "</groups>";
                }
 
-               if ( count($forums_unseen) ) {
+               if (count($forums_unseen)) {
                        echo '<forums>';
-                       foreach ($forums_unseen as $it) {
-                               echo '<forum id="' . $it['id'] . '">' . $it['count'] . "</forum>";
-                       }
+                       foreach ($forums_unseen as $it)
+                               if ($it['count'] > 0)
+                                       echo '<forum id="'.$it['id'].'">'.$it['count']."</forum>";
+
                        echo "</forums>";
                }