Indexed View in SQL Server - sql-server

Can I create an index on a view which references synonyms which point to a linked server? Since the view isn't based on local tables I wasn't sure if it was possible.

Nope.
You can't schema bind a view that references external objects and this is only one prerequisite for indexing it.
An indexed view can only reference objects within the database.

Related

Is materialized view concept same as an indexed view in 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.

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 Transaction Replication For Indexed Views

I am doing transaction replication for indexed views. I have other replicating schemabound views that reference the indexed views using the NOEXPAND hint. Even though I call sp_addarticle for the NOEXPANDing views after calling sp_addarticle for the indexed views, I'm getting the error:
Hint 'noexpand' on object '...' is invalid.
because SQL Server is trying to create the NOEXPANDing view at the target server before creating the index on the indexed view.
Is there a way to force SQL Server to finish replicating the indexed view indexes before starting on the NOEXPANDing views?
Have a look on the distributor database and you'll find scripts for pre and post replication. These are straight forward sql scripts so you can modify these and put whatever you like in them.
That means you could modify the pre-repl script to avoid the error and modify the post-repl script to add the noexpanding view after the index has been created.
I think i've found an easier way.
When adding an indexed view to replication using the GUI, will only copy across the schema definition meaning any Stored Procs that try to access these views will error if they contain a NOEXPAND hint.
Now I guess you can mess around with the pre/post scripts but using this article from MSDN: Indexed Views - Replications - NoExpand Hint there seems a simpler option.
You can replicate the indexes by changing the script for sp_addarticle and replace #schema_option from 0x0000000008000001 to 0x0000000008000051. The following example allows both clustered and non-clustered index to be generated at the subscriber.
This pulls across the indexes automatically and has meant less scripting too.

HBM2DDL -- Create a database view instead of a Table?

All,
Is there some setting that I can tell hbm2ddl to run a view creation statement instead of create a table when generating the database schema?
I'm creating my database schema using the wonderful hbm2ddl tool, but I have one issue. I need to flatten some of the tables into views to aid searching the database, and hql would be overly complex a solution. I've created Entity objects pointed at these views, in order to fetch search results via hibernate. This all works fine, until hbm2ddl is used. In an empty database schema, hbm2ddl will create the database schema based on the jpa annotations, unfortunately, it will also create my views as tables. Is there some setting that I can tell hbm2ddl to run a view creation statement instead of create a table? In lieu of that, is there a way to tell hbm2ddl to skip table creation for an entity (exclude, or something)?
Thanks!
To my knowledge, and this is unfortunate, Hibernate doesn't support things like creating views instead of tables nor validating a schema containing views. See issues like HHH-1872, HHH-2018 or HHH-1329.

Convert dependencies to point to View instead of Table

I currently have a SQL Server 2008 database in which I am planning to separate out some tables to other databases. I want to be able to replace all references to the separated tables from the original database using views. Is there a better way (other than manually changing all FK and SProc references) to switch all the dependencies to reference the view instead of the table?
Usually the best course is to rename the tables and then name the view what the table used to be named.
You can try using sp_rename to change the name of the tables. I don't know if this will change your references also.

Resources