A materialized view is a database object that stores the result of a query physically. It allows faster access to complex query results by saving the data instead of computing it each time.
Click to reveal answer
beginner
Why do we create indexes on materialized views?
Indexes on materialized views improve query performance by allowing faster data retrieval from the stored results, just like indexes on regular tables.
Click to reveal answer
intermediate
How do you create an index on a materialized view in PostgreSQL?
Use the standard CREATE INDEX command on the materialized view name, for example: CREATE INDEX idx_name ON mat_view_name(column_name);
Click to reveal answer
intermediate
Does refreshing a materialized view affect its indexes?
Refreshing a materialized view updates its data but does not drop or recreate its indexes. The indexes remain and continue to speed up queries.
Click to reveal answer
beginner
What is the difference between a materialized view and a regular view regarding indexing?
Regular views do not store data physically and cannot have indexes. Materialized views store data physically and can have indexes to improve query speed.
Click to reveal answer
What command creates an index on a materialized view column in PostgreSQL?
ACREATE MATERIALIZED VIEW INDEX idx ON mat_view(column);
BCREATE INDEX idx ON mat_view(column);
CCREATE VIEW INDEX idx ON mat_view(column);
DCREATE INDEX idx ON VIEW mat_view(column);
✗ Incorrect
The correct syntax is CREATE INDEX on the materialized view name and column.
What happens to indexes when you refresh a materialized view?
AIndexes remain intact and continue to work.
BMaterialized views cannot have indexes.
CIndexes are disabled temporarily.
DIndexes are dropped and must be recreated.
✗ Incorrect
Refreshing updates data but does not drop or disable indexes.
Which of the following is true about materialized views?
AThey do not store data physically.
BThey automatically update when base tables change.
CThey can have indexes to speed up queries.
DThey cannot be refreshed.
✗ Incorrect
Materialized views store data physically and can have indexes.
Why might you choose to index a materialized view?
ATo speed up queries on the stored data.
BTo save storage space.
CTo slow down query performance.
DTo prevent data refresh.
✗ Incorrect
Indexes speed up data retrieval from materialized views.
Can you create a unique index on a materialized view?
AOnly if the materialized view is refreshed concurrently.
BNo, unique indexes are not allowed.
COnly on regular views, not materialized views.
DYes, just like on regular tables.
✗ Incorrect
Materialized views support unique indexes similar to tables.
Explain what a materialized view is and why indexing it can improve performance.
Think about how saving data and adding indexes helps avoid repeating complex queries.
You got /3 concepts.
Describe the process and syntax to create an index on a materialized view in PostgreSQL.
It is similar to creating an index on a regular table.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of creating an index on a materialized view in PostgreSQL?
easy
A. To speed up query performance on the materialized view
B. To automatically refresh the materialized view
C. To store the materialized view data physically
D. To prevent the materialized view from being updated
Solution
Step 1: Understand what an index does
An index helps the database find data faster by creating a quick lookup structure.
Step 2: Apply this to materialized views
Since materialized views store query results physically, indexing them speeds up queries on that stored data.
Final Answer:
To speed up query performance on the materialized view -> Option A
Quick Check:
Indexing = Faster queries [OK]
Hint: Indexes speed up data retrieval on stored results [OK]
Common Mistakes:
Thinking indexes refresh the view automatically
Confusing storage with indexing
Believing indexes prevent updates
2. Which of the following is the correct syntax to create an index on a materialized view named sales_summary on column region?
easy
A. CREATE INDEX idx_region ON TABLE sales_summary(region);
B. CREATE INDEX idx_region ON MATERIALIZED VIEW sales_summary(region);
C. CREATE INDEX idx_region ON sales_summary(region);
D. CREATE INDEX idx_region ON VIEW sales_summary(region);
Solution
Step 1: Recall index creation syntax
Indexes are created with CREATE INDEX index_name ON table_name(column); syntax.
Step 2: Apply to materialized views
Materialized views behave like tables for indexing, so no extra keywords like MATERIALIZED VIEW or VIEW are used.
Final Answer:
CREATE INDEX idx_region ON sales_summary(region); -> Option C
Quick Check:
Index syntax = CREATE INDEX name ON table(column) [OK]
Hint: Use table syntax for materialized views when creating indexes [OK]
Common Mistakes:
Adding MATERIALIZED VIEW keyword in index creation
Using VIEW keyword instead of table name
Incorrect syntax with TABLE keyword
3. Given a materialized view mv_orders with an index on order_date, what will be the output of this query?
SELECT order_date FROM mv_orders WHERE order_date = '2023-01-01';
Assuming the materialized view is refreshed and contains data for that date.
medium
A. Returns all rows with order_date '2023-01-01' quickly using the index
B. Returns an error because indexes cannot be used on materialized views
C. Returns no rows because materialized views do not store data
D. Returns all rows but ignores the index, causing slow performance
Solution
Step 1: Understand index usage on materialized views
Indexes on materialized views speed up queries by quickly locating matching rows.
Step 2: Analyze the query behavior
The query filters by order_date, which is indexed, so it returns matching rows efficiently.
Final Answer:
Returns all rows with order_date '2023-01-01' quickly using the index -> Option A
Quick Check:
Indexed query = fast matching rows [OK]
Hint: Indexed columns speed up matching queries on materialized views [OK]
Common Mistakes:
Thinking indexes don't work on materialized views
Assuming materialized views are empty without refresh
Believing query ignores indexes always
4. You created an index on a materialized view but notice queries are still slow. What is the most likely cause?
medium
A. Indexes do not work on materialized views in PostgreSQL
B. The materialized view has not been refreshed after data changes
C. The index was created on a non-existent column
D. Materialized views cannot have indexes on date columns
Solution
Step 1: Understand materialized view refresh
Materialized views store data physically but need manual refresh to update data and indexes.
Step 2: Connect refresh to query speed
If not refreshed, the view and its index are outdated, causing slow or incorrect query results.
Final Answer:
The materialized view has not been refreshed after data changes -> Option B
Quick Check:
Refresh needed for updated data and index [OK]
Hint: Always refresh materialized views after data changes [OK]
Common Mistakes:
Believing indexes don't work on materialized views
Creating index on wrong column silently
Thinking date columns can't be indexed
5. You have a materialized view mv_sales refreshed daily. You want to speed up queries filtering by customer_id and sale_date. Which is the best indexing strategy?
hard
A. Do not create any index; rely on sequential scan
B. Create separate indexes on customer_id and sale_date individually
C. Create an index only on sale_date because it is a date column
D. Create a single composite index on (customer_id, sale_date)
Solution
Step 1: Understand composite vs single indexes
A composite index on multiple columns speeds up queries filtering by both columns together.
Step 2: Apply to query filtering by customer_id and sale_date
Since queries filter on both columns, a composite index is more efficient than separate indexes.
Final Answer:
Create a single composite index on (customer_id, sale_date) -> Option D
Quick Check:
Composite index = faster multi-column filters [OK]
Hint: Use composite index for multi-column filtering queries [OK]
Common Mistakes:
Creating separate indexes causing less efficient queries
Indexing only one column when filtering by two
Skipping indexes and relying on slow scans
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.