[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
parent ef17f3ea7d
commit 194976135f
9 changed files with 187 additions and 140 deletions

View File

@@ -1647,14 +1647,16 @@ function common_sql_date($datetime)
*/
function common_sql_weight($column, $dropoff)
{
if (common_config('db', 'type') == 'pgsql') {
// PostgreSQL doesn't support timestampdiff function.
// @fixme will this use the right time zone?
// @fixme does this handle cross-year subtraction correctly?
return "sum(exp(-extract(epoch from (now() - $column)) / $dropoff))";
if (common_config('db', 'type') !== 'mysql') {
$expr = sprintf(
'(((EXTRACT(DAY %1$s) * 24 + EXTRACT(HOUR %1$s)) * 60 + '
. 'EXTRACT(MINUTE %1$s)) * 60 + EXTRACT(SECOND %1$s))',
"FROM ({$column} - CURRENT_TIMESTAMP)"
);
} else {
return "sum(exp(timestampdiff(second, utc_timestamp(), $column) / $dropoff))";
$expr = "timestampdiff(SECOND, CURRENT_TIMESTAMP, {$column})";
}
return "SUM(EXP({$expr} / {$dropoff}))";
}
function common_redirect($url, $code=307)