Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

19 ms

Page Size

25

Current Page

336 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 336 Results Showing 8376 - 8400 of 24441

ID Name Category Price Stock Rating Reviews SKU
8935 Bulk Product 70627606 Electronics $20.03 255 1.0 22 BULK-4a29fcb9
8936 Bulk Product 82c77d0f Sports & Outdoors $41.38 189 0.0 49 BULK-f2526fb9
8937 Bulk Product 2954f6ff Books $358.82 165 3.2 94 BULK-564241ae
8938 Bulk Product d6cfab39 Electronics $848.39 112 0.2 78 BULK-5e2dd8dd
8939 Bulk Product cbf35bb8 Electronics $336.69 211 4.8 97 BULK-5b764076
8940 Bulk Product c5ecc782 Clothing $397.39 135 0.4 87 BULK-362ca206
8941 Bulk Product 287a191e Sports & Outdoors $219.96 131 3.9 72 BULK-2cb19e11
8942 Bulk Product cab75f87 Books $672.66 247 4.5 58 BULK-98d7c8c4
8943 Bulk Product fef36f06 Sports & Outdoors $1,000.06 295 2.8 4 BULK-5d17ae20
8944 Bulk Product 79dcd7f5 Electronics $459.14 473 0.9 75 BULK-6cc3b2f4
8945 Bulk Product cd81a7e6 Sports & Outdoors $360.37 217 3.3 96 BULK-f2d33a00
8946 Bulk Product 36de0714 Clothing $746.96 229 0.7 51 BULK-2b048d21
8947 Bulk Product d980ac4b Toys & Games $334.91 234 3.5 7 BULK-849c6743
8948 Bulk Product 79048073 Clothing $700.32 362 3.3 59 BULK-1ad304b6
8949 Bulk Product daf8118f Home & Garden $602.44 83 4.1 49 BULK-9d04563e
8950 Bulk Product 440fd013 Home & Garden $820.36 192 4.4 5 BULK-d9ae5078
8951 Bulk Product 5180029b Home & Garden $926.17 297 4.7 15 BULK-e9424580
8952 Bulk Product 130c0e82 Books $383.86 315 0.1 96 BULK-46c3422b
8953 Bulk Product 3f9a1811 Clothing $354.03 129 2.1 41 BULK-73dcd0f2
8954 Bulk Product 0ec4716c Clothing $763.78 277 4.2 10 BULK-2c950cc8
8955 Bulk Product 067b9d2e Books $895.05 275 2.5 0 BULK-4248f812
8956 Bulk Product 06831358 Toys & Games $220.05 498 0.0 85 BULK-d82c0a50
8957 Bulk Product 3c92621e Electronics $339.81 89 2.8 76 BULK-6738b70a
8958 Bulk Product 6c9c9f29 Toys & Games $36.70 134 4.3 37 BULK-73028e26
8959 Bulk Product 09b74ff2 Clothing $755.09 247 2.2 27 BULK-cae87a02
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