4

What's the actual difference between View and Materialized View?

I have gone through lot of links where they said normal View is a like a virtual table and data cannot be stored on a disk where in case materialized data will get stores on the disk. But from there I'm getting nothing like when to use normal view and when to use Materialized view. If I create both the views I can see in my schema. I want to know exact when and where we use the normal view and we use materialized view. Please provide me the solution with practical example.

Thanks

APC
  • 144,005
  • 19
  • 170
  • 281
Jaspreet Singh
  • 155
  • 1
  • 10
  • Possible duplicate of [What is the difference between Views and Materialized Views in Oracle?](https://stackoverflow.com/q/93539/3404097) – philipxy May 25 '19 at 06:57

1 Answers1

13

A view is just a stored query. It's a handy way of saving some complicated business logic (joins, filters, derived values) so it can be reused and shared with other users.

A materialized view is also a way of saving a query but it materializes the result set. That is, it creates a copy of the data on disk.

The main reason for using a materialized view is improved performance. Every time we select from a view we execute the whole query; if it's an expensive query that's a cost we pay each time. With a materialized view we trade disk space for time.

Furthermore, we can treat a materialized view like a table: we can index them, we can add key constraints, we can even reference them in a foreign key.

The main reason not to use a materialized view is cost. They take up disk space. They also have to be maintained. By default materialized views are static, which means their data gradually becomes stale over time. Refreshing a materialized view can be burdensome (depending on the underlying query). Whereas querying a view always gives us the most up-to-date view of the data.

The following are only guidelines, and neither complete or distinct

When to use a view

  • when we want to make a query reusable, shareable and controllable
  • when we want to have a simple interface over internal complexity
  • when we want to enforce access restrictions to our data

When to use a materialized view

  • when we want to run standard summarising (usually aggregating) queries against a large volume of data
  • when we need data from another database and we can't guarantee its availability
  • the view use cases when the volume of the base data is sufficiently large and the performance requirements sufficiently strict that it's cost effective to materialize the data

The answers to your questions are in the documentation(*). I have no intention of rewriting that fine manual but I will address your questions because having started I am condemned to continue.

  1. A materialized view is a copy of data from one or more tables, perhaps in other schemas or even other databases.
  2. As I said, copying data comes with overheads. Storage space and dealing with stale data are the big costs.
  3. A view is just a query, there is literally nothing to index.
  4. select * from user_extents where segment_name = 'name of mview'
  5. Refresh on commit is not free. It costs system resources to execute (transactions over the source table will take longer). Besides, many materialized views can only support complete on demand refresh.

(*) This is Oracle's documentation because that's what your profile suggests but other RDBMS platforms have similar docs.

APC
  • 144,005
  • 19
  • 170
  • 281
  • @apc......1)You want to say that view and materialized view both are handy way to save the query and logics but materialized view uses disk space to replicate the data which normal view don't,right? 2)Still there is a little bit confuion if both are handy way to save the query and logics then the only reason we don't use materialized view just beause they take disk space? 3)We can apply indexes, in materialized view that we can't apply in view? – Jaspreet Singh May 23 '19 at 15:53
  • @apc......4) How we can check that materialized view uses disk space to replicate the data? 5)Like you said refreshing a materialized view can be burdensome but while creating the materialized view we have the option whether we want to refresh the materialized view on Commit or on Demmand and if we go for the Refresh on Commit then when ever user insert's the data and do commit then materialized view will get's refersh automatically? – Jaspreet Singh May 23 '19 at 15:54