Improved indexes, avoiding SQL errors, improving speed

This commit is contained in:
Michael 2021-02-20 20:07:25 +00:00
parent d2360d14ed
commit 92a8a399de
8 changed files with 43 additions and 75 deletions

View File

@ -78,7 +78,7 @@ class Item
'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object', 'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network', 'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network', 'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network', 'owner-contact-type',
'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type', 'causer-network', 'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type', 'causer-network',
'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar', 'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
'writable', 'self', 'cid', 'alias', 'writable', 'self', 'cid', 'alias',
@ -2513,12 +2513,11 @@ class Item
* Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
* *
* @param array $item * @param array $item
* @param bool $update
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @todo Remove reference, simply return "rendered-html" and "rendered-hash" * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
*/ */
public static function putInCache(&$item, $update = false) public static function putInCache(&$item)
{ {
// Save original body to prevent addons to modify it // Save original body to prevent addons to modify it
$body = $item['body']; $body = $item['body'];
@ -2542,17 +2541,8 @@ class Item
$item['rendered-hash'] = $hook_data['rendered-hash']; $item['rendered-hash'] = $hook_data['rendered-hash'];
unset($hook_data); unset($hook_data);
// Force an update if the generated values differ from the existing ones // Update if the generated values differ from the existing ones
if ($rendered_hash != $item['rendered-hash']) { if ((($rendered_hash != $item['rendered-hash']) || ($rendered_html != $item['rendered-html'])) && !empty($item['id'])) {
$update = true;
}
// Only compare the HTML when we forcefully ignore the cache
if (DI::config()->get('system', 'ignore_cache') && ($rendered_html != $item['rendered-html'])) {
$update = true;
}
if ($update && !empty($item['id'])) {
self::update( self::update(
[ [
'rendered-html' => $item['rendered-html'], 'rendered-html' => $item['rendered-html'],
@ -2640,15 +2630,7 @@ class Item
unset($hook_data); unset($hook_data);
} }
// Update the cached values if there is no "zrl=..." on the links. self::putInCache($item);
$update = (!Session::isAuthenticated() && ($item["uid"] == 0));
// Or update it if the current viewer is the intented viewer.
if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
$update = true;
}
self::putInCache($item, $update);
$s = $item["rendered-html"]; $s = $item["rendered-html"];
$hook_data = [ $hook_data = [

View File

@ -58,18 +58,14 @@ class Nodeinfo
$config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']); $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
$config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']); $config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']);
$logger->debug('user statistics', $userStats); $logger->info('user statistics', $userStats);
$items = DBA::p("SELECT COUNT(*) AS `total`, `gravity` FROM `post-view` WHERE `origin` AND NOT `deleted` AND `uid` != 0 AND `gravity` IN (?, ?) GROUP BY `gravity`", $posts = DBA::count('post-thread', ["EXISTS(SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin` AND `uri-id` = `post-thread`.`uri-id`)"]);
GRAVITY_PARENT, GRAVITY_COMMENT); $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]);
while ($item = DBA::fetch($items)) { $config->set('nodeinfo', 'local_posts', $posts);
if ($item['gravity'] == GRAVITY_PARENT) { $config->set('nodeinfo', 'local_comments', $comments);
$config->set('nodeinfo', 'local_posts', $item['total']);
} elseif ($item['gravity'] == GRAVITY_COMMENT) { $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);
$config->set('nodeinfo', 'local_comments', $item['total']);
}
}
DBA::close($items);
} }
/** /**

View File

@ -23,6 +23,7 @@ namespace Friendica\Model;
use BadMethodCallException; use BadMethodCallException;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
@ -421,7 +422,7 @@ class Post
{ {
$affected = 0; $affected = 0;
Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition]); Logger::debug('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => local_user(),'callstack' => System::callstack(10)]);
// Don't allow changes to fields that are responsible for the relation between the records // Don't allow changes to fields that are responsible for the relation between the records
unset($fields['id']); unset($fields['id']);

View File

@ -315,13 +315,13 @@ class Community extends BaseModule
{ {
if (self::$content == 'local') { if (self::$content == 'local') {
if (!is_null(self::$accountType)) { if (!is_null(self::$accountType)) {
$condition = ["`wall` AND `origin` AND `private` = ? AND `owner`.`contact-type` = ?", Item::PUBLIC, self::$accountType]; $condition = ["`wall` AND `origin` AND `private` = ? AND `owner-contact-type` = ?", Item::PUBLIC, self::$accountType];
} else { } else {
$condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC]; $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
} }
} elseif (self::$content == 'global') { } elseif (self::$content == 'global') {
if (!is_null(self::$accountType)) { if (!is_null(self::$accountType)) {
$condition = ["`uid` = ? AND `private` = ? AND `owner`.`contact-type` = ?", 0, Item::PUBLIC, self::$accountType]; $condition = ["`uid` = ? AND `private` = ? AND `owner-contact-type` = ?", 0, Item::PUBLIC, self::$accountType];
} else { } else {
$condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC]; $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
} }

View File

@ -181,9 +181,9 @@ 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` AND `thr-parent-id` IN (`gravity` = ? AND `vid` = ? AND `origin` AND `thr-parent-id` IN
(SELECT `uri-id` FROM `post-view` AS `i` (SELECT `uri-id` FROM `post-view`
WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?) AND `uid` IN (?, ?) WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?) AND `uid` IN (?, ?)
AND `i`.`uri-id` = `thr-parent-id`)))", AND `uri-id` = `thr-parent-id`)))",
GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT, GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT,
Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS,
0, $a->profile['uid']]); 0, $a->profile['uid']]);

View File

@ -81,7 +81,7 @@ class Profile extends BaseModule
} }
$items_stmt = DBA::p( $items_stmt = DBA::p(
"SELECT DISTINCT(`parent-uri-id`) AS `uri-id`, `created` FROM `post-view` "SELECT DISTINCT(`parent-uri-id`) AS `uri-id`, MAX(`created`), MAX(`received`) FROM `post-view`
WHERE `uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending` WHERE `uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending`
AND `visible` AND (NOT `deleted` OR `gravity` = ?) AND `visible` AND (NOT `deleted` OR `gravity` = ?)
AND `wall` $sql_extra4 $sql_extra AND `wall` $sql_extra4 $sql_extra

View File

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1404); define('DB_UPDATE_VERSION', 1405);
} }
return [ return [
@ -1002,7 +1002,6 @@ return [
"author-id" => ["author-id"], "author-id" => ["author-id"],
"causer-id" => ["causer-id"], "causer-id" => ["causer-id"],
"vid" => ["vid"], "vid" => ["vid"],
"received" => ["received"],
] ]
], ],
"post-category" => [ "post-category" => [
@ -1110,7 +1109,6 @@ return [
"network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""], "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
"created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"received" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], "received" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"changed" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Date that something in the conversation changed, indicating clients should fetch the conversation again"],
"commented" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""] "commented" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""]
], ],
"indexes" => [ "indexes" => [
@ -1160,26 +1158,24 @@ return [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],
"uid_uri-id" => ["UNIQUE", "uid", "uri-id"], "uid_uri-id" => ["UNIQUE", "uid", "uri-id"],
"uri-id" => ["uri-id"], "uri-id" => ["uri-id"],
"contact-id" => ["contact-id"], "parent-uri-id" => ["parent-uri-id"],
"psid" => ["psid"],
"uid_hidden" => ["uid", "hidden"],
"event-id" => ["event-id"],
"uid_wall" => ["uid", "wall"],
"parent-uri-id_uid" => ["parent-uri-id", "uid"],
"thr-parent-id" => ["thr-parent-id"], "thr-parent-id" => ["thr-parent-id"],
"external-id" => ["external-id"], "external-id" => ["external-id"],
"owner-id" => ["owner-id"], "owner-id" => ["owner-id"],
"author-id_uid" => ["author-id", "uid"], "author-id" => ["author-id"],
"causer-id" => ["causer-id"], "causer-id" => ["causer-id"],
"vid" => ["vid"], "vid" => ["vid"],
"uid_received" => ["uid", "received"], "contact-id" => ["contact-id"],
"event-id" => ["event-id"],
"psid" => ["psid"],
"author-id_uid" => ["author-id", "uid"],
"author-id_received" => ["author-id", "received"],
"parent-uri-id_uid" => ["parent-uri-id", "uid"],
"uid_hidden" => ["uid", "hidden"],
"uid_contactid" => ["uid", "contact-id"],
"uid_unseen_contactid" => ["uid", "unseen", "contact-id"], "uid_unseen_contactid" => ["uid", "unseen", "contact-id"],
"uid_network_received" => ["uid", "network", "received"], "uid_unseen" => ["uid", "unseen"],
"uid_contactid_received" => ["uid", "contact-id", "received"],
"authorid_received" => ["author-id", "received"],
"uid_unseen_wall" => ["uid", "unseen", "wall"], "uid_unseen_wall" => ["uid", "unseen", "wall"],
"uid_eventid" => ["uid", "event-id"],
"psid_wall" => ["psid", "wall"],
], ],
], ],
"post-thread-user" => [ "post-thread-user" => [
@ -1211,30 +1207,21 @@ return [
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["uid", "uri-id"], "PRIMARY" => ["uid", "uri-id"],
"uid_wall" => ["uid", "wall"],
"uid_pinned" => ["uid", "pinned"],
"uri-id" => ["uri-id"], "uri-id" => ["uri-id"],
"owner-id" => ["owner-id"],
"author-id" => ["author-id"],
"causer-id" => ["causer-id"],
"uid" => ["uid"],
"contact-id" => ["contact-id"], "contact-id" => ["contact-id"],
"psid" => ["psid"], "psid" => ["psid"],
"post-user-id" => ["post-user-id"], "post-user-id" => ["post-user-id"],
"owner-id" => ["owner-id"],
"causer-id" => ["causer-id"],
"uid_received" => ["uid", "received"],
"uid_commented" => ["uid", "commented"],
"uid_changed" => ["uid", "changed"],
"uid_contact-id" => ["uid", "contact-id", "received"],
"uid_unseen_contactid" => ["uid", "unseen", "contact-id"],
"uid_network_received" => ["uid", "network", "received"],
"uid_network_commented" => ["uid", "network", "commented"],
"uid_contact-id_received" => ["uid", "contact-id", "received"],
"author-id_received" => ["author-id", "received"],
"uid_wall_changed" => ["uid", "wall", "changed"],
"uid_unseen_wall" => ["uid", "unseen", "wall"],
"mention_uid" => ["mention", "uid"],
"psid_wall" => ["psid", "wall"],
"received" => ["received"],
"commented" => ["commented"], "commented" => ["commented"],
"changed" => ["changed"], "received" => ["received"],
"author-id_received" => ["author-id", "received"],
"uid_pinned" => ["uid", "pinned"],
"uid_commented" => ["uid", "commented"],
"mention_uid" => ["mention", "uid"],
"uid_mention" => ["uid", "mention"],
] ]
], ],
"post-user-notification" => [ "post-user-notification" => [

View File

@ -134,6 +134,7 @@
"owner-network" => ["owner", "network"], "owner-network" => ["owner", "network"],
"owner-blocked" => ["owner", "blocked"], "owner-blocked" => ["owner", "blocked"],
"owner-hidden" => ["owner", "hidden"], "owner-hidden" => ["owner", "hidden"],
"owner-contact-type" => ["owner", "contact-type"],
"causer-id" => ["post-user", "causer-id"], "causer-id" => ["post-user", "causer-id"],
"causer-link" => ["causer", "url"], "causer-link" => ["causer", "url"],
"causer-addr" => ["causer", "addr"], "causer-addr" => ["causer", "addr"],
@ -289,6 +290,7 @@
"owner-network" => ["owner", "network"], "owner-network" => ["owner", "network"],
"owner-blocked" => ["owner", "blocked"], "owner-blocked" => ["owner", "blocked"],
"owner-hidden" => ["owner", "hidden"], "owner-hidden" => ["owner", "hidden"],
"owner-contact-type" => ["owner", "contact-type"],
"causer-id" => ["post-thread-user", "causer-id"], "causer-id" => ["post-thread-user", "causer-id"],
"causer-link" => ["causer", "url"], "causer-link" => ["causer", "url"],
"causer-addr" => ["causer", "addr"], "causer-addr" => ["causer", "addr"],