Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

43 ms

Page Size

25

Current Page

507 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 507 Results Showing 12651 - 12675 of 24441

ID Name Category Price Stock Rating Reviews SKU
13210 Bulk Product e8cec06e Electronics $141.10 2 4.4 75 BULK-2f8d4013
13211 Bulk Product 7e67694d Clothing $125.18 360 2.8 18 BULK-0fff3438
13212 Bulk Product dbdd78d1 Toys & Games $637.25 415 5.0 61 BULK-66f11ce1
13213 Bulk Product 1a8e26ef Toys & Games $539.45 198 3.1 48 BULK-b3a56683
13214 Bulk Product 5d5ed162 Sports & Outdoors $35.62 132 0.9 47 BULK-6526602a
13215 Bulk Product 74d4908c Sports & Outdoors $30.97 115 4.2 88 BULK-0d1e0a58
13216 Bulk Product 8eb4d99a Books $479.35 92 0.2 9 BULK-383dfadc
13217 Bulk Product 80e791f5 Sports & Outdoors $34.02 258 3.0 19 BULK-b6e08e4d
13218 Bulk Product 6e2a51f6 Clothing $94.63 98 2.3 64 BULK-a7caf0e7
13219 Bulk Product ceed17a5 Books $64.09 98 0.4 51 BULK-197e7500
13220 Bulk Product 5e104f69 Sports & Outdoors $561.16 210 3.3 51 BULK-f076910f
13221 Bulk Product c0533a5f Electronics $664.19 402 2.5 19 BULK-226fe1f0
13222 Bulk Product d518a599 Toys & Games $142.08 141 2.4 81 BULK-938420a5
13223 Bulk Product 80d6bfe8 Books $846.50 136 4.9 16 BULK-1c4144fa
13224 Bulk Product c43ea6d8 Home & Garden $933.39 401 0.3 14 BULK-7fbaaf49
13225 Bulk Product 7fbdf94f Toys & Games $253.11 477 1.1 26 BULK-4365f6de
13226 Bulk Product a76708cd Books $86.50 487 3.3 98 BULK-5cae23b9
13227 Bulk Product 70553050 Toys & Games $146.56 53 2.3 96 BULK-e639822f
13228 Bulk Product 7b64897b Electronics $220.28 480 1.7 14 BULK-877a50ca
13229 Bulk Product 2208a63f Sports & Outdoors $955.54 15 0.6 40 BULK-4b8a93d9
13230 Bulk Product 2a284e30 Books $549.43 8 2.9 58 BULK-ec68d467
13231 Bulk Product bdd2dacf Books $337.16 198 1.5 46 BULK-2ce4f7ee
13232 Bulk Product 562a3151 Home & Garden $521.22 240 0.4 73 BULK-5513b4e3
13233 Bulk Product b1c8dc81 Clothing $756.77 89 0.9 67 BULK-d2271fea
13234 Bulk Product 9eacdfb9 Sports & Outdoors $530.62 39 2.4 92 BULK-89d272a1
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