Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

23 ms

Page Size

25

Current Page

517 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 517 Results Showing 12901 - 12925 of 24441

ID Name Category Price Stock Rating Reviews SKU
13460 Bulk Product 9f0b6496 Electronics $74.17 168 1.6 84 BULK-f31bbe41
13461 Bulk Product e0c8ee5b Sports & Outdoors $160.70 280 4.8 36 BULK-8dab21db
13462 Bulk Product 05ab2532 Home & Garden $719.73 337 0.4 84 BULK-ab103c48
13463 Bulk Product 8eda690e Books $892.57 235 3.9 65 BULK-23da4f9e
13464 Bulk Product 0aa54731 Clothing $75.59 156 2.1 56 BULK-9303fc7c
13465 Bulk Product 17bda66e Clothing $576.71 472 1.7 42 BULK-4060ddd8
13466 Bulk Product bda8e149 Electronics $357.02 489 3.1 54 BULK-18128ee8
13467 Bulk Product 81256204 Electronics $185.36 497 1.7 67 BULK-fb291e05
13468 Bulk Product fd362e2c Sports & Outdoors $290.86 186 4.2 6 BULK-56e27950
13469 Bulk Product e819b69b Sports & Outdoors $975.16 429 1.8 7 BULK-0b65f9a4
13470 Bulk Product 3a37cc8b Sports & Outdoors $504.87 270 4.5 64 BULK-d7289627
13471 Bulk Product 3a5c1db2 Home & Garden $582.11 293 2.7 1 BULK-50648545
13472 Bulk Product f1b499d2 Sports & Outdoors $574.96 264 4.1 85 BULK-a179dde7
13473 Bulk Product 350dd1da Sports & Outdoors $960.79 307 2.5 78 BULK-4c7f5791
13474 Bulk Product 233b69a4 Electronics $717.85 373 4.9 89 BULK-e3313766
13475 Bulk Product 8ced5b8d Electronics $500.97 417 2.8 77 BULK-8411a784
13476 Bulk Product 676c4e90 Electronics $107.11 100 2.3 46 BULK-40748710
13477 Bulk Product 105813c2 Electronics $522.70 398 1.7 97 BULK-3879e06e
13478 Bulk Product 3f04c433 Home & Garden $956.77 99 1.7 77 BULK-3ac01cda
13479 Bulk Product 6db0b3d9 Electronics $373.16 499 2.3 4 BULK-713e852e
13480 Bulk Product a43ecb27 Toys & Games $1,004.43 92 1.9 81 BULK-209af744
13481 Bulk Product 2aef411d Sports & Outdoors $229.87 185 4.7 82 BULK-70eeb679
13482 Bulk Product 84f4ddca Home & Garden $367.98 331 1.1 24 BULK-7b9d3f9e
13483 Bulk Product 9ae98314 Books $326.03 207 1.7 27 BULK-de2d4cfc
13484 Bulk Product f1985a0d Toys & Games $998.79 396 0.2 18 BULK-c9785128
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