SQL Optimization Techniques

Use LIMIT for Pagination Instead of OFFSET

General Tuning
Performance

Pagination with OFFSET can degrade in performance—use indexed bookmarks instead.

Ourv0.1-alphais coming in May 2025.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Performance Insights

While OFFSET is a common way to implement pagination in SQL, it performs poorly as datasets grow. This is because the database must scan and skip over every row before your desired page—creating a performance penalty that compounds with each request. For example, paginating with OFFSET 10000 forces the engine to examine and discard the first 10,000 rows before returning anything.

Instead, you can use keyset pagination, also known as indexed pagination. This method uses a WHERE clause to bookmark your last-seen ID (or timestamp), allowing the query to jump directly to the starting point for the next page. It’s faster, more scalable, and works well with indexes.

For instance, rather than using OFFSET 10000, you could store the last ID returned (e.g., last_seen_id) and filter with WHERE id < last_seen_id ORDER BY id DESC LIMIT 10. This allows the query to perform a targeted index scan instead of a full traversal, maintaining consistent performance even on datasets with millions of rows.

Galaxy’s optimizer detects inefficient OFFSET usage and recommends a keyset-based alternative automatically. For high-volume pagination—especially in logs, timelines, or customer lists—this change alone can turn a sluggish 5-second query into a 50-millisecond response.

Before Optimization

SELECT * FROM logs ORDER BY id DESC OFFSET 10000 LIMIT 10;

After Optimization

SELECT * FROM logs WHERE id < last_seen_id ORDER BY id DESC LIMIT 10;

Estimated Improvement

Constant-time pagination on large datasets

Check out other SQL optimizations!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Comulate
Truvideo Logo