From ebeb3b6c6086d43c39b3d15ac52b194cac7487c0 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 13 Feb 2024 17:35:01 +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 ec9ec3611..0377f9cc6 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]));