]> git.mxchange.org Git - friendica.git/commitdiff
Performance: Avoid queries where there is no media or category
authorMichael <heluecht@pirati.ca>
Mon, 16 May 2022 17:06:58 +0000 (17:06 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 16 May 2022 17:06:58 +0000 (17:06 +0000)
database.sql
src/Content/Item.php
src/Model/Item.php
src/Model/Post/Media.php
static/dbstructure.config.php
static/dbview.config.php

index b9e9d284f2ed4669c3104cb894cc29b49592e8ad..f2f9a89e2e35ecaf500bce30b92ea37648ffb82d 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2022.05-rc (Siberian Iris)
--- DB_UPDATE_VERSION 1463
+-- DB_UPDATE_VERSION 1464
 -- ------------------------------------------
 
 
@@ -1816,6 +1816,8 @@ CREATE VIEW `post-user-view` AS SELECT
        `post-question`.`multiple` AS `question-multiple`,
        `post-question`.`voters` AS `question-voters`,
        `post-question`.`end-time` AS `question-end-time`,
+       EXISTS(SELECT `uri-id` FROM `post-category` WHERE `post-category`.`uri-id` = `post-user`.`uri-id` AND `post-category`.`uid` = `post-user`.`uid`) AS `has-categories`,
+       EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-user`.`uri-id`) AS `has-media`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
@@ -1985,6 +1987,8 @@ CREATE VIEW `post-thread-user-view` AS SELECT
        `post-question`.`multiple` AS `question-multiple`,
        `post-question`.`voters` AS `question-voters`,
        `post-question`.`end-time` AS `question-end-time`,
+       EXISTS(SELECT `uri-id` FROM `post-category` WHERE `post-category`.`uri-id` = `post-thread-user`.`uri-id` AND `post-category`.`uid` = `post-thread-user`.`uid`) AS `has-categories`,
+       EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread-user`.`uri-id`) AS `has-media`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
@@ -2120,6 +2124,8 @@ CREATE VIEW `post-view` AS SELECT
        `post-question`.`multiple` AS `question-multiple`,
        `post-question`.`voters` AS `question-voters`,
        `post-question`.`end-time` AS `question-end-time`,
+       0 AS `has-categories`,
+       EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post`.`uri-id`) AS `has-media`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
@@ -2251,6 +2257,8 @@ CREATE VIEW `post-thread-view` AS SELECT
        `post-question`.`multiple` AS `question-multiple`,
        `post-question`.`voters` AS `question-voters`,
        `post-question`.`end-time` AS `question-end-time`,
+       0 AS `has-categories`,
+       EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`) AS `has-media`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
index 240de4c741cba9f63a7a5dddbda42e4077a2c474..99dda25a031ecd5cf77578a823d7d219352a056b 100644 (file)
@@ -93,7 +93,7 @@ class Item
 
                $uid = $item['uid'] ?: $uid;
 
-               if (!Post\Category::existsForURIId($item['uri-id'], $uid)) {
+               if (empty($item['has-categories'])) {
                        return [$categories, $folders];
                }
 
index 2435e15c2a2d549c60f8863d9dfe8d24da32fb6d..c88b35a5d2ec625666bb96c3df2bc443cc013456 100644 (file)
@@ -97,6 +97,7 @@ class Item
                'event-summary', 'event-desc', 'event-location', 'event-type',
                'event-nofinish', 'event-ignore', 'event-id',
                "question-id", "question-multiple", "question-voters", "question-end-time",
+               "has-categories", "has-media",
                'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed'
        ];
 
@@ -2788,7 +2789,7 @@ class Item
                        $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
                        $shared_uri_id = $shared_item['uri-id'] ?? 0;
                        $shared_links = [strtolower($shared_item['plink'] ?? '')];
-                       $shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared['guid']);
+                       $shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared['guid'], [], $item['has-media']);
                        $shared_links = array_merge($shared_links, array_column($shared_attachments['visual'], 'url'));
                        $shared_links = array_merge($shared_links, array_column($shared_attachments['link'], 'url'));
                        $shared_links = array_merge($shared_links, array_column($shared_attachments['additional'], 'url'));
@@ -2797,7 +2798,7 @@ class Item
                        $shared_uri_id = 0;
                        $shared_links = [];
                }
-               $attachments = Post\Media::splitAttachments($item['uri-id'], $item['guid'] ?? '', $shared_links);
+               $attachments = Post\Media::splitAttachments($item['uri-id'], $item['guid'] ?? '', $shared_links, $item['has-media']);
                $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? '');
 
                $item['body'] = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", "\n", $item['body']);
index b783fb6a4fc6c8532c1c2a639ea82c9b7952dce2..78f27b7a099252380b09821bc3c170d9f2dd17a3 100644 (file)
@@ -547,12 +547,17 @@ class Media
         * @param int    $uri_id
         * @param string $guid
         * @param array  $links list of links that shouldn't be added
+        * @param bool   $has_media
         * @return array attachments
         */
-       public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
+       public static function splitAttachments(int $uri_id, string $guid = '', array $links = [], bool $has_media = true)
        {
                $attachments = ['visual' => [], 'link' => [], 'additional' => []];
 
+               if (!$has_media) {
+                       return $attachments;
+               }
+
                $media = self::getByURIId($uri_id);
                if (empty($media)) {
                        return $attachments;
index f13523960329491c61ca62affbb4050c96f3b96c..bf85bb56c793c62020fbe5f13585efad0d60df1e 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1463);
+       define('DB_UPDATE_VERSION', 1464);
 }
 
 return [
index e2e29d8f4f0f17bab7c5a9170a50521cf5954f17..30e17baf81abf82a8bba95c6d6f7626858fdf712 100644 (file)
                        "question-multiple" => ["post-question", "multiple"],
                        "question-voters" => ["post-question", "voters"],
                        "question-end-time" => ["post-question", "end-time"],
+                       "has-categories" => "EXISTS(SELECT `uri-id` FROM `post-category` WHERE `post-category`.`uri-id` = `post-user`.`uri-id` AND `post-category`.`uid` = `post-user`.`uid`)",
+                       "has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-user`.`uri-id`)",
                        "signed_text" => ["diaspora-interaction", "interaction"],
                        "parent-guid" => ["parent-item-uri", "guid"],
                        "parent-network" => ["parent-post", "network"],
                        "question-multiple" => ["post-question", "multiple"],
                        "question-voters" => ["post-question", "voters"],
                        "question-end-time" => ["post-question", "end-time"],
+                       "has-categories" => "EXISTS(SELECT `uri-id` FROM `post-category` WHERE `post-category`.`uri-id` = `post-thread-user`.`uri-id` AND `post-category`.`uid` = `post-thread-user`.`uid`)",
+                       "has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread-user`.`uri-id`)",
                        "signed_text" => ["diaspora-interaction", "interaction"],
                        "parent-guid" => ["parent-item-uri", "guid"],
                        "parent-network" => ["parent-post", "network"],
                        "question-multiple" => ["post-question", "multiple"],
                        "question-voters" => ["post-question", "voters"],
                        "question-end-time" => ["post-question", "end-time"],
+                       "has-categories" => "0",
+                       "has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post`.`uri-id`)",
                        "signed_text" => ["diaspora-interaction", "interaction"],
                        "parent-guid" => ["parent-item-uri", "guid"],
                        "parent-network" => ["parent-post", "network"],
                        "question-multiple" => ["post-question", "multiple"],
                        "question-voters" => ["post-question", "voters"],
                        "question-end-time" => ["post-question", "end-time"],
+                       "has-categories" => "0",
+                       "has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`)",
                        "signed_text" => ["diaspora-interaction", "interaction"],
                        "parent-guid" => ["parent-item-uri", "guid"],
                        "parent-network" => ["parent-post", "network"],