Joins are powerful but can exponentially increase query load when not indexed correctly. MySQL needs a fast way to match rows when joining two tables. Without indexes, it may scan the tables repeatedly, multiplying the workload.
For example, if
type is listed as
ALL for both tables being joined and there are 8,000 orders (rows) and 2,000 customers (rows). MySQL could process millions of combinations for this join.
You can address this by indexing the join column (
CREATE INDEX idx_customer_id ON customers(id);). This may shift the
customers table
type to something more efficient like
eq_ref. But keep in mind:
- The column needs to be unique or primary in the referenced table.
- All parts of any composite index must be covered by the join condition.
- The optimizer must consider it the best plan.