Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

12 ms

Page Size

25

Current Page

401 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 401 Results Showing 10001 - 10025 of 24441

ID Name Category Price Stock Rating Reviews SKU
10560 Bulk Product 82349e94 Home & Garden $604.97 81 2.8 54 BULK-620c15f8
10561 Bulk Product 6c42173b Home & Garden $675.15 283 2.5 1 BULK-b9f64b26
10562 Bulk Product 5971cafc Home & Garden $926.15 457 2.4 49 BULK-a8bfb027
10563 Bulk Product faebefa6 Clothing $601.33 404 4.4 24 BULK-55861901
10564 Bulk Product 156238f1 Clothing $805.14 44 4.7 20 BULK-21577d46
10565 Bulk Product 3f1453d8 Home & Garden $496.55 87 3.9 41 BULK-51bcbfc1
10566 Bulk Product 2365a6e9 Clothing $385.21 58 4.2 19 BULK-aef1fd05
10567 Bulk Product 1f4c05a0 Electronics $562.45 129 3.3 1 BULK-36b43c7e
10568 Bulk Product 06aee662 Sports & Outdoors $995.06 14 4.0 6 BULK-126c7a8a
10569 Bulk Product 601038dd Toys & Games $506.95 270 0.3 9 BULK-6a9c23d5
10570 Bulk Product 3cb2fb8a Clothing $132.40 414 0.5 39 BULK-5b9c4a2a
10571 Bulk Product 3a10f6f4 Books $121.16 52 3.8 72 BULK-b64163a6
10572 Bulk Product 77b46bdb Electronics $266.28 41 4.6 25 BULK-47271e63
10573 Bulk Product bf3e9e23 Clothing $620.18 463 2.2 14 BULK-6fb82eda
10574 Bulk Product 83984ebe Books $918.99 130 1.8 30 BULK-368860ba
10575 Bulk Product 9ecb88d3 Electronics $218.62 82 0.6 26 BULK-a770f06b
10576 Bulk Product 65c9bf35 Sports & Outdoors $772.87 413 4.9 51 BULK-4d5ebb5e
10577 Bulk Product 16c0e3d7 Home & Garden $629.15 310 1.4 46 BULK-75a93951
10578 Bulk Product a704c214 Clothing $842.71 220 4.7 69 BULK-e6391718
10579 Bulk Product ae15cd30 Books $229.81 405 4.0 46 BULK-98208a4c
10580 Bulk Product da3c2fd1 Clothing $854.14 388 2.1 75 BULK-e28673bd
10581 Bulk Product 05047122 Toys & Games $118.62 281 1.2 88 BULK-cb5e0cb3
10582 Bulk Product 8f80f31b Toys & Games $478.25 56 2.5 97 BULK-7542e074
10583 Bulk Product 1cde1cc6 Home & Garden $335.51 233 1.7 65 BULK-c39a32a8
10584 Bulk Product cbcf3ebd Electronics $315.78 212 0.6 99 BULK-e4ca11ed
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