Index on materialized views
- SQL course
- Advanced
- Lesson 34/81
- 4 min
Add an index to a materialized view.
Index on materialized view
You can create an index on a materialized view with CREATE INDEX. It speeds up queries.
CREATE INDEX idx_mv_date ON daily_revenue (sale_date);This query adds an index on the date column.
The index idx_mv_date is created.
Index on materialized view — details
Since a materialized view is like a table, you can add indexes. Queries get noticeably faster.
For example, adding CREATE INDEX on sale_date lets a report that asks for a specific date find it without scanning every row.
Practice Index on materialized view
Add a sale_date index on the daily_revenue materialized view.
Starter code
-- mat view index
CREATE INDEXSample solution
CREATE INDEX idx_mv_date ON daily_revenue (sale_date);Expected output
İndeks oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Add CREATE INDEX
CREATE ____ idx_mv_date ON daily_revenue (sale_date);
CREATE INDEX idx_mv_date ON daily_revenue (sale_date);
Explanation: Because a materialized view is stored like a real table, you can add an index with CREATE INDEX.
2 Index on materialized views: match the terms
| Term | Meaning |
|---|---|
index |
speeds up |
materialized |
physical |
CREATE INDEX |
create |
query |
fast |
3 Can you add an index to a materialized view?
- No
- Yes Correct answer
- In some cases
- Only to a view
Explanation: You can add an index to a materialized view.