Skip to content

Optimization: skip COUNT query when pagination is disabled#1915

Merged
bkoelman merged 1 commit intomasterfrom
skip-total-query
Feb 3, 2026
Merged

Optimization: skip COUNT query when pagination is disabled#1915
bkoelman merged 1 commit intomasterfrom
skip-total-query

Conversation

@bkoelman
Copy link
Copy Markdown
Member

@bkoelman bkoelman commented Feb 3, 2026

This PR eliminates the SQL COUNT(*) query when pagination is disabled (either via options or the query string) for GET requests. As all resources are being returned in this case, they can simply be counted in-memory to set total in the response.

Additionally, this PR slightly changes the execution order of resource-definition callbacks for certain GET requests and eliminates redundant ones. Before any SQL executes, all callbacks are invoked now. This is needed to determine whether pagination is active, so we can possibly skip the COUNT query. As a result:

  • GET /blogs: No longer calls the first OnApplyFilter, which was used to prepare the COUNT query. Related to that, the IQueryLayerComposer.GetPrimaryFilterFromConstraints method has been marked obsolete because it is no longer used.
  • GET /blogs/1/posts and GET /blogs/1/relationships/posts: OnApplyFilter callbacks for left/right to determine count happen after the callbacks for fetching data. They don't happen at all when the optimization kicks in.

In the unlikely case your resource definition callbacks depend on execution order, it is recommended to verify that nothing broke.

Finally, this PR fixes a bug in the NoEntityFrameworkExample where total was not always returned.

Closes #1752.

QUALITY CHECKLIST

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 71.23288% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.19%. Comparing base (4c0811e) to head (9f05ee1).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ameworkExample/Services/InMemoryResourceService.cs 60.71% 4 Missing and 7 partials ⚠️
...onApiDotNetCore.Annotations/CollectionConverter.cs 50.00% 4 Missing and 1 partial ⚠️
...onApiDotNetCore/Services/JsonApiResourceService.cs 85.71% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1915      +/-   ##
==========================================
- Coverage   92.25%   92.19%   -0.06%     
==========================================
  Files         437      437              
  Lines       14858    14883      +25     
  Branches     2450     2464      +14     
==========================================
+ Hits        13707    13722      +15     
- Misses        708      714       +6     
- Partials      443      447       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bkoelman bkoelman marked this pull request as ready for review February 3, 2026 23:42
@bkoelman bkoelman merged commit df4756d into master Feb 3, 2026
15 checks passed
@bkoelman bkoelman deleted the skip-total-query branch February 3, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Optimization: don't fetch count when pagination disabled

1 participant