Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

64 ms

Page Size

25

Current Page

784 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 784 Results Showing 19576 - 19600 of 24441

ID Name Category Price Stock Rating Reviews SKU
20135 Bulk Product a14129ff Clothing $871.61 142 2.9 2 BULK-5c339672
20136 Bulk Product 060c9e71 Clothing $389.57 188 4.2 92 BULK-c9968990
20137 Bulk Product a53c7d03 Sports & Outdoors $976.90 482 3.6 72 BULK-505c9bd8
20138 Bulk Product bb11f24b Sports & Outdoors $581.93 48 4.6 56 BULK-85c3e60c
20139 Bulk Product 0b05b998 Clothing $301.99 66 1.1 76 BULK-8304c7a2
20140 Bulk Product 90c31c71 Clothing $742.45 424 1.8 42 BULK-03dc5ae1
20141 Bulk Product 8cd8732c Sports & Outdoors $311.87 293 0.4 24 BULK-e519a607
20142 Bulk Product 1e4cc122 Home & Garden $225.14 426 4.3 76 BULK-33d4e745
20143 Bulk Product 1cea2a5f Home & Garden $469.22 219 4.2 27 BULK-e0eae553
20144 Bulk Product 3e1c3cfc Books $533.97 235 2.4 66 BULK-75df8fd9
20145 Bulk Product ce1fc102 Sports & Outdoors $316.82 136 2.8 10 BULK-8ba61497
20146 Bulk Product 40721b36 Toys & Games $200.58 87 1.8 56 BULK-3f12a73a
20147 Bulk Product 363cce53 Sports & Outdoors $884.09 284 3.4 26 BULK-30412f47
20148 Bulk Product a4c7203f Electronics $219.68 38 0.5 89 BULK-806f5013
20149 Bulk Product eb57ce40 Clothing $630.37 215 2.8 46 BULK-eba64f6d
20150 Bulk Product b4bea90b Clothing $893.85 278 3.6 57 BULK-39ae6ac2
20151 Bulk Product 7c5457e0 Electronics $860.77 309 2.8 34 BULK-71e4801d
20152 Bulk Product 61e80596 Sports & Outdoors $153.79 274 2.6 79 BULK-a8699783
20153 Bulk Product 0443c280 Home & Garden $591.17 410 3.8 80 BULK-21a71253
20154 Bulk Product 33e4f3ac Toys & Games $417.94 486 2.3 92 BULK-38e576f4
20155 Bulk Product ddcc948a Books $135.41 249 2.7 76 BULK-08b9918a
20156 Bulk Product 901655c3 Clothing $105.62 75 4.3 20 BULK-87bc9554
20157 Bulk Product d7832a80 Clothing $402.47 259 1.9 50 BULK-c1c0c2c1
20158 Bulk Product cbbc75e8 Home & Garden $678.87 200 4.9 13 BULK-eac49edc
20159 Bulk Product 76923520 Clothing $230.72 280 2.7 4 BULK-e74ebd8b
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