Improved the performance of reading tags without a specific language id
authorAlexander Ebert <ebert@woltlab.com>
Sat, 22 Aug 2020 21:05:23 +0000 (23:05 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 22 Aug 2020 21:05:23 +0000 (23:05 +0200)
wcfsetup/install/files/lib/system/tagging/TagEngine.class.php

index db14f9297673171e8342217ab8d614995ef35a90..3578026f68e12f572f5c60a01e075f26fa19d02d 100644 (file)
@@ -6,6 +6,7 @@ use wcf\data\tag\TagAction;
 use wcf\data\tag\TagList;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\InvalidObjectTypeException;
+use wcf\system\language\LanguageFactory;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
 use wcf\util\ArrayUtil;
@@ -163,9 +164,17 @@ class TagEngine extends SingletonFactory {
                                if (!$languageID) unset($languageIDs[$index]);
                        }
                        
-                       if (!empty($languageIDs)) {
-                               $conditions->add("tag_to_object.languageID IN (?)", [$languageIDs]);
+                       // The `languageID` is part of the index, skipping it will cause MySQL to skip the
+                       // `objectID` column, causing a partial table scan.
+                       if (empty($languageIDs)) {
+                               // The `languageID` column is never null, tags are always assigned to a language
+                               // thus we cannot use the content language ids here.
+                               foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
+                                       $languageIDs[] = $language->languageID;
+                               }
                        }
+                       
+                       $conditions->add("tag_to_object.languageID IN (?)", [$languageIDs]);
                }
                
                $sql = "SELECT          tag.*, tag_to_object.objectID