Introduction
A materialized view saves the result of a query so you can quickly get the data without running the query again.
CREATE MATERIALIZED VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
CREATE MATERIALIZED VIEW recent_orders AS SELECT order_id, customer_id, order_date FROM orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
CREATE MATERIALIZED VIEW top_customers AS SELECT customer_id, SUM(amount) AS total_spent FROM sales GROUP BY customer_id ORDER BY total_spent DESC LIMIT 10;
CREATE MATERIALIZED VIEW active_users AS SELECT user_id, last_login FROM users WHERE last_login > CURRENT_DATE - INTERVAL '30 days'; -- To see the data: SELECT * FROM active_users;
CREATE MATERIALIZED VIEW in PostgreSQL?sales_summary from a table sales?CREATE TABLE products (id INT, price INT); INSERT INTO products VALUES (1, 100), (2, 200); CREATE MATERIALIZED VIEW cheap_products AS SELECT * FROM products WHERE price < 150; SELECT * FROM cheap_products;
CREATE MATERIALIZED VIEW recent_orders AS SELECT * FROM orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; REFRESH MATERIALIZED VIEW recent_orders;
sales_data that updates daily. You want to create a materialized view monthly_summary that shows total sales per month. Which approach correctly creates and maintains this materialized view efficiently?Pass the test and this pattern moves unknown