Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Hopefully) SQL improvements #11926

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mod/pubsub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Worker/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private static function deliverPosts()
*/
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'],
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Nodeinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static function update()

$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]);
Expand Down
4 changes: 2 additions & 2 deletions src/Module/Api/Mastodon/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ protected function rawContent(array $request = [])

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]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Module/Api/Mastodon/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private static function searchHashtags(string $q, bool $exclude_unreviewed, int

$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);

Expand Down
2 changes: 1 addition & 1 deletion src/Module/Api/Mastodon/Timelines/PublicTimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function rawContent(array $request = [])

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);
Expand Down
9 changes: 5 additions & 4 deletions src/Module/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ protected function content(array $request = []): string

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;
case 'hidden':
$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;
Expand All @@ -227,8 +227,9 @@ protected function content(array $request = []): string
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`";
Expand Down Expand Up @@ -275,7 +276,7 @@ protected function content(array $request = []): string
}

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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Module/Conversation/Community.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private static function selectItems($min_id, $max_id, $item_id, $itemspage)
$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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Module/Conversation/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ protected static function getItems(string $table, array $params, array $conditio
$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()]);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Module/Profile/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ protected function content(array $request = []): string

$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]);
Expand Down
32 changes: 16 additions & 16 deletions src/Worker/ExpirePosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,22 @@ private static function deleteUnusedItemUri()
}
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;
Expand Down