Consider a materialized view mv_sales_summary created from a large sales table. You create a B-tree index on the customer_id column of the materialized view.
What is the expected effect when you run a query filtering by customer_id on mv_sales_summary?
CREATE MATERIALIZED VIEW mv_sales_summary AS SELECT customer_id, SUM(amount) AS total FROM sales GROUP BY customer_id; CREATE INDEX idx_mv_customer ON mv_sales_summary(customer_id); -- Query: SELECT * FROM mv_sales_summary WHERE customer_id = 123;
Think about how indexes help find data quickly in tables and materialized views.
Materialized views in PostgreSQL can have indexes just like tables. Creating an index on customer_id allows queries filtering on that column to use the index, speeding up data retrieval.
You have a materialized view with several indexes. After updating the base tables, what must you do to ensure the materialized view and its indexes reflect the latest data?
Materialized views store data physically and need manual refresh.
Materialized views do not update automatically when base tables change. You must run REFRESH MATERIALIZED VIEW to update the data and rebuild the indexes accordingly.
Which of the following SQL statements correctly creates a B-tree index on the order_date column of a materialized view named mv_orders?
Creating an index on a materialized view uses the same syntax as for tables.
Materialized views are indexed like tables. The correct syntax is CREATE INDEX index_name ON materialized_view(column);
You have a materialized view with a column location storing geographic points. You want to optimize queries filtering by location within a radius.
Which index type is best to create on location for this purpose?
Think about spatial data and which index type supports geometric queries.
GiST indexes support spatial data types and queries like radius searches. B-tree and Hash indexes do not support spatial operators efficiently.
You created a materialized view mv_customer_orders with an index on customer_id. However, queries filtering by customer_id are still slow and do not use the index.
What is the most likely cause?
Consider how query conditions affect index usage.
If the query applies a function or expression on the indexed column (e.g., LOWER(customer_id)), the index on the raw column cannot be used unless a functional index exists.