Is materialized view concept same as an indexed view in SQL server? - sql-server

I have always worked with SQL server.
I have recently come across materialized view (heard of this in a general database presentation):
The basic difference between View and Materialized View is that Views
are not stored physically on the disk. On the other hands,
Materialized Views are stored on the disc.
Is materialized view concept same as an indexed view in SQL server?

Here's what I found in Microsoft's documentation. (The emphasis in the quoted material is mine.)
I could be wrong, but it appears that Microsoft uses the term "Materialized View" specifically with Azure Synapse Analytics, and not with SQL Server generally.
From Views (SQL Server 2019)
Indexed Views
An indexed view is a view that has been materialized. This means the
view definition has been computed and the resulting data stored just
like a table. You index a view by creating a unique clustered index on
it. Indexed views can dramatically improve the performance of some
types of queries. Indexed views work best for queries that aggregate
many rows. They are not well-suited for underlying data sets that are
frequently updated.
From CREATE MATERIALIZED VIEW AS SELECT (Transact-SQL)
Applies to Azure Synapse Analytics
Remarks
A materialized view in Azure data warehouse is similar to an indexed
view in SQL Server.  It shares almost the same restrictions as indexed
view (see Create Indexed Views for details) except that a materialized
view supports aggregate functions.

Related

Create enforce on SQL Server Tables which is used in regular SQL View

I have created a SQL View by joining multiple tables and view is consumed by numerous real-time dashboards.
I want to ensure that no changes to the underlying schema impacts the SQL View. How can we enforce this?

Mirror table vs materialized view

From this excellent video "Microservices Evolution: How to break your monolithic database by Edson Yanaga" I know that there are different ways to split chunk of data as separate db for microservice:
View
Materialized View
Mirror Table using Trigger
Mirror Table using Transactional Code
Mirror Table using ETL tools
Event Sourcing
Could you please explain me the difference between mirrored table and materialized view?
I'm confused due to both of them are stored on disk...
My understanding is :-
Mirrored tables
Mirrored tables are generally an exact copy of the another, source table. Same structure and the same data. Some database platforms allow triggers to be created on the source table which will perform updates on the source table to the mirror table. If the database platform does not provide this functionality, or if the Use Case dictates, you may perform the update in transactional code instead of a trigger.
Materialized Views
A Materialized View contains the result of a query. With a regular database view, when the underlying table data changes, querying the view reflects those changes. However, with a materialized view the data is current only at the point in time of creation (or refresh) of the Materialized view. In simple terms, a materialized view is a snapshot of data at a point in time.

SQL Server Indexed Views vs Oracle Materialized View

I know materialized view and I'm using it. I have never used indexed views but I will. What are the differences between them ?
SQL Server’s indexed views are always kept up to date. In SQL Server, if a view’s base tables are modified, then the view’s indexes are also kept up to date in the same atomic transaction.
Oracle provides something similar called a materialized view. If Oracle’s materialized views are created without the **REFRESH FAST ON COMMIT** option, then the materialized view is not modified when its base tables are. So that’s one major difference. While SQL Server’s indexed views are always kept current, Oracle’s materialized views can be static.

How to create schema bound, cross-database view in SQL Server

I am attempting to create an indexed view on SQL Server 2008. I have a master database in which I cannot make any changes to (in terms of adding tables, views, etc.). However, I need to create some different views for various reasons that need to work with live data.
I have created a new database along side my master database so I can create views there. I am able to create views just fine, but I want to index some of the larger views. However, when I try to create a schema bound view cross-database, I receive the following error:
Cannot schema bind view 'dbo.Divisions' because name
'master.dbo.hbs_fsdv' is invalid for schema binding. Names must be in
two-part format and an object cannot reference itself.
Since I am going cross-database with the views, I have to reference the name in three-part format.
My creation statement for the view:
CREATE VIEW dbo.Divisions WITH SCHEMABINDING AS
SELECT master.dbo.hbs_fsdv.seq_ AS DivisionID,
master.dbo.hbs_fsdv.fs_division_desc_ AS Description
FROM master.dbo.hbs_fsdv
How can I create an indexed cross-database view in SQL Server?
Plain and simple. You can't. From the MSDN page:
The view must reference only base tables that are in the same database as the view.
https://msdn.microsoft.com/en-us/library/ms191432.aspx
Although (per the docs) it cannot be done directly with a simple SQL statement, this use case is very common and has a solution.
The architecture would have to involve caching the remote tables into your centralized database, and building the indexed view on top of them.
Some good notes on this can be found here:
What is the best way to cache a table from a (SQL) linked server view?
and
https://thomaslarock.com/2013/05/top-3-performance-killers-for-linked-server-queries/

SQL Server NOEXPAND with indexed view

I've been researching a bit on the use of indexed views and the noexpand hint in SQL Server and needed some clarification. Let's assume for this example you have two situations.
An indexed view is created against a table which already has an index created for the underlying table
An indexed view is created against a table which does not have an index created for the underlying table.
If we omit the noexpand hint, will SQL Server utilize the the view index in either situation? Or is noexpand only applicable when the table has an index and there is a choice to be made?
Secondly, does this behavior vary between editions of SQL Server? Has anything changed in 2012?
Depending what message board, blog, or MSDN documentation you read, it's a little unclear.

Resources