Merge pull request #11926 from annando/sql-queries

(Hopefully) SQL improvements
This commit is contained in:
Hypolite Petovan 2022-09-24 15:01:34 -04:00 committed by GitHub
commit 02e9a867ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 34 additions and 35 deletions

View File

@ -145,7 +145,7 @@ function pubsub_post(App $a)
} }
// We only import feeds from OStatus here // 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]); Logger::warning('Unexpected network', ['contact' => $contact]);
hub_post_return(); hub_post_return();
} }

View File

@ -189,7 +189,7 @@ class Cron
*/ */
private static function addIntros() 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)) { while ($contact = DBA::fetch($contacts)) {
$fields = [ $fields = [
'uid' => $contact['uid'], 'uid' => $contact['uid'],

View File

@ -61,9 +61,9 @@ class Nodeinfo
$logger->info('user statistics', $userStats); $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`)"]); $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 EXISTS(SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post`.`uri-id`)", GRAVITY_COMMENT]); $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_posts', $posts);
$config->set('nodeinfo', 'local_comments', $comments); $config->set('nodeinfo', 'local_comments', $comments);
$logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]); $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);

View File

@ -79,13 +79,13 @@ class Notifications extends BaseApi
if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) { if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) {
$condition = DBA::mergeConditions($condition, $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]); Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
} }
if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) { if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) {
$condition = DBA::mergeConditions($condition, $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]); Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
} }

View File

@ -180,7 +180,7 @@ class Search extends BaseApi
$params = ['order' => ['name'], 'limit' => [$offset, $limit]]; $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); $tags = DBA::select('tag', ['name'], $condition, $params);

View File

@ -92,7 +92,7 @@ class PublicTimeline extends BaseApi
if (!empty($uid)) { if (!empty($uid)) {
$condition = DBA::mergeConditions($condition, $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); $items = Post::selectPostsForUser($uid, ['uri-id'], $condition, $params);

View File

@ -210,7 +210,7 @@ class Contact extends BaseModule
switch ($type) { switch ($type) {
case 'blocked': 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 // This makes the query look for contact.uid = 0
array_unshift($sql_values, 0); array_unshift($sql_values, 0);
break; break;
@ -218,7 +218,7 @@ class Contact extends BaseModule
$sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`"; $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
break; break;
case 'ignored': 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 // This makes the query look for contact.uid = 0
array_unshift($sql_values, 0); array_unshift($sql_values, 0);
break; break;
@ -227,8 +227,9 @@ class Contact extends BaseModule
break; break;
case 'pending': case 'pending':
$sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?) $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[] = Model\Contact::SHARING;
$sql_values[] = local_user();
break; break;
default: default:
$sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`"; $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
@ -275,7 +276,7 @@ class Contact extends BaseModule
} }
if ($group) { 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; $sql_values[] = $group;
} }

View File

@ -323,7 +323,7 @@ class Community extends BaseModule
$condition[] = $item_id; $condition[] = $item_id;
} else { } else {
if (local_user() && !empty($_REQUEST['no_sharer'])) { 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(); $condition[] = local_user();
} }

View 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]); $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
} elseif (self::$forumContactId) { } elseif (self::$forumContactId) {
$conditionStrings = DBA::mergeConditions($conditionStrings, $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()]); self::$forumContactId, self::$forumContactId, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()]);
} }

View File

@ -175,10 +175,8 @@ class Status extends BaseProfile
$condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR
(`gravity` = ? AND `vid` = ? AND `origin` (`gravity` = ? AND `vid` = ? AND `origin`
AND EXISTS(SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?) AND `thr-parent-id` IN (SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` = ?)))",
AND `uri-id` = `post-user-view`.`thr-parent-id`)))", GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT, Protocol::ACTIVITYPUB]);
GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT,
Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS]);
$condition = DBA::mergeConditions($condition, ['uid' => $profile['uid'], 'network' => Protocol::FEDERATED, $condition = DBA::mergeConditions($condition, ['uid' => $profile['uid'], 'network' => Protocol::FEDERATED,
'visible' => true, 'deleted' => false]); 'visible' => true, 'deleted' => false]);

View File

@ -179,22 +179,22 @@ class ExpirePosts
} }
Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]); Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]);
$uris = DBA::select('item-uri', ['id'], ["`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 `id` IN (SELECT `uri-id` FROM `post-user`)
AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `parent-uri-id` FROM `post-user`)
AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `thr-parent-id` FROM `post-user`)
AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `external-id` FROM `post-user`)
AND NOT EXISTS(SELECT `conversation-id` FROM `post-thread` WHERE `conversation-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `conversation-id` FROM `post-thread`)
AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `mail`)
AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `event`)
AND NOT EXISTS(SELECT `uri-id` FROM `user-contact` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `user-contact`)
AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `contact`)
AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `apcontact`)
AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `fcontact`)
AND NOT EXISTS(SELECT `uri-id` FROM `inbox-status` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `inbox-status`)
AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`)
AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `inbox-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`)
AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`) AND NOT `id` IN (SELECT `parent-uri-id` FROM `mail`)
AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]); 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']]); Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
$affected_count = 0; $affected_count = 0;