Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

16 ms

Page Size

25

Current Page

200 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 200 Results Showing 4976 - 5000 of 24441

ID Name Category Price Stock Rating Reviews SKU
5535 Bulk Product 9267e58c Clothing $845.89 135 2.6 47 BULK-3518ed8f
5536 Bulk Product 3992ca26 Home & Garden $863.85 450 4.4 84 BULK-1f852dc8
5537 Bulk Product 99c704f4 Books $803.46 259 1.7 83 BULK-262b4947
5538 Bulk Product 0b0e650c Toys & Games $93.15 172 0.3 89 BULK-b247affb
5539 Bulk Product 189e5aee Home & Garden $427.44 447 4.7 32 BULK-68f9b043
5540 Bulk Product 97fba9f6 Toys & Games $867.88 434 3.4 25 BULK-1ca2efff
5541 Bulk Product 66ea4784 Books $536.77 237 5.0 77 BULK-9aa7366d
5542 Bulk Product 836fcfaf Sports & Outdoors $81.47 114 1.9 68 BULK-677d8602
5543 Bulk Product 23a8f36d Clothing $409.77 161 3.8 30 BULK-a470c630
5544 Bulk Product 8134e7f6 Toys & Games $301.58 15 1.9 96 BULK-a83b59fd
5545 Bulk Product 9e9a8f34 Toys & Games $903.02 460 1.0 85 BULK-5e712a1c
5546 Bulk Product 9f90d1eb Electronics $889.63 496 4.3 98 BULK-dda33f06
5547 Bulk Product 9fda4b0d Sports & Outdoors $44.76 147 0.8 88 BULK-ecd489a2
5548 Bulk Product b6d32c3f Clothing $412.99 11 4.8 98 BULK-4f498b73
5549 Bulk Product cb5b42ff Books $94.28 429 2.8 23 BULK-fa4c7ae7
5550 Bulk Product 664b44a6 Sports & Outdoors $88.60 40 0.5 59 BULK-0262afd5
5551 Bulk Product 21f84907 Electronics $50.05 402 4.6 68 BULK-c78bdd60
5552 Bulk Product 2daa795b Clothing $679.13 36 3.9 63 BULK-1d7d7707
5553 Bulk Product bcbe3181 Sports & Outdoors $124.08 228 4.9 46 BULK-5130aed5
5554 Bulk Product 7a08c0b3 Electronics $104.16 274 3.9 81 BULK-ef06ad66
5555 Bulk Product c24ecebd Toys & Games $771.12 336 1.4 97 BULK-7bf682d8
5556 Bulk Product c622891d Toys & Games $729.35 293 3.5 94 BULK-a89776c0
5557 Bulk Product 5877e02f Electronics $840.74 357 0.5 14 BULK-022faed2
5558 Bulk Product a5d67d54 Sports & Outdoors $289.57 194 2.5 53 BULK-ef7accca
5559 Bulk Product e9022bcb Clothing $326.81 145 2.1 87 BULK-d31aae00
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