Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between view and indexed view or materialized view

I am confused in these two and tried to figure out the differences but didn't get something specific for which I am looking for.

  • Where to use indexed view over an ordinary view.
  • Some important differences between them.
like image 731
IShubh Avatar asked Sep 14 '18 07:09

IShubh


People also ask

What is the difference between view and index?

A view is just a way of abbreviating a subquery. An index is used to optimize matching column data.

Are materialized views indexed?

Unlike indexes, materialized views can be accessed directly using a SELECT statement. Note: The techniques shown in this chapter illustrate how to use materialized views in data warehouses. Materialized views can also be used by Oracle Replication.

Why use materialized view instead of a view?

Querying materialized views, unlike querying tables or logical views, can reduce query costs by maintaining results in memory that are only updated when necessary.

What are the different types of views in database?

There are two types of database views: dynamic views and static views. Dynamic views can contain data from one or two tables and automatically include all of the columns from the specified table or tables. Dynamic views are automatically updated when related objects or extended objects are created or changed.


2 Answers

The key difference is that materialized view is well, materialized. This basically means that data is being persisted into a virtual table which is maintained by SQL Server itself.

This has both benefits and issues. Notable benefits:

  • Commonly used queries can be encapsulated in a view and indexed in order to improve read performance (compare running select from a single table versus, for instance, 5 tables that are joined)
  • Aggregations can be precomputed and would also improve read performance

Drawbacks:

  • It will certainly impact write performance because with each DML operation, SQL Server will have to update view. This can be observed in execution plans
  • It can negatively impact replication performance if subscriber creates a material view from a replicated table
  • A lot of restrictions in order to create an indexed view
  • If you're using a non-enterprise SQL Server version, WITH (NOEXPAND) hint must be added, otherwise SQL Server will expand view and will just run SQL statement within it and totally ignore index.
  • DBAs usually tend to avoid them because they add extra maintenance.
like image 196
Evaldas Buinauskas Avatar answered Oct 08 '22 11:10

Evaldas Buinauskas


Views (unindexed) is really nothing more than a way to put a query in a nice, clean, table-like thing. It takes up no space, because it doesn't contain anything until it's queried. There are almost no restrictions on what you can or cant put in said query.

Indexed views are just what they say on the tin. They're views, but indexed. What that means is that it materializes the view and keeps it up to date via the transaction log and stuff.

Why not index every view? Basically they come with a laundry list of limitations, the potential for maintenance and blocking issues, and you lose many of the lightweight nature of a normal view.

End of the day, if you need an indexed view, you need an indexed view. But by default, they're probably more trouble than they're worth.

like image 37
Xedni Avatar answered Oct 08 '22 13:10

Xedni