Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

1801 ms

Page Size

25

Current Page

302 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 302 Results Showing 7526 - 7550 of 24441

ID Name Category Price Stock Rating Reviews SKU
8085 Bulk Product 23fc41f0 Electronics $907.01 79 0.3 69 BULK-35430d6d
8086 Bulk Product e78f1acd Books $919.88 260 4.1 10 BULK-74e0fa4a
8087 Bulk Product 42a78f05 Books $287.54 373 2.0 47 BULK-27d1a30f
8088 Bulk Product 98f6e57c Clothing $822.78 221 2.2 94 BULK-4aa26ed3
8089 Bulk Product e8dd3459 Sports & Outdoors $34.74 255 5.0 54 BULK-d528e22a
8090 Bulk Product d5d04812 Electronics $642.36 66 1.2 7 BULK-c3e1fcea
8091 Bulk Product a5b0462b Toys & Games $713.61 299 4.2 79 BULK-f5d79d93
8092 Bulk Product ef8a7a9d Clothing $331.30 441 4.4 44 BULK-532731e2
8093 Bulk Product db049548 Home & Garden $815.46 69 3.8 78 BULK-b33f5608
8094 Bulk Product bd8425a9 Books $304.63 496 3.2 17 BULK-6d7fae5f
8095 Bulk Product f771a766 Sports & Outdoors $1,005.42 238 4.3 95 BULK-66847d2c
8096 Bulk Product a5bbe5c8 Home & Garden $963.79 142 4.3 23 BULK-e5efddad
8097 Bulk Product f461ec4c Electronics $744.56 134 3.3 61 BULK-d25d7f97
8098 Bulk Product b6165521 Sports & Outdoors $521.93 49 2.5 16 BULK-08b78fa0
8099 Bulk Product 3fcf29b2 Home & Garden $571.97 158 2.5 13 BULK-5a401e66
8100 Bulk Product 79a99437 Home & Garden $662.42 315 2.4 63 BULK-88bde65d
8101 Bulk Product 55a29183 Books $966.04 21 0.8 62 BULK-24b54ec7
8102 Bulk Product dc40c484 Clothing $995.26 295 0.3 63 BULK-73d3a3eb
8103 Bulk Product 9e631e40 Clothing $210.49 317 3.1 78 BULK-5a0c4515
8104 Bulk Product c7590963 Home & Garden $343.76 0 1.9 35 BULK-123f7e43
8105 Bulk Product e8f2d291 Home & Garden $305.21 220 0.9 93 BULK-91f44bf6
8106 Bulk Product 5f28f69a Clothing $968.48 349 0.9 56 BULK-fcf734eb
8107 Bulk Product 8e0d524b Clothing $275.36 452 3.9 72 BULK-cf7c3312
8108 Bulk Product b9f85bb5 Home & Garden $994.06 425 2.6 80 BULK-d792ee42
8109 Bulk Product b8c1c10b Sports & Outdoors $379.99 441 1.9 69 BULK-4978ac8c
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