]> git.mxchange.org Git - friendica.git/commitdiff
(Hopefully) SQL improvements
authorMichael <heluecht@pirati.ca>
Sat, 24 Sep 2022 17:56:07 +0000 (17:56 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 24 Sep 2022 17:56:07 +0000 (17:56 +0000)
mod/pubsub.php
src/Core/Worker/Cron.php
src/Model/Nodeinfo.php
src/Module/Api/Mastodon/Notifications.php
src/Module/Api/Mastodon/Search.php
src/Module/Api/Mastodon/Timelines/PublicTimeline.php
src/Module/Contact.php
src/Module/Conversation/Community.php
src/Module/Conversation/Network.php
src/Module/Profile/Status.php
src/Worker/ExpirePosts.php

index 918c55ee14cb817f192232c675ff112ea79792c9..a740b7b81da89c5e03ea6e5d5e03401c0cfbb25d 100644 (file)
@@ -145,7 +145,7 @@ function pubsub_post(App $a)
        }
 
        // We only import feeds from OStatus here
-       if ($contact['network'] != Protocol::OSTATUS) {
+       if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS])) {
                Logger::warning('Unexpected network', ['contact' => $contact]);
                hub_post_return();
        }
index 9db954298ad3b243e3bfbed71affad1685963986..800f0c8f9a10de87ba22ddbd742658e60cb40967 100644 (file)
@@ -189,7 +189,7 @@ class Cron
         */
        private static function addIntros()
        {
-               $contacts = DBA::p("SELECT `uid`, `id`, `created` FROM `contact` WHERE `rel` = ? AND `pending` AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)", Contact::FOLLOWER);
+               $contacts = DBA::p("SELECT `uid`, `id`, `created` FROM `contact` WHERE `rel` = ? AND `pending` AND NOT `id` IN (SELECT `contact-id` FROM `intro`)", Contact::FOLLOWER);
                while ($contact = DBA::fetch($contacts)) {
                        $fields = [
                                'uid'        => $contact['uid'],
index 84a14a294d5c3ab0f4c2d7c5d90f68bbebddcca7..5ce7f895137910ef9aea271ec3072fc7228a9bd5 100644 (file)
@@ -61,9 +61,9 @@ class Nodeinfo
 
                $logger->info('user statistics', $userStats);
 
-               $posts = DBA::count('post-thread', ["EXISTS(SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin` AND `uri-id` = `post-thread`.`uri-id`)"]);
-               $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND EXISTS(SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post`.`uri-id`)", GRAVITY_COMMENT]);
-               $config->set('nodeinfo', 'local_posts', $posts);
+               $posts = DBA::count('post-thread', ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin`)"]);
+               $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)", GRAVITY_COMMENT]);
+                               $config->set('nodeinfo', 'local_posts', $posts);
                $config->set('nodeinfo', 'local_comments', $comments);
 
                $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);
index 490f4f83d59a4412b53446a81b54360ebf07dc78..4ab71d88f9107c52b4499788cd3d590219e9e408 100644 (file)
@@ -79,13 +79,13 @@ class Notifications extends BaseApi
 
                if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition,
-                               ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))",
+                               ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE `pending`))",
                                Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
                }
 
                if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition,
-                               ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))",
+                               ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE NOT `pending`))",
                                Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
                }
 
index e3f462325ed54abf9cfb2cb1fe2a20254ee02741..27f99f00bf031119f8c1b0f86f9f3160c5ee24f1 100644 (file)
@@ -180,7 +180,7 @@ class Search extends BaseApi
 
                $params = ['order' => ['name'], 'limit' => [$offset, $limit]];
 
-               $condition = ["EXISTS(SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%'];
+               $condition = ["`id` IN (SELECT `tid` FROM `post-tag` WHERE `type` = ?) AND `name` LIKE ?", Tag::HASHTAG, $q . '%'];
 
                $tags = DBA::select('tag', ['name'], $condition, $params);
 
index b851974c380fe57dee7f932a4d82fdc0f97f670b..d34deac079b91d40086564ca0a7b431b2eb08397 100644 (file)
@@ -92,7 +92,7 @@ class PublicTimeline extends BaseApi
 
                if (!empty($uid)) {
                        $condition = DBA::mergeConditions($condition,
-                               ["NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `parent-author-id` AND (`blocked` OR `ignored`))", $uid]);
+                               ["NOT `parent-author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]);
                }
 
                $items = Post::selectPostsForUser($uid, ['uri-id'], $condition, $params);
index ccc61d343897af2befdeebaac4d5dad754a9d69d..05c4e05081254dce75fa3cb78a4c5748a6b0deab 100644 (file)
@@ -210,7 +210,7 @@ class Contact extends BaseModule
 
                switch ($type) {
                        case 'blocked':
-                               $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
+                               $sql_extra = " AND `id` IN (SELECT `cid` FROM `user-contact` WHERE `user-contact`.`uid` = ? AND `user-contact`.`blocked`)";
                                // This makes the query look for contact.uid = 0
                                array_unshift($sql_values, 0);
                                break;
@@ -218,7 +218,7 @@ class Contact extends BaseModule
                                $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
                                break;
                        case 'ignored':
-                               $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
+                               $sql_extra = " AND `id` IN (SELECT `cid` FROM `user-contact` WHERE `user-contact`.`uid` = ? AND `user-contact`.`ignored`)";
                                // This makes the query look for contact.uid = 0
                                array_unshift($sql_values, 0);
                                break;
@@ -227,8 +227,9 @@ class Contact extends BaseModule
                                break;
                        case 'pending':
                                $sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?)
-                                       OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
+                                       OR `id` IN (SELECT `contact-id` FROM `intro` WHERE `intro`.`uid` = ? AND NOT `ignore`))";
                                $sql_values[] = Model\Contact::SHARING;
+                               $sql_values[] = local_user();
                                break;
                        default:
                                $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
@@ -275,7 +276,7 @@ class Contact extends BaseModule
                }
 
                if ($group) {
-                       $sql_extra .= " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
+                       $sql_extra .= " AND `id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)";
                        $sql_values[] = $group;
                }
 
index 16e0f38afd48186be454e44e49b0d69ec7ceffd6..8134c070e388f704e323769a317bf72fb3372bc1 100644 (file)
@@ -323,7 +323,7 @@ class Community extends BaseModule
                        $condition[] = $item_id;
                } else {
                        if (local_user() && !empty($_REQUEST['no_sharer'])) {
-                               $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user-view`.`uri-id` AND `post-user`.`uid` = ?)";
+                               $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ?)";
                                $condition[] = local_user();
                        }
        
index 848f36ce3e35a58dd3fc2273dced9562275ae764..c36876b3479a9be374fafe431f474eef1766c1f2 100644 (file)
@@ -413,7 +413,7 @@ class Network extends BaseModule
                        $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
                } elseif (self::$forumContactId) {
                        $conditionStrings = DBA::mergeConditions($conditionStrings, 
-                               ["((`contact-id` = ?) OR EXISTS(SELECT `uri-id` FROM `post-user-view` WHERE `post-user-view`.`parent-uri-id` = " . DBA::quoteIdentifier($table) . ".`uri-id` AND (`contact-id` = ? AND `gravity` = ? AND `vid` = ? AND `uid` = ?)))",
+                               ["((`contact-id` = ?) OR `uri-id` IN (SELECT `parent-uri-id` FROM `post-user-view` WHERE (`contact-id` = ? AND `gravity` = ? AND `vid` = ? AND `uid` = ?)))",
                                self::$forumContactId, self::$forumContactId, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()]);
                }
 
index f3015692f328439e41633f464b016ff2ab623093..d851d5f474772f2b7485bd3071eb7590c62a4c5c 100644 (file)
@@ -175,10 +175,8 @@ class Status extends BaseProfile
 
                $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR
                        (`gravity` = ? AND `vid` = ? AND `origin`
-                       AND EXISTS(SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?)
-                               AND `uri-id` = `post-user-view`.`thr-parent-id`)))",
-                       GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT,
-                       Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS]);
+                       AND `thr-parent-id` IN (SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` = ?)))",
+                       GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT, Protocol::ACTIVITYPUB]);
 
                $condition = DBA::mergeConditions($condition, ['uid' => $profile['uid'], 'network' => Protocol::FEDERATED,
                        'visible' => true, 'deleted' => false]);
index bbdfaa42c7e94d19d80d0eaaea6d7641f8df0c62..7372ddcc88f40039b6ba7fd4eb18a70671be61f8 100644 (file)
@@ -179,22 +179,22 @@ class ExpirePosts
                }
                Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]);
                $uris = DBA::select('item-uri', ['id'], ["`id` < ?
-                       AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `conversation-id` FROM `post-thread` WHERE `conversation-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `user-contact` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `inbox-status` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `inbox-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
+                       AND NOT `id` IN (SELECT `uri-id` FROM `post-user`)
+                       AND NOT `id` IN (SELECT `parent-uri-id` FROM `post-user`)
+                       AND NOT `id` IN (SELECT `thr-parent-id` FROM `post-user`)
+                       AND NOT `id` IN (SELECT `external-id` FROM `post-user`)
+                       AND NOT `id` IN (SELECT `conversation-id` FROM `post-thread`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `mail`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `event`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `user-contact`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `contact`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `apcontact`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `fcontact`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `inbox-status`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`)
+                       AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`)
+                       AND NOT `id` IN (SELECT `parent-uri-id` FROM `mail`)
+                       AND NOT `id` IN (SELECT `thr-parent-id` FROM `mail`)", $item['uri-id']]);
 
                Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
                $affected_count = 0;