Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

11 ms

Page Size

25

Current Page

2 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 2 Results Showing 26 - 50 of 24441

ID Name Category Price Stock Rating Reviews SKU
29 Elite Sports & Outdoors Product 2024 Sports & Outdoors $148.04 128 2.7 602 SKU-000029
30 Premium Electronics Product Pro Electronics $100.96 435 2.2 553 SKU-000030
31 Mega Clothing Product 2024 Clothing $115.74 458 4.8 163 SKU-000031
32 Pro Health & Beauty Product Classic Health & Beauty $996.94 321 2.1 405 SKU-000032
33 Pro Sports & Outdoors Product Advanced Sports & Outdoors $736.58 432 4.6 304 SKU-000033
34 Deluxe Books Product Pro Books $656.69 40 2.9 600 SKU-000034
35 Ultra Sports & Outdoors Product XL Sports & Outdoors $976.95 253 1.4 744 SKU-000035
36 Mega Books Product Advanced Books $972.71 135 2.6 970 SKU-000036
37 Standard Home & Garden Product Advanced Home & Garden $753.83 219 2.4 911 SKU-000037
38 Ultra Sports & Outdoors Product XL Sports & Outdoors $854.28 377 4.0 463 SKU-000038
39 Premium Office Supplies Product Special Edition Office Supplies $416.94 389 3.0 671 SKU-000039
40 Mega Food & Grocery Product Max Food & Grocery $783.32 334 1.5 411 SKU-000040
41 Super Automotive Product Advanced Automotive $386.05 167 2.3 737 SKU-000041
43 Ultra Sports & Outdoors Product Mini Sports & Outdoors $537.08 121 2.7 151 SKU-000043
44 Standard Health & Beauty Product Max Health & Beauty $918.69 399 4.2 798 SKU-000044
45 Ultra Home & Garden Product Special Edition Home & Garden $384.17 85 2.5 213 SKU-000045
46 Premium Sports & Outdoors Product 2024 Sports & Outdoors $727.36 3 3.6 769 SKU-000046
47 Deluxe Automotive Product Special Edition Automotive $252.24 430 2.4 761 SKU-000047
48 Elite Clothing Product Max Clothing $517.25 321 1.4 799 SKU-000048
49 Professional Electronics Product XL Electronics $186.36 322 2.2 667 SKU-000049
50 Ultra Sports & Outdoors Product Max Sports & Outdoors $155.47 101 1.8 487 SKU-000050
52 Economy Home & Garden Product 2024 Home & Garden $537.02 413 4.6 611 SKU-000052
53 Standard Electronics Product XL Electronics $469.07 43 4.0 613 SKU-000053
54 Economy Sports & Outdoors Product XL Sports & Outdoors $803.02 470 2.7 437 SKU-000054
56 Super Automotive Product Pro Automotive $432.23 181 2.1 516 SKU-000056
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