Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

25 ms

Page Size

25

Current Page

399 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 399 Results Showing 9951 - 9975 of 24441

ID Name Category Price Stock Rating Reviews SKU
10510 Bulk Product 5c5a487a Clothing $827.27 231 1.2 92 BULK-27274de4
10511 Bulk Product d893e944 Home & Garden $263.79 16 0.1 84 BULK-3de7dc39
10512 Bulk Product 18e796dc Clothing $891.69 155 1.7 91 BULK-968d672e
10513 Bulk Product 0dbf98c2 Sports & Outdoors $630.85 497 2.2 86 BULK-009b5555
10514 Bulk Product 1fedde86 Sports & Outdoors $664.08 181 1.6 53 BULK-2d12de69
10515 Bulk Product 59498582 Books $136.87 181 2.3 80 BULK-43c36bfc
10516 Bulk Product 3514e04b Sports & Outdoors $443.57 237 4.2 27 BULK-61439f87
10517 Bulk Product c1d28a1a Toys & Games $460.13 51 2.4 71 BULK-b90a7ed3
10518 Bulk Product 997d3383 Electronics $809.93 297 4.0 18 BULK-73ed8f20
10519 Bulk Product c41d2e75 Clothing $751.37 127 3.0 33 BULK-86dd7209
10520 Bulk Product d9d59f30 Books $381.01 160 0.6 63 BULK-466cc520
10521 Bulk Product 70d0085e Toys & Games $887.96 15 0.4 48 BULK-6856c290
10522 Bulk Product a5c71f58 Toys & Games $193.89 226 3.4 48 BULK-48e02fe0
10523 Bulk Product faa8ee94 Toys & Games $209.48 413 3.0 93 BULK-9593775f
10524 Bulk Product 9131f6d2 Toys & Games $578.10 88 4.1 59 BULK-5be07fe4
10525 Bulk Product 25f58f14 Electronics $540.16 106 1.8 79 BULK-88496ae0
10526 Bulk Product 0f9300c8 Toys & Games $447.21 133 3.7 95 BULK-01817abf
10527 Bulk Product 4c03d18b Books $734.98 111 2.2 48 BULK-c18f9acd
10528 Bulk Product 94c32420 Sports & Outdoors $347.04 46 4.1 63 BULK-daee1820
10529 Bulk Product d185fa24 Sports & Outdoors $457.67 292 1.7 76 BULK-9a669126
10530 Bulk Product 59539d4d Books $207.91 204 3.8 41 BULK-213369b2
10531 Bulk Product 8ecbad6d Electronics $48.05 55 3.2 28 BULK-d9ab1ab4
10532 Bulk Product b54f9c69 Clothing $494.22 473 1.9 54 BULK-56e8da9f
10533 Bulk Product 2b8f9818 Clothing $381.27 3 1.4 44 BULK-ebd776e2
10534 Bulk Product 838e2095 Clothing $358.76 160 2.6 32 BULK-c773f183
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