Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

17 ms

Page Size

25

Current Page

520 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 520 Results Showing 12976 - 13000 of 24441

ID Name Category Price Stock Rating Reviews SKU
13535 Bulk Product 983d7d88 Electronics $498.75 436 4.2 58 BULK-b6296baf
13536 Bulk Product 6b95ec29 Books $852.25 397 3.8 73 BULK-d99e26cb
13537 Bulk Product 019def52 Electronics $654.36 68 3.1 83 BULK-ae2985e9
13538 Bulk Product db6b49ae Sports & Outdoors $728.00 404 1.9 91 BULK-00ea7b31
13539 Bulk Product 66348479 Home & Garden $360.97 103 1.8 63 BULK-93619ba3
13540 Bulk Product 84aa6bbf Books $476.61 132 0.2 87 BULK-019c4a0d
13541 Bulk Product 3ca178dd Electronics $837.09 259 1.0 6 BULK-c401b4c1
13542 Bulk Product 2541b6df Books $410.05 40 0.1 96 BULK-1c1d7a3e
13543 Bulk Product 006e4402 Books $685.85 15 2.2 58 BULK-82c0a81e
13544 Bulk Product 089f6c87 Sports & Outdoors $163.96 442 1.2 23 BULK-6eccaf8b
13545 Bulk Product e2adf647 Sports & Outdoors $933.32 205 1.2 95 BULK-78d1fa1f
13546 Bulk Product 7c77fe46 Sports & Outdoors $538.15 384 2.3 31 BULK-7ac9149a
13547 Bulk Product 588fab8c Home & Garden $473.87 405 5.0 95 BULK-47e7e09f
13548 Bulk Product 27861d68 Books $883.40 112 4.5 62 BULK-973608b1
13549 Bulk Product bc7f9705 Clothing $988.90 210 0.3 99 BULK-660ef5f1
13550 Bulk Product 210444ce Toys & Games $590.21 481 2.0 85 BULK-28fc57e7
13551 Bulk Product abad6dbf Clothing $201.93 416 4.1 68 BULK-7cc26cb3
13552 Bulk Product 684a595c Toys & Games $154.72 399 3.3 41 BULK-1a7eabb2
13553 Bulk Product 87c8406c Sports & Outdoors $472.95 204 4.5 57 BULK-3f1bff88
13554 Bulk Product 49e314b6 Books $34.62 5 1.9 64 BULK-cd183199
13555 Bulk Product 19078c65 Books $625.51 245 4.9 91 BULK-e7922724
13556 Bulk Product 9360805b Books $506.29 432 4.4 4 BULK-fd85e6af
13557 Bulk Product 864d47ce Clothing $642.54 210 1.1 56 BULK-757f4aa0
13558 Bulk Product 83aebc6f Sports & Outdoors $563.34 122 3.8 22 BULK-faa10c1c
13559 Bulk Product 3caffa12 Toys & Games $376.70 485 4.0 87 BULK-4fd44656
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