Joins & Relationships
INNER JOIN Deep Dive: Performance
INNER JOIN performance depends on indexes on join columns (foreign keys and primary keys), table sizes (smaller tables join faster), number of matching rows (fe
INNER JOIN performance depends on indexes on join columns (foreign keys and primary keys), table sizes (smaller tables join faster), number of matching rows (fewer matches = faster), and WHERE clause filtering (filter early when possible).
Index ALL join columns - most critical optimization (100x speedup possible)
Create index on foreign keys: CREATE INDEX idx_orders_customer ON orders(customer_id)
Primary keys usually auto-indexed, but verify with SHOW INDEXES
Use covering indexes that include all SELECT columns for index-only scans
Filter with WHERE to reduce rows before or after join
Avoid functions on join columns: LOWER(c.id) prevents index usage
Select specific columns, not SELECT * to reduce data transfer
Keep table statistics updated: ANALYZE TABLE for optimal query plans
Smaller table × larger table usually faster (optimizer handles this)
Use EXPLAIN ANALYZE to identify missing indexes and slow operations
Forgetting ON clause creates cartesian product (every row × every row)
Not indexing foreign key columns causes full table scans
Using old comma syntax (FROM a, b WHERE) instead of explicit INNER JOIN
Confusing INNER JOIN (excludes non-matches) with LEFT JOIN (preserves left table)
Putting filters in ON clause instead of WHERE (works but less clear)
Using functions on join columns: ON YEAR(o.date) = c.year prevents indexes
Expecting NULL foreign keys to match (they never do)
Not using table aliases makes queries verbose and hard to read
Selecting SELECT * when only few columns needed wastes resources
Assuming join order matters (optimizer chooses, but indexes matter more)