Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

37 ms

Page Size

25

Current Page

720 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 720 Results Showing 17976 - 18000 of 24441

ID Name Category Price Stock Rating Reviews SKU
18535 Bulk Product 013db41e Sports & Outdoors $314.60 189 4.7 41 BULK-8bb3d4bc
18536 Bulk Product e2d32b82 Toys & Games $751.17 224 0.8 94 BULK-0decb21f
18537 Bulk Product 9deb1331 Clothing $713.12 315 0.5 15 BULK-3bf84135
18538 Bulk Product eb95a7f3 Toys & Games $925.34 382 1.5 8 BULK-ea434be6
18539 Bulk Product 0095989a Sports & Outdoors $727.21 325 4.0 62 BULK-3d5b4a2b
18540 Bulk Product 612aeaf5 Electronics $878.21 300 2.6 9 BULK-653a2bd8
18541 Bulk Product fc8bb030 Toys & Games $653.22 191 0.0 54 BULK-1993aa98
18542 Bulk Product b54a51c1 Sports & Outdoors $66.17 465 1.1 33 BULK-1fafdd1b
18543 Bulk Product f5ee25be Home & Garden $337.78 242 3.4 19 BULK-fb7e3327
18544 Bulk Product b8882ff3 Clothing $289.67 89 3.6 1 BULK-b9dab2b3
18545 Bulk Product 7f8be3b8 Toys & Games $193.44 118 1.5 43 BULK-c4162c35
18546 Bulk Product 61ca7b34 Books $188.67 64 0.3 95 BULK-dcf9d7bd
18547 Bulk Product 3e09caf1 Electronics $281.79 281 1.1 19 BULK-38462859
18548 Bulk Product 283d0c99 Electronics $808.62 82 2.5 24 BULK-387e7936
18549 Bulk Product faff6929 Toys & Games $289.61 270 0.9 10 BULK-b6e20e8b
18550 Bulk Product 92921b0f Electronics $722.31 119 1.2 75 BULK-3dff95fd
18551 Bulk Product a4fa400d Sports & Outdoors $490.41 345 3.9 47 BULK-b0a898c4
18552 Bulk Product ed891f73 Home & Garden $325.21 110 4.4 27 BULK-db5b3e8e
18553 Bulk Product ec529ee0 Home & Garden $863.42 31 0.6 80 BULK-4cb94ba8
18554 Bulk Product 22135034 Home & Garden $739.63 258 3.0 50 BULK-f6e607c6
18555 Bulk Product a2ec52c3 Books $205.73 196 2.4 11 BULK-defe1eac
18556 Bulk Product d8ab8632 Toys & Games $224.93 365 4.6 72 BULK-b05fcf62
18557 Bulk Product 4c8e9dfd Sports & Outdoors $737.30 124 0.2 59 BULK-212d6aab
18558 Bulk Product 9d10ea5c Electronics $851.07 164 0.9 70 BULK-fff0a8c1
18559 Bulk Product 2dc921c7 Toys & Games $942.22 79 3.4 52 BULK-bddd85b6
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