Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

36 ms

Page Size

25

Current Page

921 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 921 Results Showing 23001 - 23025 of 24441

ID Name Category Price Stock Rating Reviews SKU
23560 Bulk Product c98b1d86 Home & Garden $112.78 440 4.0 27 BULK-55be5a62
23561 Bulk Product 4afcc0c7 Toys & Games $516.54 493 0.1 38 BULK-f07aeaf6
23562 Bulk Product 60d28910 Toys & Games $725.91 60 0.1 28 BULK-070843c2
23563 Bulk Product f005bb24 Toys & Games $641.72 285 3.3 83 BULK-9c8e0d21
23564 Bulk Product 5e42cef4 Toys & Games $729.35 217 1.0 84 BULK-f70c76b0
23565 Bulk Product ba167d62 Sports & Outdoors $540.34 71 0.3 53 BULK-38f4f7d4
23566 Bulk Product 8410f4c9 Electronics $156.65 224 4.9 87 BULK-df8d2cd7
23567 Bulk Product abb949dd Books $102.44 264 2.6 3 BULK-537975be
23568 Bulk Product ea9af1cb Clothing $73.84 473 0.1 67 BULK-d2a1d2f7
23569 Bulk Product 8d33005c Clothing $19.18 33 2.6 3 BULK-76b7b466
23570 Bulk Product b2439a49 Clothing $244.32 8 1.8 76 BULK-2170d263
23571 Bulk Product c5e4d050 Clothing $341.92 476 3.3 82 BULK-f9e768e2
23572 Bulk Product 87f99c28 Electronics $688.62 257 2.4 50 BULK-95c0b256
23573 Bulk Product 97238d3c Clothing $181.94 184 1.8 85 BULK-cb97eb36
23574 Bulk Product 7976e2b2 Clothing $869.20 204 1.8 35 BULK-f20b1b75
23575 Bulk Product e5dc569c Sports & Outdoors $269.44 71 4.3 42 BULK-f5c6bbc0
23576 Bulk Product aca31baf Books $208.44 22 2.4 40 BULK-4316b60c
23577 Bulk Product 80e75052 Electronics $140.30 72 0.6 28 BULK-498f2c4c
23578 Bulk Product efc1ed11 Sports & Outdoors $414.53 367 3.8 66 BULK-9ce7e676
23579 Bulk Product 889a8e1e Books $133.42 349 1.2 24 BULK-af160e3d
23580 Bulk Product 9844db0e Electronics $59.42 101 2.9 78 BULK-09a3beac
23581 Bulk Product ebd421f3 Electronics $419.46 295 3.6 33 BULK-0f968643
23582 Bulk Product 77060560 Home & Garden $363.34 165 1.5 81 BULK-8ea2e99b
23583 Bulk Product 5cc5f88d Toys & Games $329.51 107 4.8 84 BULK-1198b460
23584 Bulk Product e917dfd3 Sports & Outdoors $545.74 108 1.6 61 BULK-f625a14f
Implementation Code
// Efficient pagination with Skip and Take
public async Task<PaginatedResult<DemoProduct>> GetPaginatedProductsAsync(int page, int pageSize)
{
    // Get total count (cached if possible)
    var totalCount = await _context.DemoProducts.CountAsync(p => p.IsActive);
    
    // Get page of results
    var items = await _context.DemoProducts
        .Where(p => p.IsActive)
        .OrderBy(p => p.Id) // Important: Must have ORDER BY for consistent paging
        .Skip((page - 1) * pageSize) // OFFSET in SQL
        .Take(pageSize) // FETCH NEXT in SQL
        .AsNoTracking() // No tracking needed for read-only
        .ToListAsync();

    return new PaginatedResult<DemoProduct>
    {
        Items = items,
        TotalCount = totalCount,
        Page = page,
        PageSize = pageSize
    };
}
Pagination Best Practices
  • Always use OrderBy: Skip/Take require stable sorting for consistent results
  • Use AsNoTracking(): Pagination queries are typically read-only
  • Consider caching: Cache total count if dataset changes infrequently
  • Limit max page size: Prevent users from requesting too many records at once
  • Use indexed columns: Ensure ORDER BY columns are indexed for performance