Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

12 ms

Page Size

25

Current Page

386 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 386 Results Showing 9626 - 9650 of 24441

ID Name Category Price Stock Rating Reviews SKU
10185 Bulk Product 8cfdfe69 Electronics $195.67 32 1.2 14 BULK-425cf7c5
10186 Bulk Product c3ee3e33 Clothing $157.34 42 2.4 3 BULK-a7626e0e
10187 Bulk Product aed76104 Sports & Outdoors $762.00 478 3.2 26 BULK-ced48ece
10188 Bulk Product d89e5a00 Electronics $400.29 136 2.0 92 BULK-2ff5574c
10189 Bulk Product 4ecce459 Home & Garden $426.63 12 3.0 86 BULK-1ce45ce1
10190 Bulk Product 8f874e24 Clothing $365.70 135 1.3 4 BULK-5cb91bfa
10191 Bulk Product 9b73ebdf Home & Garden $222.89 446 1.7 55 BULK-a58473d2
10192 Bulk Product b250d081 Books $554.49 262 1.1 0 BULK-d118ace0
10193 Bulk Product 4ce51a49 Sports & Outdoors $197.56 439 0.9 46 BULK-6854a855
10194 Bulk Product 4600f6fd Toys & Games $91.98 450 4.8 23 BULK-6a7f3c59
10195 Bulk Product 13e654a8 Toys & Games $547.67 338 0.1 31 BULK-da12aeca
10196 Bulk Product ac50b012 Home & Garden $313.10 301 2.6 7 BULK-37d410be
10197 Bulk Product d08cb9cc Books $151.33 389 1.2 85 BULK-df17dd71
10198 Bulk Product b05ad77d Sports & Outdoors $219.92 312 0.4 57 BULK-15bc0dd3
10199 Bulk Product 41ca7103 Clothing $642.87 10 3.5 27 BULK-a5b0910d
10200 Bulk Product 12d6c208 Books $221.20 251 2.7 48 BULK-db493b1e
10201 Bulk Product e09b1899 Sports & Outdoors $454.32 482 3.7 34 BULK-d759def0
10202 Bulk Product fc65780c Sports & Outdoors $806.39 318 0.8 39 BULK-2b619cce
10203 Bulk Product 418a8522 Home & Garden $187.05 296 3.5 99 BULK-0a23d8e2
10204 Bulk Product bd9ed0df Sports & Outdoors $418.11 446 2.7 38 BULK-e4bd0b67
10205 Bulk Product 894e6a9e Electronics $75.56 395 3.7 99 BULK-2bdd125b
10206 Bulk Product d968386f Sports & Outdoors $451.08 132 3.6 40 BULK-5e68a3e0
10207 Bulk Product 39050318 Books $872.11 275 4.6 60 BULK-6ef4cf3b
10208 Bulk Product 36999a66 Home & Garden $128.03 425 2.3 29 BULK-1b36e413
10209 Bulk Product 9fa97f9b Sports & Outdoors $720.18 271 0.8 63 BULK-a9ffe1a7
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