From 139ec526e2a8ca4b2dc813a7cb6e19623e8e8302 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 13 Feb 2024 17:47:09 +0000 Subject: [PATCH] [core] don't specify ORDER BY when counting total number of results, which allows the DB to be much faster --- core/imageboard/search.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/imageboard/search.php b/core/imageboard/search.php index f5f4c7c66..34f0dcc86 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -174,7 +174,7 @@ public static function count_images(array $tags = []): int $total = $cache->get($cache_key); if (is_null($total)) { [$tag_conditions, $img_conditions, $order] = self::terms_to_conditions($tags); - $querylet = self::build_search_querylet($tag_conditions, $img_conditions, $order); + $querylet = self::build_search_querylet($tag_conditions, $img_conditions, null); $total = (int)$database->get_one("SELECT COUNT(*) AS cnt FROM ($querylet->sql) AS tbl", $querylet->variables); if (SPEED_HAX && $total > 5000) { // when we have a ton of images, the count @@ -240,7 +240,7 @@ private static function terms_to_conditions(array $terms): array private static function build_search_querylet( array $tag_conditions, array $img_conditions, - string $order, + ?string $order = null, ?int $limit = null, ?int $offset = null ): Querylet { @@ -414,7 +414,9 @@ private static function build_search_querylet( $query->append(new Querylet($img_sql, $img_vars)); } - $query->append(new Querylet(" ORDER BY ".$order)); + if(!is_null($order)) { + $query->append(new Querylet(" ORDER BY ".$order)); + } if (!is_null($limit)) { $query->append(new Querylet(" LIMIT :limit ", ["limit" => $limit]));