]> git.mxchange.org Git - friendica.git/commitdiff
Fix slow queries
authorMichael <heluecht@pirati.ca>
Mon, 1 Mar 2021 22:19:47 +0000 (22:19 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 1 Mar 2021 22:19:47 +0000 (22:19 +0000)
src/Model/Contact.php
src/Model/Item.php
src/Model/Post.php
src/Worker/UpdateContacts.php
static/dbstructure.config.php
update.php

index ae715840eb038e7ac0f4b0f41da34e632bb36150..3b65f2af1ff9f649440f52b2d183c377c44ed2ba 100644 (file)
@@ -1959,7 +1959,7 @@ class Contact
                }
 
                // If Probe::uri fails the network code will be different ("feed" or "unkn")
-               if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
+               if (($ret['network'] == Protocol::PHANTOM) || (($ret['network'] == Protocol::FEED) && ($ret['network'] != $contact['network']))) {
                        self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
                        return false;
                }
index 48652a0e12fbf691f6ceea1bdb42b6bbed7c2838..299736d14db1414b76444eb94291ca3b467182c5 100644 (file)
@@ -970,6 +970,10 @@ class Item
                        unset($item['event-id']);
                }
 
+               if (empty($item['causer-id'])) {
+                       unset($item['causer-id']);
+               }
+
                Post::insert($item['uri-id'], $item);
 
                if ($item['gravity'] == GRAVITY_PARENT) {
index 002ddbc07af78a5d9122df45b8695e625b3722b5..51f74ced5c6c47859aa84237522286d992061097 100644 (file)
@@ -285,7 +285,7 @@ class Post
                $condition = DBA::mergeConditions($condition,
                        ["`visible` AND NOT `deleted`
                        AND NOT `author-blocked` AND NOT `owner-blocked`
-                       AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
+                       AND (NOT `causer-blocked` OR `causer-id` = ? OR `causer-id` IS NULL) AND NOT `contact-blocked`
                        AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
                                OR `self` OR `gravity` != ? OR `contact-uid` = ?)
                        AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `hidden` AND `uri-id` = `" . $view . "`.`uri-id` AND `uid` = ?)
index 699e0f5abbe5e9529d2ee1ce6e25506733e3ea54..86959c88bbbf56cf0e7cd2e6cb1eea4002926fa9 100644 (file)
@@ -49,16 +49,27 @@ class UpdateContacts
                        return;
                }
 
-               // Add every contact our system interacted with and hadn't been updated for a week if unarchived
-               // or for a month if archived.
-               $condition = DBA::mergeConditions($base_condition, ["(`id` IN (SELECT `author-id` FROM `post-user`) OR
-                       `id` IN (SELECT `owner-id` FROM `post-user`) OR `id` IN (SELECT `causer-id` FROM `post-user`) OR
-                       `id` IN (SELECT `cid` FROM `post-tag`) OR `id` IN (SELECT `cid` FROM `user-contact`) OR `uid` != ?) AND
-                       (`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
+               $condition = DBA::mergeConditions($base_condition,
+                       ["`uid` != ? AND (`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
                        0, DateTimeFormat::utc('now - 1 month'), DateTimeFormat::utc('now - 1 week')]);
-               Logger::info('Updatable interacting federated contacts', ['count' => DBA::count('contact', $condition)]);
                $ids = self::getContactsToUpdate($condition, [], $limit);
-               Logger::info('Fetched interacting federated contacts', ['count' => count($ids)]);
+               Logger::info('Fetched federated user contacts', ['count' => count($ids)]);
+
+               $conditions = ["`id` IN (SELECT `author-id` FROM `post-user`)", "`id` IN (SELECT `owner-id` FROM `post-user`)", 
+                       "`id` IN (SELECT `causer-id` FROM `post-user`)", "`id` IN (SELECT `cid` FROM `post-tag`)",
+                       "`id` IN (SELECT `cid` FROM `user-contact`)"];
+
+               foreach ($conditions as $contact_condition) {
+                       $condition = DBA::mergeConditions($base_condition,
+                               [$contact_condition . " AND (`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
+                               DateTimeFormat::utc('now - 1 month'), DateTimeFormat::utc('now - 1 week')]);
+                       $ids = self::getContactsToUpdate($condition, $ids, $limit);
+                       Logger::info('Fetched interacting federated contacts', ['count' => count($ids), 'condition' => $contact_condition]);
+               }
+
+               if (count($ids) > $limit) {
+                       $ids = array_slice($ids, 0, $limit, true);
+               }
 
                if (!DI::config()->get('system', 'update_active_contacts')) {
                        // Add every contact (mostly failed ones) that hadn't been updated for six months
@@ -66,7 +77,6 @@ class UpdateContacts
                        $condition = DBA::mergeConditions($base_condition,
                                ["(`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
                                        DateTimeFormat::utc('now - 6 month'), DateTimeFormat::utc('now - 1 month')]);
-                       Logger::info('Updatable federated contacts', ['count' => DBA::count('contact', $condition)]);
                        $previous = count($ids);
                        $ids = self::getContactsToUpdate($condition, $ids, $limit - $previous);
                        Logger::info('Fetched federated contacts', ['count' => count($ids) - $previous]);
@@ -93,7 +103,7 @@ class UpdateContacts
        {
                $contacts = DBA::select('contact', ['id'], $condition, ['limit' => $limit, 'order' => ['last-update']]);
                while ($contact = DBA::fetch($contacts)) {
-                       $ids[] = $contact['id'];
+                       $ids[$contact['id']] = $contact['id'];
                }
                DBA::close($contacts);
                return $ids;
index cb84e0ba9125ee6dd44349a65dd3963d5005f45c..85be690e275239a366599ae68138866eb5ebf4c2 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1406);
+       define('DB_UPDATE_VERSION', 1407);
 }
 
 return [
@@ -993,7 +993,7 @@ return [
                        "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Network from where the item comes from"],
                        "owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the owner of this item"],
                        "author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the author of this item"],
-                       "causer-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
+                       "causer-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
                        "post-type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Post type (personal note, bookmark, ...)"],
                        "vid" => ["type" => "smallint unsigned", "foreign" => ["verb" => "id", "on delete" => "restrict"], "comment" => "Id of the verb table entry that contains the activity verbs"],
                        "private" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "0=public, 1=private, 2=unlisted"],
@@ -1114,7 +1114,7 @@ return [
                        "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"],
                        "owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item owner"],
                        "author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item author"],
-                       "causer-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
+                       "causer-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
                        "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" => ""],
@@ -1146,7 +1146,7 @@ return [
                        "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Network from where the item comes from"],
                        "owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the owner of this item"],
                        "author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the author of this item"],
-                       "causer-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
+                       "causer-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
                        "post-type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Post type (personal note, bookmark, ...)"],
                        "vid" => ["type" => "smallint unsigned", "foreign" => ["verb" => "id", "on delete" => "restrict"], "comment" => "Id of the verb table entry that contains the activity verbs"],
                        "private" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "0=public, 1=private, 2=unlisted"],
@@ -1194,7 +1194,7 @@ return [
                        "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"],
                        "owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item owner"],
                        "author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item author"],
-                       "causer-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
+                       "causer-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"],
                        "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" => ""],
index b8d357da229e87d4447f7afa0686b6f868478683..a1343255bcb180aa5ebe1c6f098ea0ee497a5582 100644 (file)
@@ -885,3 +885,19 @@ function update_1404()
                DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
        }
 }
+
+function update_1407()
+{
+       if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
+               return Update::FAILED;
+       }
+       if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
+               return Update::FAILED;
+       }
+       if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
+               return Update::FAILED;
+       }
+       if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
+               return Update::FAILED;
+       }
+}