limit and offset are methods for limiting the number of query results and implementing pagination. These are essential methods for performance optimization and implementing paging functionality.
Basic Usage
limit
Limits the maximum number of results to return.offset
Skips a specified number of results.offset is typically used with limit for pagination.Pagination
Basic Pagination
Pagination Function
Cursor-Based Pagination
Cursor-based pagination is more efficient for large datasets than offset-based pagination.Basic Cursor Pagination
Cursor Pagination with ID
Practical Examples
- Infinite Scroll
- Data Table
- Batch Processing
- Latest Items
Performance Optimization
1. offset is Slow for Large Datasets
2. Use with orderBy
3. Optimize COUNT Queries
offset vs Cursor
offset Method
Advantages:- Simple implementation
- Can jump to any page
- Suitable for numbered pages
- Slow for large offsets
- Inconsistent results when data changes
- Always scans from beginning
Cursor Method
Advantages:- Fast regardless of position
- Consistent results
- Efficient memory usage
- Cannot jump to arbitrary pages
- Slightly complex implementation
- Cursor management needed
Cautions
1. Missing orderBy
2. Large offset
3. limit Without WHERE
4. Pagination and Data Changes
Type Safety
Type safety is maintained when using limit and offset.Next Steps
order-by
Sort results
where
Filter with conditions
select
Select fields to query
advanced-methods
Advanced query methods