O'Reilly logo

Rails, Angular, Postgres, and Bootstrap by David B. Copeland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​​

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required