Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

17 ms

Page Size

25

Current Page

25 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 25 Results Showing 601 - 625 of 24441

ID Name Category Price Stock Rating Reviews SKU
674 Pro Automotive Product Lite Automotive $740.79 456 3.3 438 SKU-000674
675 Deluxe Office Supplies Product Classic Office Supplies $628.28 66 4.6 928 SKU-000675
676 Elite Toys & Games Product Classic Toys & Games $746.25 116 3.8 446 SKU-000676
677 Premium Home & Garden Product Advanced Home & Garden $38.46 434 3.1 867 SKU-000677
678 Professional Office Supplies Product Max Office Supplies $691.40 445 2.9 347 SKU-000678
679 Ultra Automotive Product XL Automotive $69.14 170 1.3 955 SKU-000679
680 Elite Sports & Outdoors Product Lite Sports & Outdoors $321.22 268 1.5 765 SKU-000680
682 Standard Food & Grocery Product Max Food & Grocery $951.89 160 1.2 942 SKU-000682
684 Premium Books Product Lite Books $753.18 472 3.8 736 SKU-000684
685 Elite Office Supplies Product Advanced Office Supplies $842.57 21 1.1 432 SKU-000685
686 Standard Home & Garden Product Lite Home & Garden $975.48 196 3.3 793 SKU-000686
687 Super Office Supplies Product Mini Office Supplies $455.36 244 4.1 322 SKU-000687
688 Professional Home & Garden Product Lite Home & Garden $214.61 341 3.1 380 SKU-000688
689 Elite Office Supplies Product Lite Office Supplies $735.13 78 2.8 894 SKU-000689
690 Elite Sports & Outdoors Product Special Edition Sports & Outdoors $510.16 369 3.0 113 SKU-000690
691 Standard Automotive Product Max Automotive $920.42 370 1.6 877 SKU-000691
692 Professional Electronics Product Classic Electronics $507.53 317 4.9 858 SKU-000692
693 Premium Health & Beauty Product Pro Health & Beauty $893.92 152 3.2 89 SKU-000693
694 Deluxe Health & Beauty Product Advanced Health & Beauty $114.78 410 2.9 630 SKU-000694
695 Standard Books Product Plus Books $897.17 409 1.1 167 SKU-000695
696 Ultra Electronics Product Mini Electronics $542.62 80 1.2 331 SKU-000696
697 Standard Home & Garden Product Lite Home & Garden $464.53 43 2.7 566 SKU-000697
698 Economy Books Product Max Books $742.20 349 3.5 293 SKU-000698
699 Super Books Product Special Edition Books $217.40 135 3.4 465 SKU-000699
700 Mega Clothing Product Classic Clothing $232.79 231 3.7 782 SKU-000700
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