]> git.mxchange.org Git - friendica.git/commitdiff
Improved indexes, avoiding SQL errors, improving speed
authorMichael <heluecht@pirati.ca>
Sat, 20 Feb 2021 20:07:25 +0000 (20:07 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 20 Feb 2021 20:07:25 +0000 (20:07 +0000)
src/Model/Item.php
src/Model/Nodeinfo.php
src/Model/Post.php
src/Module/Conversation/Community.php
src/Module/Profile/Status.php
src/Module/Update/Profile.php
static/dbstructure.config.php
static/dbview.config.php

index 9046674e7b33afb1c65608c425f806eea283252d..10f69cfcef5864fbfc2ca495db48c5b0a32a8ded 100644 (file)
@@ -78,7 +78,7 @@ class Item
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                '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',
                'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
                '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
         *
         * @param array $item
-        * @param bool  $update
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @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
                $body = $item['body'];
@@ -2542,17 +2541,8 @@ class Item
                        $item['rendered-hash'] = $hook_data['rendered-hash'];
                        unset($hook_data);
 
-                       // Force an update if the generated values differ from the existing ones
-                       if ($rendered_hash != $item['rendered-hash']) {
-                               $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'])) {
+                       // Update if the generated values differ from the existing ones
+                       if ((($rendered_hash != $item['rendered-hash']) || ($rendered_html != $item['rendered-html'])) && !empty($item['id'])) {
                                self::update(
                                        [
                                                'rendered-html' => $item['rendered-html'],
@@ -2640,15 +2630,7 @@ class Item
                        unset($hook_data);
                }
 
-               // Update the cached values if there is no "zrl=..." on the links.
-               $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);
+               self::putInCache($item);
                $s = $item["rendered-html"];
 
                $hook_data = [
index ef22c0dddfa06f2af02206332b5a74f21e57c93d..5240bc0802595fdb2388c4b8f290e5e67daf379b 100644 (file)
@@ -58,18 +58,14 @@ class Nodeinfo
                $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
                $config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']);
 
-               $logger->debug('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`",
-                       GRAVITY_PARENT, GRAVITY_COMMENT);
-               while ($item = DBA::fetch($items)) {
-                       if ($item['gravity'] == GRAVITY_PARENT) {
-                               $config->set('nodeinfo', 'local_posts', $item['total']);
-                       } elseif ($item['gravity'] == GRAVITY_COMMENT) {
-                               $config->set('nodeinfo', 'local_comments', $item['total']);
-                       }
-               }
-               DBA::close($items);
+               $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);
+               $config->set('nodeinfo', 'local_comments', $comments);
+
+               $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);
        }
 
        /**
index e3114ca90995e05b44d5698ecb7743c56b54aac2..572dde96f229fbc2e4c91bd4e97ced308d1171ff 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Model;
 
 use BadMethodCallException;
 use Friendica\Core\Logger;
+use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
@@ -421,7 +422,7 @@ class Post
        {
                $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
                unset($fields['id']);
index 026c31f1d1272effff62ede6ff5a03a36e80eef0..dc4a7c9918dd4ff1493eb8e66c6bd1978424a8bf 100644 (file)
@@ -315,13 +315,13 @@ class Community extends BaseModule
        {
                if (self::$content == 'local') {
                        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 {
                                $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
                        }
                } elseif (self::$content == 'global') {
                        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 {
                                $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
                        }
index 267e3c1892d1786ec4929246923ec30af168f4d2..7c3875e46ce1884ae9421ac7d6b6692f3bb69d62 100644 (file)
@@ -181,9 +181,9 @@ class Status extends BaseProfile
 
                $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR
                        (`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 (?, ?)
-                                               AND `i`.`uri-id` = `thr-parent-id`)))",
+                                               AND `uri-id` = `thr-parent-id`)))",
                        GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT,
                        Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS,
                        0, $a->profile['uid']]);
index bab1a63a344b08a1b30f7027605f9643bf4239b1..8dcd88e6ac47f043e96de19d3c80e18023f687da 100644 (file)
@@ -81,7 +81,7 @@ class Profile extends BaseModule
                }
 
                $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`
                                AND `visible` AND (NOT `deleted` OR `gravity` = ?)
                                AND `wall` $sql_extra4 $sql_extra
index 8013b928f11a89fc7c396b0a1fac4115ad62c46e..ebb55b5d57f9e32353ba092d524a66cc26da4e0f 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1404);
+       define('DB_UPDATE_VERSION', 1405);
 }
 
 return [
@@ -1002,7 +1002,6 @@ return [
                        "author-id" => ["author-id"],
                        "causer-id" => ["causer-id"],
                        "vid" => ["vid"],
-                       "received" => ["received"],
                ]
        ],
        "post-category" => [
@@ -1110,7 +1109,6 @@ return [
                        "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
                        "created" => ["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" => ""]
                ],
                "indexes" => [
@@ -1160,26 +1158,24 @@ return [
                        "PRIMARY" => ["id"],
                        "uid_uri-id" => ["UNIQUE", "uid", "uri-id"],
                        "uri-id" => ["uri-id"],
-                       "contact-id" => ["contact-id"],
-                       "psid" => ["psid"],
-                       "uid_hidden" => ["uid", "hidden"],
-                       "event-id" => ["event-id"],
-                       "uid_wall" => ["uid", "wall"],
-                       "parent-uri-id_uid" => ["parent-uri-id", "uid"],
+                       "parent-uri-id" => ["parent-uri-id"],
                        "thr-parent-id" => ["thr-parent-id"],
                        "external-id" => ["external-id"],
                        "owner-id" => ["owner-id"],
-                       "author-id_uid" => ["author-id", "uid"],
+                       "author-id" => ["author-id"],
                        "causer-id" => ["causer-id"],
                        "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_network_received" => ["uid", "network", "received"],
-                       "uid_contactid_received" => ["uid", "contact-id", "received"],
-                       "authorid_received" => ["author-id", "received"],
+                       "uid_unseen" => ["uid", "unseen"],
                        "uid_unseen_wall" => ["uid", "unseen", "wall"],
-                       "uid_eventid" => ["uid", "event-id"],
-                       "psid_wall" => ["psid", "wall"],
                ],
        ],
        "post-thread-user" => [
@@ -1211,30 +1207,21 @@ return [
                ],
                "indexes" => [
                        "PRIMARY" => ["uid", "uri-id"],
-                       "uid_wall" => ["uid", "wall"],
-                       "uid_pinned" => ["uid", "pinned"],
                        "uri-id" => ["uri-id"],
+                       "owner-id" => ["owner-id"],
+                       "author-id" => ["author-id"],
+                       "causer-id" => ["causer-id"],
+                       "uid" => ["uid"],
                        "contact-id" => ["contact-id"],
                        "psid" => ["psid"],
                        "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"],
+                       "commented" => ["commented"],
+                       "received" => ["received"],
                        "author-id_received" => ["author-id", "received"],
-                       "uid_wall_changed" => ["uid", "wall", "changed"],
-                       "uid_unseen_wall" => ["uid", "unseen", "wall"],
+                       "uid_pinned" => ["uid", "pinned"],
+                       "uid_commented" => ["uid", "commented"],
                        "mention_uid" => ["mention", "uid"],
-                       "psid_wall" => ["psid", "wall"],
-                       "received" => ["received"],
-                       "commented" => ["commented"],
-                       "changed" => ["changed"],
+                       "uid_mention" => ["uid", "mention"],
                ]
        ],
        "post-user-notification" => [
index 5e5630fc960d0833ef34823db8eee6b6777bd65c..9ee4bbb62b585c98391c5f1862a6607e862d52de 100644 (file)
                        "owner-network" => ["owner", "network"],
                        "owner-blocked" => ["owner", "blocked"],
                        "owner-hidden" => ["owner", "hidden"],
+                       "owner-contact-type" => ["owner", "contact-type"],
                        "causer-id" => ["post-user", "causer-id"],
                        "causer-link" => ["causer", "url"],
                        "causer-addr" => ["causer", "addr"],
                        "owner-network" => ["owner", "network"],
                        "owner-blocked" => ["owner", "blocked"],
                        "owner-hidden" => ["owner", "hidden"],
+                       "owner-contact-type" => ["owner", "contact-type"],
                        "causer-id" => ["post-thread-user", "causer-id"],
                        "causer-link" => ["causer", "url"],
                        "causer-addr" => ["causer", "addr"],