SQLAdvanced#performance

How do you optimise a slow SQL query?

Read the execution plan, index the columns used in WHERE/JOIN/ORDER BY, select only needed columns, avoid functions on indexed columns, replace correlated subqueries with joins, and paginate large result sets.

Example
-- before
SELECT * FROM orders WHERE LOWER(email) = 'a@b.com';
-- after
CREATE INDEX idx_email_lower ON orders (LOWER(email));
SELECT id, total FROM orders WHERE LOWER(email) = 'a@b.com';

Related Questions

1
SQLAdvanced#scaling

What is partitioning and how does it differ from sharding?

Open
2
SQLIntermediate#architecture

OLTP vs OLAP?

Open
3
SQLIntermediate#security

What is SQL injection and how do you prevent it?

Open