Database view
Представление
A named SQL query can be exposed as a table-like object called a view. A regular view stores the query definition, while a materialized view also stores the computed rows.
Also known as: materialized view, материализованное представление
What a database view does
A view gives a query a name you can use in FROM or JOIN. Say several reports need the same definition of a completed order. Instead of copying the status filter into every report, you can define it in one view. Each report can then select the columns it needs, add filters, or join the view to other data.
Regular vs. materialized views
A regular view stores a query definition, not a separate set of rows. When you read from it, the database plans the defining query together with the query that references it. For example, the optimizer may apply an outer customer_id filter directly to the underlying table and use an index there. It does not necessarily compute every row in the view first. The result reflects the source data visible to the reading transaction.
A materialized view stores the result of its defining query. Consider a dashboard that repeatedly displays daily revenue: reading precomputed totals can be much cheaper than aggregating millions of orders on each page load. In return, the database needs storage for those totals and must do work to keep them up to date. Refresh behavior depends on the database. In PostgreSQL, for instance, you can schedule REFRESH MATERIALIZED VIEW to recompute the result; source changes do not update it automatically.
Example: a consistent definition for reporting
Suppose the orders table includes an order ID, a customer ID, an amount, and a status. This view lets reports agree on which orders count as paid:
CREATE VIEW paid_orders AS
SELECT id, customer_id, total
FROM orders
WHERE status = 'paid';
SELECT id, total
FROM paid_orders
WHERE customer_id = 42
ORDER BY id;
Once a status change to paid is visible to the reading transaction, the order can appear in the regular view without a refresh.
Where views help and where they don't
Views can centralize joins and business rules or give applications a stable set of column names while the underlying schema evolves. They can also limit what a reporting role sees. Omitting a sensitive column only protects it if the role cannot read it through the base table or another route, so check the actual grants.
Naming a query does not make it faster. Use EXPLAIN to see whether the database applies filters early or uses the indexes you expect. Also, if the caller needs a particular row order, put ORDER BY in the outer query rather than relying on the view to supply it.
Interview questions
How is a view different from a common table expression (CTE)? A view is a reusable schema object; a CTE names a result within one statement. Can you write through a view? That depends on the database and the definition. PostgreSQL can automatically update some simple single-table views, but not a view that groups rows with GROUP BY. On an updatable filtered view, WITH CHECK OPTION can reject inserts or updates that would leave the row outside the view's filter.