Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

11 ms

Page Size

25

Current Page

512 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 512 Results Showing 12776 - 12800 of 24441

ID Name Category Price Stock Rating Reviews SKU
13335 Bulk Product 6b4a53f4 Electronics $36.09 328 3.2 59 BULK-72c459bf
13336 Bulk Product b14ce9cf Toys & Games $950.63 296 2.1 62 BULK-698344cc
13337 Bulk Product a3cc2160 Books $525.36 206 1.9 18 BULK-30f86024
13338 Bulk Product f7731106 Clothing $403.46 82 0.5 23 BULK-c80a6622
13339 Bulk Product ce9583a8 Sports & Outdoors $621.32 156 0.8 69 BULK-ffd01870
13340 Bulk Product 5c30cba5 Toys & Games $495.05 445 1.7 58 BULK-11f9b906
13341 Bulk Product f8af2c44 Home & Garden $451.72 111 3.0 77 BULK-f5eeb37c
13342 Bulk Product 075e97a9 Electronics $722.25 421 2.2 5 BULK-be873d7b
13343 Bulk Product 0dae12a5 Home & Garden $371.91 416 2.2 84 BULK-d4671e39
13344 Bulk Product 4160cb81 Home & Garden $604.97 443 2.1 92 BULK-2e2b27aa
13345 Bulk Product e43c8b12 Sports & Outdoors $999.11 235 2.7 2 BULK-598158c1
13346 Bulk Product ab7d1e45 Electronics $969.04 125 3.7 48 BULK-2c9532d5
13347 Bulk Product 8ce31cd9 Toys & Games $836.29 214 1.3 84 BULK-3c7cd49a
13348 Bulk Product 87d1fdb2 Toys & Games $840.00 412 1.5 19 BULK-82e7e3dc
13349 Bulk Product 2264e8de Electronics $209.42 313 1.2 31 BULK-2294ee26
13350 Bulk Product fb4586d5 Sports & Outdoors $867.16 188 1.3 39 BULK-b0d47122
13351 Bulk Product 4bb2efe3 Books $679.65 396 2.7 69 BULK-612080db
13352 Bulk Product d62d7f7b Home & Garden $688.75 379 1.4 99 BULK-f889cc0e
13353 Bulk Product 9a887faf Electronics $477.80 377 4.9 10 BULK-7377eee9
13354 Bulk Product 87df8d7f Books $250.43 189 1.4 10 BULK-64485912
13355 Bulk Product 1931bc38 Home & Garden $152.78 264 2.6 91 BULK-78005ac7
13356 Bulk Product 02cfad00 Toys & Games $886.85 449 3.9 1 BULK-b0257166
13357 Bulk Product 5e6cbc60 Home & Garden $983.04 277 1.7 90 BULK-8b1afd01
13358 Bulk Product ac7b7dc9 Sports & Outdoors $86.10 365 2.3 0 BULK-a60da63b
13359 Bulk Product 8f4214a8 Home & Garden $457.34 226 2.6 15 BULK-960f2082
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