Skip to content

Query Performance

Database

Optimize SQL query performance: execution plans, N+1, and pagination.

Official Site
01

Overview

Read EXPLAIN ANALYZE to understand execution. Avoid SELECT *, fetch only needed columns. Fix N+1 with JOINs or batch loading. Use OFFSET/LIMIT or keyset pagination. Avoid functions on indexed columns in WHERE.

bash
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id=1;
-- Avoid N+1: use JOIN
SELECT u.*,o.* FROM users u JOIN orders o ON o.user_id=u.id;
-- Keyset pagination (faster than OFFSET)
SELECT * FROM items WHERE id>last_id ORDER BY id LIMIT 20;
-- Avoid: WHERE YEAR(created)=2024 (kills index)
-- Use: WHERE created>="2024-01-01"
02

Best Practices

Use EXPLAIN ANALYZE. Avoid SELECT *. Fix N+1 with JOIN/eager loading. Keyset pagination over OFFSET. Avoid functions on indexed columns. Use prepared statements. Batch inserts. Monitor slow query log.

Ready to try Query Performance?

Visit the official site for the latest version and full documentation.

Visit Query Performance