Skip to content

Add totalItems caching for server pagination to avoid redundant COUNT queries#2

Merged
dcanob merged 6 commits intomasterfrom
copilot/fix-search-field-disable
Feb 3, 2026
Merged

Add totalItems caching for server pagination to avoid redundant COUNT queries#2
dcanob merged 6 commits intomasterfrom
copilot/fix-search-field-disable

Conversation

Copy link

Copilot AI commented Feb 3, 2026

Server pagination currently requires calculating total item count on every page request. This adds a totalItems optimization that caches the count from the first request and reuses it, resetting only when search changes.

Changes

  • Add totalItems state - Initialized to 0, stores total count across pagination requests
  • Pass totalItems in onServerPage callback - Server receives cached count and can skip COUNT(*) on pages 2+
  • Reset on search change - serverSearchForPagination() resets totalItems = 0 to force recalculation for filtered results
  • Accept totalItems in setServerPaginatedOptions() - Third parameter stores count when provided by server

Server Implementation

function onServerPage(params, virtualSelect) {
  const { page, pageSize, searchValue, totalItems } = params;
  
  // Calculate only if totalItems is 0 (first request or search changed)
  const total = totalItems > 0 ? totalItems : await db.count({ search: searchValue });
  
  const results = await db.find({ search: searchValue })
    .skip((page - 1) * pageSize)
    .limit(pageSize);
  
  virtualSelect.setServerPaginatedOptions(results, page * pageSize < total, total);
}

Behavior

First page: totalItems: 0 → Server calculates → Returns 200
Page 2-N: totalItems: 200 → Server reuses → No COUNT query
Search changes: totalItems: 0 → Server recalculates → Returns filtered count

Screenshots

Initial load with optimization explanation:
Initial

Page 2 showing cached totalItems (no recalculation):
Page 2

Search triggering totalItems reset and recalculation:
Search


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 3, 2026 18:22
Co-authored-by: dcanob <7740721+dcanob@users.noreply.github.com>
Co-authored-by: dcanob <7740721+dcanob@users.noreply.github.com>
Co-authored-by: dcanob <7740721+dcanob@users.noreply.github.com>
Co-authored-by: dcanob <7740721+dcanob@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix search field disable in onServerPage option Fix onServerPage search functionality and scroll preservation Feb 3, 2026
Copilot AI requested a review from dcanob February 3, 2026 18:33
Co-authored-by: dcanob <7740721+dcanob@users.noreply.github.com>
Copilot AI changed the title Fix onServerPage search functionality and scroll preservation Add totalItems caching for server pagination to avoid redundant COUNT queries Feb 3, 2026
@dcanob dcanob marked this pull request as ready for review February 3, 2026 19:13
@dcanob dcanob merged commit a7e4aad into master Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants