Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

19 ms

Page Size

25

Current Page

378 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 378 Results Showing 9426 - 9450 of 24441

ID Name Category Price Stock Rating Reviews SKU
9985 Bulk Product dc382fa1 Electronics $293.14 143 1.9 86 BULK-9707f128
9986 Bulk Product 79640e02 Books $885.06 45 1.0 95 BULK-cf9d5c62
9987 Bulk Product 18115f8a Sports & Outdoors $348.43 192 2.4 47 BULK-70c85eac
9988 Bulk Product fc7941b5 Sports & Outdoors $823.78 327 4.6 7 BULK-d02c97bc
9989 Bulk Product 1b10038f Home & Garden $83.04 393 0.7 31 BULK-1b74a33a
9990 Bulk Product 2acee5f6 Clothing $569.08 231 0.7 27 BULK-33c7e0da
9991 Bulk Product e2ea7e3a Clothing $626.70 301 2.4 98 BULK-d7b7f67f
9992 Bulk Product b8009ef2 Electronics $326.29 279 3.8 34 BULK-6fbf8ba3
9993 Bulk Product f1fc2a6b Clothing $63.49 420 4.2 28 BULK-8a9f63e5
9994 Bulk Product aa1bc554 Sports & Outdoors $444.47 384 1.6 35 BULK-0d692e6c
9995 Bulk Product ab255868 Electronics $981.05 132 2.9 10 BULK-be862a59
9996 Bulk Product c15b05c2 Books $79.29 309 0.9 24 BULK-752a1ffb
9997 Bulk Product c2ec3ec3 Home & Garden $502.61 77 2.7 91 BULK-5d9d65b0
9998 Bulk Product 0b9a6868 Books $495.15 263 3.4 11 BULK-3e5da8fb
9999 Bulk Product a61343f7 Books $243.20 345 3.6 83 BULK-4422502a
10000 Bulk Product ae6abbe0 Books $947.36 76 3.7 84 BULK-90f0215c
10001 Bulk Product f99f579b Books $309.82 339 2.2 49 BULK-3815e7fb
10002 Bulk Product 96803d16 Books $634.87 475 1.7 29 BULK-d3e0e37b
10003 Bulk Product f8ba144d Electronics $338.09 56 0.7 66 BULK-c9ed3998
10004 Bulk Product 3337bfcb Toys & Games $737.23 305 1.0 32 BULK-ef0bb3d1
10005 Bulk Product f8714db6 Electronics $491.19 101 2.2 97 BULK-a417df15
10006 Bulk Product 65a573a9 Toys & Games $875.89 128 2.8 56 BULK-7c0dd7cd
10007 Bulk Product 40176488 Clothing $597.36 164 1.2 40 BULK-79ef070b
10008 Bulk Product 1231e689 Electronics $696.42 230 2.8 97 BULK-2d60a73f
10009 Bulk Product aef04d60 Toys & Games $952.73 487 1.6 98 BULK-0dd3d5da
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