From f4e4b498bd9ddd12601e44ca661e0767b2897c79 Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Fri, 24 Oct 2025 23:58:15 +0200 Subject: [PATCH] fix: avoid subqueries when calling /messages without parameters The `/messages` endpoint returns the number of matching messages. As an optimization for the default case with no filters, we return an approximate (fast) count of the total number of messages, and accept the performance penalty when specifying filters. The condition to determine whether we go through the fast path was weirdly designed (it depends on an empty `kwargs`) and new features introduced a bug where the fast path is never used anymore. This currently happens because of `start_block` and `end_block`, but also `message_statuses`. This PR fixes the issue for start/end blocks, the issue with message statuses requires more investigation. --- src/aleph/db/accessors/messages.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/aleph/db/accessors/messages.py b/src/aleph/db/accessors/messages.py index 1bc6f37d5..a4dfa3cda 100644 --- a/src/aleph/db/accessors/messages.py +++ b/src/aleph/db/accessors/messages.py @@ -211,6 +211,8 @@ def count_matching_messages( session: DbSession, start_date: float = 0.0, end_date: float = 0.0, + start_block: int = 0, + end_block: int = 0, sort_by: SortBy = SortBy.TIME, sort_order: SortOrder = SortOrder.DESCENDING, page: int = 1, @@ -220,11 +222,13 @@ def count_matching_messages( # Note that we deliberately ignore the pagination parameters so that users can pass # the same parameters as get_matching_messages and get the total number of messages, # not just the number on a page. - if kwargs or start_date or end_date: + if kwargs or start_date or end_date or start_block or end_block: select_stmt = make_matching_messages_query( **kwargs, start_date=start_date, end_date=end_date, + start_block=start_block, + end_block=end_block, include_confirmations=False, page=1, pagination=0,