Sub topic 10
How the Database Runs a Query Against a View
Prerequisites
This chapter assumes that you can write SELECT queries, join tables, and understand basic database privileges. You should also know that a view is a named query that behaves like a table when you read from it.
A regular view usually does not store its result rows. When you query the view, the database combines the view definition with your query and plans the resulting statement.
The database expands the view
Suppose a database contains this view:
CREATE VIEW employee_directory AS
SELECT e.id,
e.name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id;You can query it like a table:
SELECT name
FROM employee_directory
WHERE department = 'Engineering';Conceptually, the database expands the view into a larger query:
SELECT e.name
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE d.name = 'Engineering';The database does not usually run one query to create the view and a second query to filter its result. Instead, the optimizer examines the combined query and chooses an execution plan.
Predicate pushdown
A condition in the outer query is called a predicate. The optimizer can often push that predicate into the view's underlying query. This is called predicate pushdown.
For the previous example, filtering departments before producing all joined rows can reduce the amount of data that must be joined. The optimizer might also use an index on departments.name or employees.department_id.
A view therefore does not automatically make a query slower. For a simple view, the optimizer can often produce the same plan as it would for the equivalent query written directly against the base tables.
| View feature | Typical optimization behavior |
|---|---|
| Join and filter | Often merged into the outer query |
| `GROUP BY` or `DISTINCT` | May form a boundary that limits rewrites |
| Nested views | Often expanded repeatedly, but complex definitions can produce difficult plans |
| Materialized view | Reads stored rows instead of expanding the original query |
When optimization becomes harder
Some view definitions contain operations that must happen before an outer query can safely filter rows. Examples include:
GROUP BY, which combines several rows into one result.DISTINCT, which removes duplicate rows.- Window functions, which calculate values across a set of rows.
- Set operations such as
UNION. - Expressions or functions that prevent use of an index.
- Several layers of nested views.
For example, filtering a grouped view may require the database to calculate groups before applying the outer condition. The optimizer can still improve the plan, but it has fewer valid rewrites.
Use the database's plan tool to see what actually happens. Common systems provide EXPLAIN:
EXPLAIN
SELECT name
FROM employee_directory
WHERE department = 'Engineering';The output differs by database system. Look for table scans, index scans, join methods, estimated rows, and expensive sorting or grouping steps. Compare the plan with the equivalent query written directly against employees and departments.
Regular views and materialized views
A regular view stores SQL text. Each query reads current data from the underlying tables.
A materialized view stores the result of its query. Reading it can be faster for expensive aggregations, but the stored rows can become stale. The database must refresh the materialized view before it reflects later changes.
Choose a regular view when current data and a reusable interface matter. Consider a materialized view when repeated, expensive reads justify refresh work and slightly delayed data is acceptable.
Exercise
A view joins orders and customers, and an outer query filters by customers.country. What should you inspect before assuming the view is slow?
Show answer
Inspect the view definition, indexes, statistics, and the EXPLAIN plan. The optimizer may push the country filter into the join and produce an efficient plan.