Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

38 ms

Page Size

25

Current Page

494 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 494 Results Showing 12326 - 12350 of 24441

ID Name Category Price Stock Rating Reviews SKU
12885 Bulk Product efb9b788 Books $280.58 75 0.1 19 BULK-62b5a36a
12886 Bulk Product a22f3fe2 Books $191.71 358 5.0 41 BULK-c79f5e3a
12887 Bulk Product 62beaeab Sports & Outdoors $977.50 51 2.1 89 BULK-86947856
12888 Bulk Product 403d1a0d Home & Garden $775.02 216 3.0 5 BULK-83efa49c
12889 Bulk Product 3ef3fe34 Toys & Games $868.76 475 3.7 32 BULK-5042bc06
12890 Bulk Product b4b1f94b Books $565.25 128 2.8 87 BULK-d2bdef5e
12891 Bulk Product 9743ce47 Sports & Outdoors $644.98 178 1.8 4 BULK-3a12a913
12892 Bulk Product 4a2df4fc Electronics $778.33 130 2.7 33 BULK-ae4ab1a7
12893 Bulk Product 8adda6f9 Books $991.65 151 2.3 35 BULK-203a2a10
12894 Bulk Product 7ab4fb81 Toys & Games $952.59 272 0.2 20 BULK-774da4b0
12895 Bulk Product 35d21ba1 Home & Garden $740.83 272 0.6 90 BULK-1901aba5
12896 Bulk Product 7de1d845 Toys & Games $617.03 132 1.3 1 BULK-fd117b51
12897 Bulk Product 577b3c56 Electronics $289.70 355 0.4 7 BULK-157543c7
12898 Bulk Product 16b30cc8 Books $988.68 335 2.5 21 BULK-2875b9e8
12899 Bulk Product 2d30a116 Books $640.77 493 1.6 62 BULK-86c3a09a
12900 Bulk Product b539aad3 Home & Garden $908.95 87 1.7 55 BULK-58a1b398
12901 Bulk Product b10aa2d5 Sports & Outdoors $639.34 379 0.7 72 BULK-0a27b31d
12902 Bulk Product 6a13b195 Books $54.27 482 0.6 26 BULK-71928477
12903 Bulk Product 1c49ff1d Home & Garden $269.99 247 1.3 56 BULK-d3b8e6b6
12904 Bulk Product 0e42de8e Electronics $792.40 228 2.5 93 BULK-8c35227f
12905 Bulk Product 9703faf3 Sports & Outdoors $276.20 139 2.5 20 BULK-c6b3b691
12906 Bulk Product 63666b3a Sports & Outdoors $811.12 230 0.6 89 BULK-652eb0ee
12907 Bulk Product 58f88bcc Electronics $402.12 104 1.2 34 BULK-a37bf7c9
12908 Bulk Product ba18c570 Electronics $919.32 494 2.5 44 BULK-289b3a98
12909 Bulk Product 9f0e7ca3 Electronics $796.79 48 1.2 48 BULK-f04b96f9
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