Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

11 ms

Page Size

25

Current Page

23 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 23 Results Showing 551 - 575 of 24441

ID Name Category Price Stock Rating Reviews SKU
614 Mega Home & Garden Product Max Home & Garden $489.81 215 3.5 520 SKU-000614
615 Standard Home & Garden Product Plus Home & Garden $82.54 220 2.6 141 SKU-000615
616 Standard Sports & Outdoors Product Lite Sports & Outdoors $653.27 70 2.3 585 SKU-000616
618 Professional Electronics Product Pro Electronics $930.24 117 2.3 439 SKU-000618
619 Economy Health & Beauty Product Classic Health & Beauty $415.28 325 2.6 445 SKU-000619
620 Elite Office Supplies Product Classic Office Supplies $278.16 224 3.0 173 SKU-000620
621 Pro Sports & Outdoors Product Classic Sports & Outdoors $238.32 262 1.8 601 SKU-000621
623 Super Clothing Product Mini Clothing $974.10 73 2.8 252 SKU-000623
624 Economy Automotive Product 2024 Automotive $576.20 111 3.9 638 SKU-000624
625 Standard Health & Beauty Product Lite Health & Beauty $643.12 442 4.3 672 SKU-000625
627 Deluxe Office Supplies Product XL Office Supplies $911.48 417 1.9 122 SKU-000627
628 Super Sports & Outdoors Product Lite Sports & Outdoors $747.01 36 4.7 906 SKU-000628
629 Deluxe Office Supplies Product Lite Office Supplies $860.22 17 4.7 374 SKU-000629
630 Economy Home & Garden Product Mini Home & Garden $843.36 99 4.3 266 SKU-000630
631 Deluxe Books Product Lite Books $695.48 275 1.2 553 SKU-000631
633 Pro Health & Beauty Product XL Health & Beauty $313.50 408 2.5 279 SKU-000633
634 Mega Toys & Games Product Lite Toys & Games $870.29 186 2.1 403 SKU-000634
635 Premium Food & Grocery Product Mini Food & Grocery $827.74 270 3.0 293 SKU-000635
636 Super Electronics Product Advanced Electronics $866.19 173 2.7 788 SKU-000636
637 Pro Food & Grocery Product Advanced Food & Grocery $700.93 103 3.7 388 SKU-000637
638 Mega Office Supplies Product Pro Office Supplies $697.54 221 4.0 334 SKU-000638
639 Ultra Books Product Plus Books $608.58 490 1.9 886 SKU-000639
640 Standard Office Supplies Product Advanced Office Supplies $151.24 95 3.0 635 SKU-000640
641 Deluxe Books Product Max Books $828.95 172 1.5 832 SKU-000641
643 Super Automotive Product Max Automotive $330.87 459 3.4 876 SKU-000643
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