[DATABASE] Some query improvements

Make common_sql_weight employ standard SQL functions for the timestamp
difference in seconds.
Also replace UTC_TIMESTAMP in the MariaDB-specific part with CURRENT_TIMESTAMP
as it is the only occurence and GNU social sets UTC as a default timezone.

In a delete_orphan_files.php script simplify the main query considerably.

In clean_profiles.php stop using COUNT as if it is ANY, that is unnecessary
punishment for the database. Instead implement the anti-join with a
left outer join.

In Autocomplete and Activitypub_profile use joins instead of a WHERE OR
anti-pattern for the semi-joins.

In lib/ui/galleryaction.php replace a CROSS JOIN with an INNER JOIN.

In actions/sup.php remove a redundant subquery: WHERE is applied before
grouping either way.
This commit is contained in:
Alexei Sorokin
2020-06-10 16:52:00 +03:00
committed by Diogo Peralta Cordeiro
parent a93c38a9af
commit d01f44ee99
9 changed files with 187 additions and 140 deletions

View File

@@ -557,9 +557,15 @@ class Activitypub_profile extends Managed_DataObject
$user_table = common_database_tablename('user');
$sub = new Subscription();
$sub->subscribed = $profile->id;
$sub->_join .= "\n" . <<<END
INNER JOIN (
SELECT id AS subscriber FROM {$user_table}
UNION ALL
SELECT profile_id FROM activitypub_profile
) AS t1 USING (subscriber)
END;
$sub->whereAdd('subscriber <> subscribed');
$sub->whereAdd("subscriber IN (SELECT id FROM {$user_table} UNION SELECT profile_id AS id FROM activitypub_profile)");
$cnt = $sub->count('distinct subscriber');
$cnt = $sub->count('DISTINCT subscriber');
self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt);
@@ -585,9 +591,15 @@ class Activitypub_profile extends Managed_DataObject
$user_table = common_database_tablename('user');
$sub = new Subscription();
$sub->subscriber = $profile->id;
$sub->_join .= "\n" . <<<END
INNER JOIN (
SELECT id AS subscribed FROM {$user_table}
UNION ALL
SELECT profile_id FROM activitypub_profile
) AS t1 USING (subscribed)
END;
$sub->whereAdd('subscriber <> subscribed');
$sub->whereAdd("subscribed IN (SELECT id FROM {$user_table} UNION SELECT profile_id AS id FROM activitypub_profile)");
$cnt = $sub->count('distinct subscribed');
$cnt = $sub->count('DISTINCT subscribed');
self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt);