View

A View is a virtual table that does not store data, essentially a saved SQL query. Each time a view is queried, the system executes the underlying SQL in real time and returns the results.

Type Selection Reference

Comparison ItemViewMaterialized ViewDynamic Table
Data storageNo storageStores dataStores data
Query performanceSame as underlying tablesHigh (pre-computed)High (pre-computed)
Data freshnessReal-timeWithin refresh intervalWithin refresh interval
Suitable scenariosLogic encapsulation, permission isolationQuery accelerationData processing pipelines

When to use a view: Encapsulating complex JOIN logic, exposing only certain columns externally (permission isolation), and scenarios where data storage is not needed.

Typical Use Cases

  • Simplify queries: Encapsulate multi-table JOINs as views so business users can query the view directly
  • Permission isolation: Expose only certain columns to specific users, hiding sensitive fields
  • Logic reuse: Define common calculation logic once and reference it in multiple places

Quick Example

-- Create a view: hide sensitive fields
CREATE VIEW v_user_public AS
SELECT user_id, username, city
FROM users;
-- Do not expose sensitive columns like phone, id_card

-- Create a view: encapsulate complex JOINs
CREATE VIEW v_order_detail AS
SELECT o.order_id, u.username, p.product_name, o.amount
FROM orders o
JOIN users u ON o.user_id = u.user_id
JOIN products p ON o.product_id = p.product_id;