Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

7 ms

Page Size

25

Current Page

13 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 13 Results Showing 301 - 325 of 24441

ID Name Category Price Stock Rating Reviews SKU
333 Pro Clothing Product Pro Clothing $172.82 211 1.5 815 SKU-000333
334 Elite Health & Beauty Product 2024 Health & Beauty $159.69 92 3.7 535 SKU-000334
335 Premium Health & Beauty Product Classic Health & Beauty $234.68 382 3.2 833 SKU-000335
336 Premium Automotive Product Mini Automotive $474.58 34 2.1 462 SKU-000336
337 Standard Automotive Product Classic Automotive $467.93 154 3.6 458 SKU-000337
338 Mega Clothing Product Pro Clothing $616.94 77 3.4 542 SKU-000338
339 Mega Sports & Outdoors Product Classic Sports & Outdoors $357.13 189 2.0 966 SKU-000339
340 Premium Books Product Plus Books $476.70 12 2.6 679 SKU-000340
341 Deluxe Books Product Pro Books $139.90 417 3.5 241 SKU-000341
342 Pro Books Product Advanced Books $834.31 322 1.0 691 SKU-000342
343 Pro Health & Beauty Product Lite Health & Beauty $187.54 370 3.2 141 SKU-000343
344 Mega Food & Grocery Product Classic Food & Grocery $530.05 1 2.7 854 SKU-000344
345 Super Toys & Games Product Max Toys & Games $21.24 359 3.3 940 SKU-000345
346 Standard Electronics Product 2024 Electronics $439.97 394 3.6 153 SKU-000346
347 Deluxe Sports & Outdoors Product Pro Sports & Outdoors $977.31 99 4.3 85 SKU-000347
348 Standard Toys & Games Product Lite Toys & Games $413.43 68 3.6 228 SKU-000348
350 Standard Home & Garden Product Max Home & Garden $820.08 320 3.0 914 SKU-000350
351 Deluxe Clothing Product Special Edition Clothing $150.40 140 2.7 984 SKU-000351
352 Deluxe Food & Grocery Product Special Edition Food & Grocery $574.04 29 3.1 412 SKU-000352
353 Mega Electronics Product Mini Electronics $947.10 176 1.1 552 SKU-000353
354 Pro Clothing Product Pro Clothing $520.50 410 4.4 305 SKU-000354
355 Premium Food & Grocery Product Pro Food & Grocery $208.51 369 2.0 655 SKU-000355
356 Economy Office Supplies Product Advanced Office Supplies $620.94 354 1.3 815 SKU-000356
357 Pro Home & Garden Product Special Edition Home & Garden $467.20 90 1.1 140 SKU-000357
358 Mega Electronics Product Special Edition Electronics $414.35 499 2.5 42 SKU-000358
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