Query Optimization Fundamentals: Real-World
Module: Query Optimization & Performance
Transform slow queries from 30+ seconds to under 1 second. Essential for production systems handling millions of records. Used daily by backend engineers, DBAs, and data engineers to maintain application performance and user satisfaction.
Amazon - Product Search Query Optimization
Amazon's product search was experiencing 5-10 second response times during peak traffic, causing cart abandonment. The search query joined products, categories, reviews, and inventory tables with complex filtering. With 500M+ products and 50K concurrent users, this was causing database CPU to spike to 95%.
E-commerce
Optimization strategy: 1) Added composite indexes on (category_id, price, rating) for common filter combinations, 2) Created covering indexes including product_name and image_url to avoid table lookups, 3) Implemented query result caching with Redis for popular searches (80% hit rate), 4) Denormalized frequently accessed review statistics into products table, 5) Partitioned products table by category for better query pruning, 6) Rewrote correlated subqueries as JOINs, 7) Added full-text search indexes for product names and descriptions.
-- BEFORE: Slow product search (8 seconds)
SELECT *
FROM products p
LEFT JOIN categories c ON p.category_id = c.category_id
WHERE p.category_id = 123
AND p.price BETWEEN 10 AND 100
AND (SELECT AVG(rating) FROM reviews r WHERE r.product_id = p.product_id) >= 4.0
AND p.in_stock = true
ORDER BY p.popularity_score DESC
LIMIT 20;
-- AFTER: Optimized search (0.15 seconds)
-- Step 1: Denormalize review stats
ALTER TABLE products ADD COLUMN avg_rating DECIMAL(3,2);
CREATE INDEX idx_products_search ON products(category_id, price, avg_rating, in_stock);
-- Step 2: Optimized query
SELECT
p.product_id,
p.product_name,
p.price,
p.avg_rating,
p.image_url,
c.category_name
FROM products p
INNER JOIN categories c ON p.category_id = c.category_id
WHERE p.category_id = 123
AND p.price BETWEEN 10 AND 100
AND p.avg_rating >= 4.0
AND p.in_stock = true
ORDER BY p.popularity_score DESC
LIMIT 20;
-- Result: 53x faster (8s -> 0.15s)
Reduced search response time from 5-10 seconds to under 200ms (25-50x improvement). Database CPU utilization dropped from 95% to 35%. Cart abandonment rate decreased by 12%, resulting in $50M+ additional annual revenue. Customer satisfaction scores improved by 18 points. The optimization eliminated the need for $200K in additional database hardware.
PostgreSQL
Facebook - News Feed Query Optimization
Facebook's news feed query was taking 3-5 seconds to load for users with 1000+ friends, causing poor user experience. The query needed to fetch posts from friends, pages, and groups, rank them by relevance, and apply privacy filters. With 2.9B users and billions of posts, this was a massive scale challenge.