Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

23 ms

Page Size

25

Current Page

376 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 376 Results Showing 9376 - 9400 of 24441

ID Name Category Price Stock Rating Reviews SKU
9935 Bulk Product ff8f0907 Electronics $399.83 23 1.9 71 BULK-c86268c5
9936 Bulk Product fdfcb9cd Toys & Games $316.70 222 1.1 26 BULK-9f961f74
9937 Bulk Product da9f2e7e Home & Garden $947.29 444 3.5 80 BULK-0bf901ff
9938 Bulk Product 869ff8b8 Sports & Outdoors $908.50 195 4.9 65 BULK-23bfbeac
9939 Bulk Product 99bb2bdd Clothing $445.29 241 0.4 98 BULK-33e7f9ca
9940 Bulk Product a9b48166 Toys & Games $750.10 229 0.0 32 BULK-61910d6c
9941 Bulk Product 86a129aa Home & Garden $820.43 446 4.0 74 BULK-95a3ef94
9942 Bulk Product b6c63672 Sports & Outdoors $994.76 401 4.3 42 BULK-7e2676d3
9943 Bulk Product ea6cabef Home & Garden $659.45 17 1.2 87 BULK-4ab947f8
9944 Bulk Product 6095da45 Clothing $664.67 301 3.2 18 BULK-1de27e29
9945 Bulk Product 3b2808b8 Electronics $358.19 1 1.9 80 BULK-50fc5f1e
9946 Bulk Product 03bf063c Home & Garden $131.36 105 0.5 50 BULK-702bae17
9947 Bulk Product 1eb0336a Home & Garden $204.34 209 1.0 96 BULK-d7e6fa2a
9948 Bulk Product 4ee14545 Clothing $676.56 79 4.5 2 BULK-fb67f39d
9949 Bulk Product b197c155 Electronics $943.86 33 4.4 55 BULK-39d9f6c5
9950 Bulk Product cefc6d98 Sports & Outdoors $683.43 310 2.7 84 BULK-7aa17581
9951 Bulk Product 55d4f81c Electronics $344.20 321 1.1 62 BULK-6c3b6686
9952 Bulk Product 02b4d018 Toys & Games $296.16 181 1.3 47 BULK-28422283
9953 Bulk Product 2aad8fb3 Books $587.27 380 1.4 88 BULK-cd031b38
9954 Bulk Product 329a8a0c Clothing $897.05 62 3.1 59 BULK-717b7a56
9955 Bulk Product 748360c8 Electronics $760.85 180 1.5 18 BULK-e23bbe19
9956 Bulk Product f1592174 Sports & Outdoors $884.85 498 3.5 34 BULK-4ba568c4
9957 Bulk Product 4589777d Home & Garden $384.69 235 3.5 7 BULK-a854b30e
9958 Bulk Product 9a29c466 Sports & Outdoors $366.03 153 3.9 26 BULK-24537ffe
9959 Bulk Product 01a75d1a Toys & Games $452.87 321 0.7 49 BULK-390156ed
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