Skip to content

Commit

Permalink
[core] don't specify ORDER BY when counting total number of results, …
Browse files Browse the repository at this point in the history
…which allows the DB to be much faster
  • Loading branch information
shish committed Feb 13, 2024
1 parent 03ad8b7 commit 139ec52
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions core/imageboard/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]));
Expand Down

0 comments on commit 139ec52

Please sign in to comment.