Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

49 ms

Page Size

25

Current Page

9 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 9 Results Showing 201 - 225 of 24441

ID Name Category Price Stock Rating Reviews SKU
222 Premium Clothing Product Advanced Clothing $201.72 269 3.3 247 SKU-000222
223 Elite Books Product Advanced Books $213.18 479 2.0 974 SKU-000223
224 Standard Books Product Advanced Books $298.29 107 3.1 863 SKU-000224
225 Ultra Automotive Product Advanced Automotive $18.81 103 4.8 115 SKU-000225
226 Elite Automotive Product Max Automotive $240.21 107 1.2 699 SKU-000226
227 Economy Electronics Product Lite Electronics $830.47 159 3.4 621 SKU-000227
228 Economy Clothing Product Plus Clothing $64.43 73 1.8 269 SKU-000228
229 Professional Health & Beauty Product Lite Health & Beauty $136.72 450 4.6 793 SKU-000229
230 Mega Clothing Product XL Clothing $875.43 208 3.2 986 SKU-000230
231 Mega Toys & Games Product 2024 Toys & Games $835.08 68 4.6 252 SKU-000231
232 Pro Clothing Product Max Clothing $547.14 144 2.5 3 SKU-000232
233 Mega Office Supplies Product XL Office Supplies $641.24 402 3.7 550 SKU-000233
234 Pro Books Product Max Books $284.07 169 3.6 135 SKU-000234
235 Ultra Food & Grocery Product Special Edition Food & Grocery $35.36 399 3.3 117 SKU-000235
236 Elite Home & Garden Product Advanced Home & Garden $416.19 445 3.0 737 SKU-000236
237 Premium Sports & Outdoors Product Max Sports & Outdoors $629.83 343 1.7 256 SKU-000237
238 Standard Toys & Games Product 2024 Toys & Games $570.46 196 4.6 860 SKU-000238
240 Standard Clothing Product 2024 Clothing $519.23 6 4.8 132 SKU-000240
242 Deluxe Automotive Product Max Automotive $719.14 253 3.2 846 SKU-000242
243 Elite Sports & Outdoors Product 2024 Sports & Outdoors $370.82 20 3.9 33 SKU-000243
245 Deluxe Books Product Advanced Books $123.39 36 2.1 990 SKU-000245
246 Super Electronics Product 2024 Electronics $368.00 483 2.0 168 SKU-000246
247 Mega Food & Grocery Product Advanced Food & Grocery $533.87 230 2.5 923 SKU-000247
248 Ultra Office Supplies Product Plus Office Supplies $447.82 373 1.5 280 SKU-000248
249 Elite Home & Garden Product Special Edition Home & Garden $860.38 264 1.7 167 SKU-000249
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