]> git.mxchange.org Git - friendica.git/commitdiff
(Hopefully) query performance improvements
authorMichael <heluecht@pirati.ca>
Tue, 1 Nov 2022 06:02:44 +0000 (06:02 +0000)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 1 Nov 2022 12:45:25 +0000 (08:45 -0400)
12 files changed:
src/Model/Contact/Group.php
src/Model/Contact/Relation.php
src/Model/Post.php
src/Module/Api/Mastodon/Timelines/Home.php
src/Module/Api/Mastodon/Timelines/ListTimeline.php
src/Module/Api/Mastodon/Timelines/PublicTimeline.php
src/Module/Api/Mastodon/Timelines/Tag.php
src/Module/Conversation/Community.php
src/Protocol/Diaspora.php
src/Worker/RemoveUnusedAvatars.php
src/Worker/RemoveUnusedContacts.php
src/Worker/RemoveUnusedTags.php

index 52ae70807f72960351e0b0c043c3537ecafaeb7c..5a26fcaa64bc515ffda275fb5a2839592a172039 100644 (file)
@@ -79,7 +79,7 @@ class Group
        {
                return Contact::selectToArray([], ["`uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `failed`
                        AND `id` NOT IN (SELECT DISTINCT(`contact-id`) FROM `group_member` INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
-                               WHERE `group`.`uid` = ?)", $uid, $uid]);
+                               WHERE `group`.`uid` = ? AND `contact-id` = `contact`.`id`)", $uid, $uid]);
        }
 
        /**
index b8fd3aa95b671bdc22611024b2743ecfded4f36b..ae6859cdd927883fa5f314a87dca2a19a61a8752 100644 (file)
@@ -284,7 +284,7 @@ class Relation
                $results = DBA::select('contact', [], ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
                                (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
                                        AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
-                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
+                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
                        AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
                        $cid,
                        0,
@@ -314,7 +314,7 @@ class Relation
                        ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
                                (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
                                        AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
-                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
+                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
                        AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
                        $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
                        Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
@@ -354,7 +354,7 @@ class Relation
 
                // The query returns any contact that isn't followed by that user.
                $results = DBA::select('contact', [],
-                       ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))
+                       ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?) AND `nurl` = `nurl`)
                        AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
                        $uid, Contact::FRIEND, Contact::SHARING, 0, 
                        Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
index 31bd900128eea6f94a6c4bc66cea82aef887615f..635a84efd6fbc7ac4625af896e170ddb5dc2f571 100644 (file)
@@ -400,10 +400,10 @@ class Post
                        AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
                                OR `self` OR `gravity` != ? OR `contact-uid` = ?)
                        AND NOT `" . $view . "`.`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `uid` = ? AND `hidden`)
-                       AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked`)
-                       AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked`)
-                       AND NOT (`gravity` = ? AND `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored`))
-                       AND NOT (`gravity` = ? AND `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored`))",
+                       AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `author-id`)
+                       AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `owner-id`)
+                       AND NOT (`gravity` = ? AND `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `author-id`))
+                       AND NOT (`gravity` = ? AND `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `owner-id`))",
                                0, Contact::SHARING, Contact::FRIEND, Item::GRAVITY_PARENT, 0, $uid, $uid, $uid, Item::GRAVITY_PARENT, $uid, Item::GRAVITY_PARENT, $uid]);
 
                $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
index 3a18baecbf42d4ef17cb9f803afb7d1c61a102b4..f3cbd8d5b53ddad416e053399d6cac9607d81d29 100644 (file)
@@ -82,7 +82,7 @@ class Home extends BaseApi
                }
 
                if ($request['remote']) {
-                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
+                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `post-user-view`.`uri-id`)"]);
                }
 
                if ($request['exclude_replies']) {
index c786e3d9dc69c29ebb29f20e6c0dbe6aacecd91e..df6fdb39d896a87f4c09e604334a58bcebdec9cd 100644 (file)
@@ -91,7 +91,7 @@ class ListTimeline extends BaseApi
                }
 
                if ($request['remote']) {
-                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
+                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `post-user-view`.`uri-id`)"]);
                }
 
                $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
index cc2392caff88781731e727622a5dd2e606b8f625..066c4b83faf5a67e0e1ed2c568487a5ef4053c46 100644 (file)
@@ -65,7 +65,7 @@ class PublicTimeline extends BaseApi
                }
 
                if ($request['remote']) {
-                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
+                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `post-view`.`uri-id`)"]);
                }
 
                if ($request['only_media']) {
@@ -92,7 +92,7 @@ class PublicTimeline extends BaseApi
 
                if (!empty($uid)) {
                        $condition = DBA::mergeConditions($condition,
-                               ["NOT `parent-author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]);
+                               ["NOT `parent-author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`) AND `cid` = `parent-author-id`)", $uid]);
                }
 
                $items = Post::selectPostsForUser($uid, ['uri-id'], $condition, $params);
index bc4464b566e1f3cb3054cf2647031ab657117527..2049d92075a815b76ee634cfe0ad710c7d9cf715 100644 (file)
@@ -77,7 +77,7 @@ class Tag extends BaseApi
                }
 
                if ($request['remote']) {
-                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
+                       $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `tag-search-view`.`uri-id`)"]);
                }
 
                if ($request['only_media']) {
index b2cbf10146877ccd1a588e78d528284478cc70ff..7e6730f8334b07620afbecbf97b8ddb36b6d5f83 100644 (file)
@@ -335,7 +335,7 @@ class Community extends BaseModule
                        $condition[] = $item_id;
                } else {
                        if (DI::userSession()->getLocalUserId() && !empty($_REQUEST['no_sharer'])) {
-                               $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ?)";
+                               $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-thread-user-view`.`uri-id`)";
                                $condition[] = DI::userSession()->getLocalUserId();
                        }
        
index ffffaf6199a02e0177d2b05fdb0e97d550bf56ff..e7b66a222d3351d8c0ae6d558eba6a14f9d93433 100644 (file)
@@ -54,7 +54,8 @@ use GuzzleHttp\Psr7\Uri;
 use SimpleXMLElement;
 
 /**
- * This class contain functions to create and send Diaspora XML files
+ * This class contain functions to communicate via the Diaspora protocol
+ * @see https://diaspora.github.io/diaspora_federation/
  */
 class Diaspora
 {
index 4591fb22bf5b7174a376b02b3d7487bd011f3be1..27a6ac676f39e0d9dec53e3c8a444354c5f64785 100644 (file)
@@ -37,11 +37,11 @@ class RemoveUnusedAvatars
                $sql = "FROM `contact` INNER JOIN `photo` ON `contact`.`id` = `contact-id`
                        WHERE `contact`.`uid` = ? AND NOT `self` AND (`photo` != ? OR `thumb` != ? OR `micro` != ?)
                                AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
-                               AND NOT `contact`.`id` IN (SELECT `author-id` FROM `post-user`)
-                               AND NOT `contact`.`id` IN (SELECT `owner-id` FROM `post-user`)
-                               AND NOT `contact`.`id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL)
-                               AND NOT `contact`.`id` IN (SELECT `cid` FROM `post-tag`)
-                               AND NOT `contact`.`id` IN (SELECT `contact-id` FROM `post-user`);";
+                               AND NOT `contact`.`id` IN (SELECT `author-id` FROM `post-user` WHERE `author-id` = `contact`.`id`)
+                               AND NOT `contact`.`id` IN (SELECT `owner-id` FROM `post-user` WHERE `owner-id` = `contact`.`id`)
+                               AND NOT `contact`.`id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL AND `causer-id` = `contact`.`id`)
+                               AND NOT `contact`.`id` IN (SELECT `cid` FROM `post-tag` WHERE `cid` = `contact`.`id`)
+                               AND NOT `contact`.`id` IN (SELECT `contact-id` FROM `post-user` WHERE `contact-id` = `contact`.`id`);";
 
                $ret = DBA::fetchFirst("SELECT COUNT(*) AS `total` " . $sql, 0, '', '', '', 0);
                $total = $ret['total'] ?? 0;
index 038376c22c925d199edca8d6d49ec13b905e63fe..1cbb3775cd89e197b52f8ca3f6667f6643331ac9 100644 (file)
@@ -39,10 +39,14 @@ class RemoveUnusedContacts
        {
                $condition = ["`id` != ? AND `uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
                        AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < ?))
-                       AND NOT `id` IN (SELECT `author-id` FROM `post-user`) AND NOT `id` IN (SELECT `owner-id` FROM `post-user`)
-                       AND NOT `id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL) AND NOT `id` IN (SELECT `cid` FROM `post-tag`)
-                       AND NOT `id` IN (SELECT `contact-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `user-contact`)
-                       AND NOT `id` IN (SELECT `cid` FROM `event`) AND NOT `id` IN (SELECT `contact-id` FROM `group_member`)
+                       AND NOT `id` IN (SELECT `author-id` FROM `post-user` WHERE `author-id` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `owner-id` FROM `post-user` WHERE `owner-id` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL AND `causer-id` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `cid` FROM `post-tag` WHERE `cid` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `contact-id` FROM `post-user` WHERE `contact-id` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `cid` FROM `user-contact` WHERE `cid` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `cid` FROM `event` WHERE `cid` = `contact`.`id`)
+                       AND NOT `id` IN (SELECT `contact-id` FROM `group_member` WHERE `contact-id` = `contact`.`id`)
                        AND `created` < ?",
                        0, 0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, DateTimeFormat::utc('now - 365 days'), DateTimeFormat::utc('now - 30 days')];
 
index 90ebd673fcebb86cd0f32531658459c146a3c69c..ecb724c38183a5531e6e32d7160cb7eea6a4ec63 100644 (file)
@@ -30,6 +30,6 @@ class RemoveUnusedTags
         */
        public static function execute()
        {
-               DBA::delete('tag', ["NOT `id` IN (SELECT `tid` FROM `post-category`) AND NOT `id` IN (SELECT `tid` FROM `post-tag`)"]);
+               DBA::delete('tag', ["NOT `id` IN (SELECT `tid` FROM `post-category` WHERE `tid` = `tag`.`id`) AND NOT `id` IN (SELECT `tid` FROM `post-tag` WHERE `tid` = `tag`.`id`)"]);
        }
 }