EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;EXPLAIN reveals how the optimizer plans to execute the query.
Query tuning patterns with EXPLAIN, indexing, keyset pagination, deduplication, and optimization-oriented SQL recipes.
Inspect how queries run.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;EXPLAIN reveals how the optimizer plans to execute the query.
Run a query and collect timing or actual row information where supported.
EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE customer_id = 42;Use carefully in production because the query may actually execute.
Update table statistics for better planning.
ANALYZE orders;Exact command support varies, but up-to-date statistics matter for good execution plans.
Choose useful indexes and query shapes.
Speed up frequent equality or range predicates.
CREATE INDEX idx_users_email
ON users (email);Index columns used heavily in WHERE, JOIN, and ORDER BY clauses.
Create an index that supports filtering and ordering efficiently.
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at);Composite indexes can satisfy multiple parts of a query pattern.
Preserve index usability in predicates.
-- Less index-friendly
SELECT *
FROM users
WHERE LOWER(email) = 'a@example.com';
-- Prefer normalized storage or computed/indexed support where available
SELECT *
FROM users
WHERE email = 'a@example.com';Applying functions to indexed columns can prevent index usage in many engines.
Practical scalable query techniques.
Paginate by a stable cursor instead of a large OFFSET.
SELECT *
FROM posts
WHERE (published_at, post_id) < (TIMESTAMP "2025-03-01 10:00:00", 12345)
ORDER BY published_at DESC, post_id DESC
FETCH FIRST 20 ROWS ONLY;Keyset pagination scales better than OFFSET for deep pages.
Keep one row per business key and remove duplicates logically.
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at DESC) AS rn
FROM users
)
SELECT *
FROM ranked
WHERE rn = 1;Useful for data cleanup and canonical row selection.
Apply a simple wildcard search over several text fields.
SELECT *
FROM customers
WHERE first_name LIKE '%john%'
OR last_name LIKE '%john%'
OR email LIKE '%john%';Simple LIKE search is easy to start with, though full-text search is better at scale.
Checks, audits, and cleanup-oriented SQL.
Detect repeated values that should be unique.
SELECT email, COUNT(*) AS duplicate_count
FROM users
GROUP BY email
HAVING COUNT(*) > 1;This query is often the first step before cleanup and adding a unique constraint.
SELECT o.order_id
FROM orders o
LEFT JOIN customers c ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;Helps audit referential integrity problems.
Keep one row per key and delete the extras.
WITH ranked AS (
SELECT user_id, email,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at ASC) AS rn
FROM users
)
DELETE FROM users
WHERE user_id IN (
SELECT user_id
FROM ranked
WHERE rn > 1
);Always test duplicate-removal queries carefully before running them on production data.