Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

17 ms

Page Size

25

Current Page

223 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 223 Results Showing 5551 - 5575 of 24441

ID Name Category Price Stock Rating Reviews SKU
6110 Bulk Product 02fc1eee Books $293.05 157 4.8 46 BULK-e2816304
6111 Bulk Product d1598e24 Electronics $246.01 408 4.9 84 BULK-cfa9b5bd
6112 Bulk Product 1d184f5f Electronics $857.02 4 4.0 71 BULK-4b366ca6
6113 Bulk Product 6c1b8388 Electronics $779.60 235 4.8 85 BULK-31b1c2b4
6114 Bulk Product 9deef4ff Clothing $634.95 99 1.1 74 BULK-f1bcccf5
6115 Bulk Product 52b4307e Books $678.67 421 5.0 5 BULK-82a634ec
6116 Bulk Product ca3cbe41 Books $405.35 482 4.6 30 BULK-e6793330
6117 Bulk Product e372167b Toys & Games $373.29 198 3.8 93 BULK-25cc8fb4
6118 Bulk Product fcb2f0da Sports & Outdoors $515.69 442 3.7 87 BULK-74dea9f0
6119 Bulk Product 35d71e95 Sports & Outdoors $223.49 365 0.4 15 BULK-0483d64e
6120 Bulk Product c7889d9b Electronics $631.58 75 3.3 42 BULK-bf79f967
6121 Bulk Product 46b84501 Sports & Outdoors $43.41 9 3.8 88 BULK-6bc3d040
6122 Bulk Product 63d4f23a Home & Garden $703.06 272 0.8 37 BULK-8e5cd9d8
6123 Bulk Product 51de6f5c Toys & Games $240.06 306 2.5 16 BULK-232878bf
6124 Bulk Product 99471e66 Home & Garden $652.86 174 1.3 37 BULK-1732fc43
6125 Bulk Product 334cee24 Home & Garden $669.11 174 0.3 79 BULK-a90c3634
6126 Bulk Product 2d8174e2 Toys & Games $899.50 101 2.8 64 BULK-399e8e50
6127 Bulk Product 5d91c536 Sports & Outdoors $198.73 409 3.1 75 BULK-c38cfaf0
6128 Bulk Product 93f82318 Sports & Outdoors $349.66 222 1.3 43 BULK-ca22a321
6129 Bulk Product d39e688c Clothing $149.10 306 1.9 28 BULK-4afeebf1
6130 Bulk Product 96fc6705 Electronics $126.18 162 2.0 41 BULK-e1eef375
6131 Bulk Product 1eea73db Toys & Games $942.30 52 4.2 26 BULK-035ed5ae
6132 Bulk Product 7259e3dc Books $102.35 222 1.3 65 BULK-5ffd0f3e
6133 Bulk Product fe21e400 Sports & Outdoors $158.86 143 4.2 10 BULK-f476cc93
6134 Bulk Product 65f52a94 Clothing $544.00 211 2.1 56 BULK-af75b452
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