0
0
PostgreSQLquery~20 mins

Indexing materialized views in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Materialized View Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Index on Materialized View Query Performance

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?

PostgreSQL
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;
AThe index will cause the query to return incorrect results.
BThe index will not be used because materialized views do not support indexing.
CThe query will perform a full scan of the materialized view ignoring the index.
DThe query will use the index to quickly find rows for customer_id 123, improving performance.
Attempts:
2 left
💡 Hint

Think about how indexes help find data quickly in tables and materialized views.

🧠 Conceptual
intermediate
2:00remaining
When to Refresh Materialized Views with Indexes

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?

ADrop and recreate the materialized view to update data and indexes.
BOnly update the indexes manually; the data updates automatically.
CRun <code>REFRESH MATERIALIZED VIEW</code> to update the data and indexes.
DNo action is needed; materialized views update automatically.
Attempts:
2 left
💡 Hint

Materialized views store data physically and need manual refresh.

📝 Syntax
advanced
2:00remaining
Correct Syntax to Create an Index on a Materialized View

Which of the following SQL statements correctly creates a B-tree index on the order_date column of a materialized view named mv_orders?

ACREATE INDEX idx_order_date ON mv_orders(order_date);
BCREATE MATERIALIZED VIEW INDEX idx_order_date ON mv_orders(order_date);
CCREATE INDEX ON MATERIALIZED VIEW mv_orders(order_date);
DCREATE INDEX idx_order_date ON MATERIALIZED VIEW mv_orders(order_date);
Attempts:
2 left
💡 Hint

Creating an index on a materialized view uses the same syntax as for tables.

optimization
advanced
2:00remaining
Choosing Index Types for Materialized Views

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?

ACreate a BRIN index on the <code>location</code> column.
BCreate a GiST index on the <code>location</code> column.
CCreate a Hash index on the <code>location</code> column.
DCreate a B-tree index on the <code>location</code> column.
Attempts:
2 left
💡 Hint

Think about spatial data and which index type supports geometric queries.

🔧 Debug
expert
2:00remaining
Diagnosing Slow Queries on Indexed Materialized Views

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?

AThe query uses a function on <code>customer_id</code> preventing index use.
BThe materialized view was not refreshed after base table updates, so the index is outdated.
CThe index was created with the wrong column name.
DPostgreSQL does not support indexes on materialized views.
Attempts:
2 left
💡 Hint

Consider how query conditions affect index usage.