Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

9 ms

Page Size

25

Current Page

12 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 12 Results Showing 276 - 300 of 24441

ID Name Category Price Stock Rating Reviews SKU
305 Super Books Product Max Books $396.40 455 4.4 495 SKU-000305
306 Standard Clothing Product Classic Clothing $310.39 297 3.7 427 SKU-000306
307 Economy Books Product Pro Books $650.32 377 3.1 653 SKU-000307
308 Standard Clothing Product Special Edition Clothing $356.63 480 2.8 66 SKU-000308
309 Standard Health & Beauty Product Pro Health & Beauty $197.23 135 3.6 902 SKU-000309
310 Ultra Office Supplies Product Special Edition Office Supplies $992.30 277 3.2 896 SKU-000310
311 Professional Home & Garden Product XL Home & Garden $824.56 61 3.1 198 SKU-000311
312 Standard Health & Beauty Product Special Edition Health & Beauty $576.19 407 3.7 323 SKU-000312
313 Super Automotive Product Classic Automotive $59.16 326 1.5 142 SKU-000313
314 Ultra Office Supplies Product Advanced Office Supplies $118.54 214 4.7 377 SKU-000314
316 Premium Sports & Outdoors Product Lite Sports & Outdoors $428.42 15 3.5 627 SKU-000316
317 Pro Home & Garden Product Mini Home & Garden $921.48 387 4.4 193 SKU-000317
318 Mega Electronics Product Pro Electronics $686.03 173 3.0 774 SKU-000318
319 Standard Toys & Games Product 2024 Toys & Games $979.35 92 1.8 865 SKU-000319
321 Professional Clothing Product Classic Clothing $51.91 449 3.6 533 SKU-000321
322 Deluxe Books Product Plus Books $162.17 278 3.2 231 SKU-000322
323 Professional Food & Grocery Product Special Edition Food & Grocery $867.27 283 1.5 94 SKU-000323
324 Economy Books Product Special Edition Books $804.96 438 2.2 302 SKU-000324
325 Pro Toys & Games Product Mini Toys & Games $960.75 127 2.1 705 SKU-000325
326 Professional Home & Garden Product Mini Home & Garden $12.50 64 1.8 889 SKU-000326
327 Ultra Sports & Outdoors Product 2024 Sports & Outdoors $785.18 244 3.1 911 SKU-000327
328 Economy Toys & Games Product Plus Toys & Games $400.39 132 3.9 482 SKU-000328
329 Professional Home & Garden Product Mini Home & Garden $952.44 144 1.2 663 SKU-000329
331 Pro Home & Garden Product Max Home & Garden $840.52 467 1.3 769 SKU-000331
332 Super Health & Beauty Product Advanced Health & Beauty $773.88 188 3.1 558 SKU-000332
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