Efficient Pagination with Skip/Take

Efficient pagination using OFFSET-FETCH on SQL Server.

Execution Time

19 ms

Page Size

25

Current Page

665 / 978

Total Records

24441

Items per page: 25 50 100 200

Page 665 Results Showing 16601 - 16625 of 24441

ID Name Category Price Stock Rating Reviews SKU
17160 Bulk Product f7b46ec3 Clothing $984.32 194 1.8 55 BULK-cdbfa23b
17161 Bulk Product 021d0da0 Toys & Games $895.21 338 3.8 61 BULK-48803364
17162 Bulk Product 4d24a217 Home & Garden $140.58 282 0.7 79 BULK-1e33bdac
17163 Bulk Product d35d2151 Electronics $752.18 247 2.7 84 BULK-b4fc50c1
17164 Bulk Product 0a8c072d Sports & Outdoors $93.31 39 0.6 35 BULK-68b5897d
17165 Bulk Product 525b0565 Electronics $207.78 11 0.6 21 BULK-4d3446cb
17166 Bulk Product c4ceacf3 Electronics $793.42 419 1.2 18 BULK-71e1f7e6
17167 Bulk Product 53505066 Books $567.75 336 3.8 76 BULK-a5748320
17168 Bulk Product 16d557dc Clothing $952.15 223 0.3 65 BULK-ff39e4aa
17169 Bulk Product 988cd4ad Toys & Games $707.25 220 4.7 35 BULK-9aad72e8
17170 Bulk Product c79cfef0 Clothing $477.09 39 3.9 97 BULK-56edc994
17171 Bulk Product 7879050c Clothing $506.75 408 3.3 71 BULK-bf11da53
17172 Bulk Product f71b823f Sports & Outdoors $943.74 159 0.7 37 BULK-955b5283
17173 Bulk Product 237afee7 Toys & Games $702.89 187 4.1 50 BULK-68351eb4
17174 Bulk Product f8eb44cd Electronics $148.07 4 2.3 95 BULK-90fb7cc8
17175 Bulk Product b6f44af0 Books $84.89 226 4.9 31 BULK-d0e39df4
17176 Bulk Product 49efaa08 Home & Garden $545.72 369 1.8 23 BULK-9e0ccd2a
17177 Bulk Product d78c9610 Sports & Outdoors $413.44 484 3.4 39 BULK-5695db07
17178 Bulk Product f9154156 Toys & Games $419.64 238 0.7 88 BULK-578567ea
17179 Bulk Product 0bb29d70 Home & Garden $367.18 115 4.3 0 BULK-b122b33a
17180 Bulk Product 65f2cdd7 Toys & Games $945.26 496 0.1 75 BULK-664a2bb1
17181 Bulk Product 044e766d Books $656.17 111 3.3 96 BULK-9b354e72
17182 Bulk Product 7604a9c6 Sports & Outdoors $996.05 163 0.4 35 BULK-af10a093
17183 Bulk Product 0d39a62f Electronics $862.18 428 1.1 42 BULK-5d86a27d
17184 Bulk Product 4d1297ad Clothing $743.03 83 0.0 74 BULK-bd48e7b8
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