Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

29 ms

Page Size

25

Current Page

688 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 688 Results Showing 17176 - 17200 of 24441

ID Name Category Price Stock Rating Reviews SKU
17735 Bulk Product eedb1793 Home & Garden $991.26 177 2.4 6 BULK-a582bb7f
17736 Bulk Product 38497f6b Books $228.05 434 0.6 59 BULK-9ed8f234
17737 Bulk Product dcfa82d4 Clothing $375.32 205 2.5 28 BULK-975cd2ec
17738 Bulk Product 9eb7888f Home & Garden $663.71 495 1.1 22 BULK-af0efddd
17739 Bulk Product 7952027b Toys & Games $350.88 245 2.5 15 BULK-9c168f78
17740 Bulk Product 460f344e Clothing $130.20 61 3.6 92 BULK-bf0f37dc
17741 Bulk Product dc355bc0 Books $973.53 486 2.5 79 BULK-185a6530
17742 Bulk Product 0b8e6927 Sports & Outdoors $364.09 141 0.6 33 BULK-c728b7c1
17743 Bulk Product 3e9cdabf Books $860.17 2 2.6 11 BULK-ef2ca797
17744 Bulk Product 29054d41 Sports & Outdoors $763.81 416 3.7 8 BULK-a80d588e
17745 Bulk Product 4fdf3071 Toys & Games $875.37 437 4.8 25 BULK-1de86846
17746 Bulk Product 03a97f95 Books $325.25 385 1.5 86 BULK-f61c49df
17747 Bulk Product c298634e Clothing $530.03 253 0.2 23 BULK-2128c67e
17748 Bulk Product 7e697ba1 Electronics $426.64 381 4.2 78 BULK-42c57865
17749 Bulk Product 0d6c4737 Toys & Games $462.48 408 0.9 76 BULK-1359f06b
17750 Bulk Product faf3dae1 Toys & Games $176.83 418 4.5 85 BULK-913a34bd
17751 Bulk Product 243ef342 Clothing $696.05 457 4.6 9 BULK-8738073e
17752 Bulk Product 8b3ca519 Home & Garden $902.65 241 2.6 94 BULK-574ededc
17753 Bulk Product 73f77c3d Electronics $954.70 18 3.1 23 BULK-79f57b4a
17754 Bulk Product c2dae8d2 Sports & Outdoors $560.17 406 1.1 78 BULK-36253a4f
17755 Bulk Product cc24bd41 Electronics $518.06 140 1.0 35 BULK-87acfdab
17756 Bulk Product 5f0d9fd8 Home & Garden $498.96 178 1.2 83 BULK-f513bb41
17757 Bulk Product 5f5f89c7 Toys & Games $863.50 366 0.7 89 BULK-f86d2d3b
17758 Bulk Product b5267e38 Sports & Outdoors $100.23 111 1.2 48 BULK-edbc947a
17759 Bulk Product 88a96325 Toys & Games $863.21 254 0.9 97 BULK-5ad7ad6a
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