Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

24 ms

Page Size

25

Current Page

30 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 30 Results Showing 726 - 750 of 24441

ID Name Category Price Stock Rating Reviews SKU
812 Mega Office Supplies Product Max Office Supplies $469.87 121 2.7 158 SKU-000812
813 Pro Clothing Product Classic Clothing $870.46 164 4.6 13 SKU-000813
815 Professional Automotive Product Mini Automotive $725.69 384 3.9 870 SKU-000815
816 Mega Office Supplies Product Special Edition Office Supplies $571.90 414 2.9 779 SKU-000816
817 Economy Health & Beauty Product 2024 Health & Beauty $319.38 45 2.0 989 SKU-000817
818 Pro Electronics Product Pro Electronics $83.81 189 1.2 780 SKU-000818
819 Professional Automotive Product Pro Automotive $493.98 29 4.2 436 SKU-000819
820 Elite Books Product Mini Books $919.07 483 3.4 148 SKU-000820
822 Mega Automotive Product Max Automotive $88.75 104 3.1 563 SKU-000822
823 Standard Electronics Product 2024 Electronics $44.77 426 3.7 90 SKU-000823
824 Deluxe Electronics Product Advanced Electronics $198.15 125 3.4 703 SKU-000824
825 Pro Books Product Plus Books $726.05 198 3.8 894 SKU-000825
826 Premium Office Supplies Product XL Office Supplies $510.94 54 1.8 690 SKU-000826
827 Pro Electronics Product XL Electronics $552.60 427 2.4 401 SKU-000827
828 Ultra Electronics Product 2024 Electronics $481.04 97 2.5 180 SKU-000828
829 Pro Food & Grocery Product Pro Food & Grocery $833.59 100 3.6 61 SKU-000829
830 Elite Health & Beauty Product Classic Health & Beauty $192.43 233 2.7 595 SKU-000830
831 Elite Home & Garden Product Lite Home & Garden $408.75 18 1.9 286 SKU-000831
832 Mega Health & Beauty Product Max Health & Beauty $618.57 409 3.5 567 SKU-000832
833 Mega Electronics Product 2024 Electronics $855.19 388 4.0 440 SKU-000833
834 Professional Clothing Product Special Edition Clothing $172.10 253 3.4 683 SKU-000834
835 Economy Toys & Games Product Mini Toys & Games $613.68 309 3.8 803 SKU-000835
836 Elite Books Product Pro Books $675.29 487 2.8 787 SKU-000836
837 Elite Toys & Games Product Pro Toys & Games $533.92 130 2.6 227 SKU-000837
838 Premium Clothing Product Pro Clothing $986.81 263 5.0 820 SKU-000838
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