ASP.NET Core Interview Question #194
Why is pagination important for performance?
Performance, Caching & HTTP Senior Advanced
Quick Interview Answer
Pagination limits the amount of data retrieved, processed, serialized, transferred, and rendered in a single request. Returning thousands of records at once increases database load, memory usage, network traffic, and response time.
Detailed Explanation
Pagination limits the amount of data retrieved, processed, serialized, transferred, and rendered in a single request. Returning thousands of records at once increases database load, memory usage, network traffic, and response time.
Offset pagination using Skip and Take is straightforward, but very deep offsets can become expensive on large datasets. Keyset or seek pagination can be more efficient when navigating sequentially through large ordered datasets.
Stable ordering is essential for predictable pagination.
Code Example
const int pageSize = 25;
var products = await db.Products
.AsNoTracking()
.OrderBy(p => p.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);