Sub topic 12
Row Growth, Indexes, and Checking Your Results
Assume you know basic SELECT, WHERE, ORDER BY, INNER JOIN, LEFT JOIN, primary keys, foreign keys, NULL, GROUP BY, and aggregate functions. This chapter focuses on what happens to row counts when self joins and cross joins run, how indexes affect them, and how to check that a result is correct.
Predict row growth before running a query
A self join can increase the number of rows because one row can match several rows on the other side. For example, if several employees have the same manager, a join on manager_id returns one row for each matching relationship.
A cross join has a simpler rule. If the first input has n rows and the second input has m rows, the result has n × m rows.
| First input | Second input | Cross join result |
|---|---|---|
| 3 rows | 2 rows | 6 rows |
This multiplication happens before later filters or grouping. A cross join between two tables with 10,000 rows each can produce 100 million row combinations.
SELECT COUNT(*) AS combination_count
FROM sizes s
CROSS JOIN colors c;A LEFT JOIN after a cross join can preserve every generated combination, but the final count can be larger than the number of combinations if multiple fact rows match one combination. Aggregation is often needed to reduce those matches back to one report row per combination.
Understand indexes on join columns
An index is an additional data structure that helps a database find rows without scanning an entire table. Indexes often help joins when the database must find matching rows by a key.
For the employee-manager query, the database may search employees.id for each manager_id. The primary key usually creates an index on id. An index on the child-side foreign key can also help when the query starts with managers and looks for their employees.
CREATE INDEX employees_manager_id_idx
ON employees (manager_id);This index does not reduce the number of rows that the query must logically return. It can reduce the work needed to find those rows. Indexes also take storage and make inserts, updates, and deletes more expensive because the index must be maintained.
A cross join has no matching condition to speed up. An index cannot prevent the product of both input sizes. Indexes may still help filters or joins applied after the cross join, but the query should usually filter inputs before generating combinations.
Check the execution plan
An execution plan shows how the database intends to run a query. Most systems provide an EXPLAIN command, although the exact syntax and output differ by database.
EXPLAIN
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.id
ORDER BY e.id;Look for full table scans on large tables, unexpected join methods, and estimated row counts that differ greatly from actual counts. A scan is not automatically bad. A database may choose a scan when it expects to read most of a small table.
Use the database's runtime plan option, such as EXPLAIN ANALYZE where supported, to compare estimates with actual work. Test with realistic data. A query that is fast for 100 rows may be slow for millions.
Validate the result, not only the speed
Before trusting a self join or cross join, write down the expected result shape.
- For an employee-manager report, expect at most one manager row per employee when
idis unique. - For a cross join, multiply the input counts and compare that number with the result count.
- For a gap-filled report, verify that combinations with no facts have zero rather than disappearing.
- Check whether
NULLvalues represent missing parents or indicate a data problem. - Inspect duplicate keys on both sides when a join returns more rows than expected.
This query can reveal duplicate manager identifiers in a relationship table:
SELECT manager_id, COUNT(*) AS employee_count
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
ORDER BY employee_count DESC;The count is expected to be greater than one for a manager with several direct reports. Unexpected multiplication usually means the join condition is incomplete or the data does not have the uniqueness assumption you made.
Exercise
Tables A and B contain 4 and 7 rows. What is the maximum row count of A CROSS JOIN B, before any filter?
Show answer
28 rows, because a cross join returns 4 × 7 combinations.