SQL Indexing and Performance

Query tuning patterns with EXPLAIN, indexing, keyset pagination, deduplication, and optimization-oriented SQL recipes.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Execution Plans

Inspect how queries run.

Explain a query

Inspect the execution plan for a SELECT statement.

sqlANYexplainperformance
sql
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;

EXPLAIN reveals how the optimizer plans to execute the query.

Explain with runtime stats

Run a query and collect timing or actual row information where supported.

sqlANYexplain-analyzeperformance
sql
EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE customer_id = 42;

Use carefully in production because the query may actually execute.

Refresh optimizer statistics

Update table statistics for better planning.

sqlANYanalyzestatisticsperformance
sql
ANALYZE orders;

Exact command support varies, but up-to-date statistics matter for good execution plans.

Index Strategy

Choose useful indexes and query shapes.

Index a common lookup column

Speed up frequent equality or range predicates.

sqlANYindexlookupperformance
sql
CREATE INDEX idx_users_email
ON users (email);

Index columns used heavily in WHERE, JOIN, and ORDER BY clauses.

Use a covering index pattern

Create an index that supports filtering and ordering efficiently.

sqlANYindexcomposite-indexcovering-index
sql
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at);

Composite indexes can satisfy multiple parts of a query pattern.

Avoid wrapping indexed columns in functions

Preserve index usability in predicates.

sqlANYindexperformancepredicates
sql
-- 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.

Data Quality and Maintenance Queries

Checks, audits, and cleanup-oriented SQL.

Find duplicate business keys

Detect repeated values that should be unique.

sqlANYdata-qualityduplicates
sql
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.

Find orphaned child rows

Detect child rows missing a valid parent.

sqlANYdata-qualityorphaned-rows
sql
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.

Delete duplicate rows with a CTE

Keep one row per key and delete the extras.

sqlANYdeleteduplicatescte
sql
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.

Recommended next

No recommendations yet.