Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

16 ms

Page Size

25

Current Page

24 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 24 Results Showing 576 - 600 of 24441

ID Name Category Price Stock Rating Reviews SKU
644 Mega Automotive Product Max Automotive $119.42 443 3.5 691 SKU-000644
645 Super Clothing Product Lite Clothing $143.45 266 3.4 534 SKU-000645
646 Super Sports & Outdoors Product Classic Sports & Outdoors $655.97 330 2.9 556 SKU-000646
647 Deluxe Toys & Games Product Plus Toys & Games $688.70 108 4.7 252 SKU-000647
648 Professional Health & Beauty Product Pro Health & Beauty $830.91 493 1.9 211 SKU-000648
649 Economy Sports & Outdoors Product Lite Sports & Outdoors $301.96 118 4.3 324 SKU-000649
650 Professional Sports & Outdoors Product Plus Sports & Outdoors $501.88 10 4.6 657 SKU-000650
651 Economy Books Product XL Books $329.29 398 1.2 47 SKU-000651
652 Professional Home & Garden Product Lite Home & Garden $873.88 289 1.5 808 SKU-000652
653 Premium Home & Garden Product Pro Home & Garden $609.93 184 2.0 388 SKU-000653
654 Ultra Automotive Product Pro Automotive $526.63 169 2.4 948 SKU-000654
655 Professional Sports & Outdoors Product Classic Sports & Outdoors $148.19 485 1.5 311 SKU-000655
656 Economy Office Supplies Product Mini Office Supplies $491.35 155 1.6 194 SKU-000656
657 Mega Food & Grocery Product Plus Food & Grocery $583.72 382 1.9 602 SKU-000657
659 Pro Clothing Product Classic Clothing $660.34 442 1.0 524 SKU-000659
660 Professional Toys & Games Product Pro Toys & Games $597.96 477 2.5 879 SKU-000660
661 Pro Home & Garden Product Classic Home & Garden $200.24 266 1.4 471 SKU-000661
663 Deluxe Office Supplies Product Pro Office Supplies $584.27 355 1.8 446 SKU-000663
664 Deluxe Electronics Product Pro Electronics $482.20 101 4.8 180 SKU-000664
665 Professional Clothing Product Classic Clothing $440.60 284 3.0 379 SKU-000665
666 Ultra Health & Beauty Product Mini Health & Beauty $730.59 287 3.8 39 SKU-000666
667 Ultra Clothing Product Classic Clothing $310.05 383 3.5 484 SKU-000667
668 Mega Clothing Product 2024 Clothing $738.66 74 3.8 105 SKU-000668
669 Mega Books Product Special Edition Books $440.59 381 3.1 760 SKU-000669
670 Mega Office Supplies Product XL Office Supplies $617.35 278 1.2 580 SKU-000670
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