Using Materialized Views for Better Performance
Materialized views are a special form of database view that performs much better. If you aren’t familiar with views, they are a table-like construct that abstracts away a complex query. For example, we could create a view named ADDRESSES_WITH_STATES that abstracts away the need to join ADDRESSES and STATES together, like so:
| CREATE VIEW addresses_with_states AS |
| SELECT |
| addresses.id, |
| addresses.street, |
| addresses.city, |
| states.code AS state, |
| addresses.zipcode |
| FROM |
| addresses |
| JOIN states ON states.id = addresses.state_id; |
Now, we can treat ADDRESSES_WITH_STATES just like a normal table for querying:
| sql> select * from addresses_with_states |
Get Rails, Angular, Postgres, and Bootstrap now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.