
They can help hide complexity and provide database users with a nicer API. Traditional database views can be really helpful. To better optimize your materialized view queries, you can add indexes to the materialized view columns just as you would with a database table.ĬREATE INDEX my_index_1 ON my_view (some_column) ĬREATE INDEX my_index_2 ON my_view (some_other_column) REFRESH MATERIALIZED VIEW CONCURRENTLY my_view PostgreSQL 9.4 allows you to refresh your view in a way that enables queries during the refresh: This can be a problem if your application can’t tolerate downtime while the refresh is happening. This will re-execute the query used to create it.Įxecuting this refresh query will lock the materialized view so it can’t be accessed while refreshing.
#Create view psql update
To update the contents of a materialized view, you can execute a query to refresh it. You can’t insert data into a materialized view as you can with a table. You can then write other queries against my_view just as you would with a traditional view or database table. Unlike views, their underlying query is not executed every time you access them.Ĭreating a materialized view is pretty simple:Įxecuting the above query will create a materialized view populated with the data from the query.

Like views, they are defined by a database query. Materialized views are disc-stored views that can be refreshed. In this post, I’ll review what materialized views are, when you should consider using them, and what we learned while bringing them into our project.

To solve this problem, we ended up using a materialized view (we are using a PostgreSQL database). This was going to be a very expensive and slow query. Our team recently found itself in a situation where we needed to write a database query to:
