Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

38 ms

Page Size

25

Current Page

268 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 268 Results Showing 6676 - 6700 of 24441

ID Name Category Price Stock Rating Reviews SKU
7235 Bulk Product ad3b58af Electronics $177.60 298 3.4 5 BULK-f3faaae8
7236 Bulk Product a2a593ad Toys & Games $521.40 443 3.0 65 BULK-8b87a999
7237 Bulk Product b17b01bf Electronics $430.76 127 3.1 2 BULK-74d5edb9
7238 Bulk Product 6b2aaa6c Toys & Games $421.50 417 2.6 32 BULK-71de173b
7239 Bulk Product f171ea50 Electronics $1,009.87 405 4.5 59 BULK-adb20717
7240 Bulk Product e046bd39 Electronics $986.63 125 4.6 19 BULK-a1c5522d
7241 Bulk Product c085caa9 Toys & Games $134.60 169 1.4 61 BULK-3b534bb4
7242 Bulk Product 0158273a Sports & Outdoors $730.41 316 2.3 18 BULK-abc67375
7243 Bulk Product db9bfc04 Toys & Games $501.79 116 3.2 7 BULK-de4d9243
7244 Bulk Product 735f7768 Clothing $652.89 163 2.8 6 BULK-5a9811af
7245 Bulk Product 73475ce4 Home & Garden $760.82 325 3.1 4 BULK-f474a1e8
7246 Bulk Product 670d551c Electronics $301.74 445 3.0 98 BULK-339a6f41
7247 Bulk Product 2a29deb4 Sports & Outdoors $475.49 30 3.8 55 BULK-64d5df19
7248 Bulk Product d45ede60 Electronics $445.76 41 2.1 49 BULK-dd58844e
7249 Bulk Product 84db5c30 Home & Garden $362.57 122 0.2 14 BULK-aca0558d
7250 Bulk Product d638bf75 Sports & Outdoors $80.87 302 0.5 83 BULK-ef6b6f7c
7251 Bulk Product 58209a90 Electronics $593.42 395 4.1 63 BULK-9b31089f
7252 Bulk Product b3a7ae7e Books $55.38 66 0.8 88 BULK-ad88e5a5
7253 Bulk Product eca4d5c1 Home & Garden $353.13 27 3.4 52 BULK-860bb77d
7254 Bulk Product 9f67be7c Toys & Games $515.54 180 2.2 19 BULK-33cd11f9
7255 Bulk Product 62e46711 Home & Garden $290.44 170 2.9 57 BULK-6f5924bd
7256 Bulk Product e3fdda11 Sports & Outdoors $824.75 26 2.0 20 BULK-44d076a0
7257 Bulk Product 1b10f579 Toys & Games $54.65 95 4.6 49 BULK-3b28490a
7258 Bulk Product f49dfc66 Clothing $477.05 148 4.6 10 BULK-71dc7c64
7259 Bulk Product d1670cc1 Toys & Games $768.14 92 2.1 69 BULK-2e0a0311
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