Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

15 ms

Page Size

25

Current Page

541 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 541 Results Showing 13501 - 13525 of 24441

ID Name Category Price Stock Rating Reviews SKU
14060 Bulk Product 1cccac6e Books $233.24 370 3.6 12 BULK-3a3768ca
14061 Bulk Product 625903f5 Electronics $83.02 154 4.1 66 BULK-e13361b5
14062 Bulk Product 739cdebd Clothing $415.39 332 1.6 28 BULK-ae07efc8
14063 Bulk Product 7f793f9f Clothing $822.90 164 3.2 23 BULK-793a53d0
14064 Bulk Product 7e8cf183 Books $441.97 154 0.7 0 BULK-140096b0
14065 Bulk Product 9859be66 Sports & Outdoors $908.14 252 2.9 72 BULK-3c9b0bb5
14066 Bulk Product 62860022 Electronics $650.39 278 4.5 41 BULK-95c4fe1e
14067 Bulk Product 84ff406d Toys & Games $747.44 200 4.4 9 BULK-27f298ce
14068 Bulk Product baeb6bbd Books $803.76 145 2.0 90 BULK-e075bd25
14069 Bulk Product bbc91ed9 Clothing $664.17 154 2.4 97 BULK-feda1227
14070 Bulk Product 3503f372 Home & Garden $429.05 65 3.9 60 BULK-d0fcb683
14071 Bulk Product 685b4c54 Books $93.25 273 3.5 17 BULK-259687a5
14072 Bulk Product 8a89a379 Books $549.51 31 4.9 21 BULK-f7d34e72
14073 Bulk Product 2fce9231 Books $843.96 343 0.7 76 BULK-c8b8c963
14074 Bulk Product 83dbc39b Electronics $122.87 320 1.9 90 BULK-f459af1c
14075 Bulk Product 63fc384f Electronics $349.39 471 2.6 69 BULK-62903db4
14076 Bulk Product 69366e3a Sports & Outdoors $602.99 142 0.1 84 BULK-7471a250
14077 Bulk Product d48580e7 Books $623.56 145 3.3 20 BULK-22802b42
14078 Bulk Product eaee27ad Books $77.66 491 2.6 93 BULK-ebf90555
14079 Bulk Product c07cd6dc Books $173.30 190 1.5 30 BULK-7828c593
14080 Bulk Product a387cb85 Sports & Outdoors $198.64 249 1.1 32 BULK-996b6127
14081 Bulk Product 4ce499f9 Electronics $746.32 264 3.8 61 BULK-7f7606c2
14082 Bulk Product fd28eefd Electronics $719.80 132 1.9 20 BULK-a70e73c3
14083 Bulk Product e48c5831 Home & Garden $978.36 93 1.1 62 BULK-58d53a68
14084 Bulk Product f4d3db39 Clothing $55.79 38 2.5 23 BULK-c448b127
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