SQL Server indexed view matching of views with joins not working - sql-server

Does anyone have experience of when SQL Server 2008 R2 is able to automatically match indexed view (also known as materialized views) that contain joins to a query?
For example the view
select dbo.Orders.Date, dbo.OrderDetails.ProductID
from dbo.OrderDetails
join dbo.Orders on dbo.OrderDetails.OrderID = dbo.Orders.ID
Cannot automatically be matched to the same exact query. When I select directly from this view with (noexpand) I actually get a much faster query plan that does a scan on the clustered index of the indexed view. Can I get SQL Server to do this matching automatically? I have quite a few queries and views and I do not want to reference the indexed view manually each time because I am using an OR mapper.
I am on enterprise edition of SQL Server 2008 R2.
Edit: I found the solution. SQL Server 2008 R2 does not match indexed views with more than 2 joins automatically. Probably it would slow down the optimization process too much.
Edit 2: Reviewing this 2 years after the question was created by me, I don't think my conclusion was correct. Materialized view matching is a very fragile process with no clear rules that I could find over the years.
Certainly, the following play a role:
Number of joins
Presence of a predicate
Join order, both in the view and in the query

I'm a little fuzzy on exactly what your question is; but I think this will give you what you want:
http://msdn.microsoft.com/en-us/library/ms181151.aspx
There are a lot of strange, arbitrary-seeming conditions that limit when SQL Server will use a view index in a query. This page documents them for SQL Server 2008.

Related

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.

In SQL Server 2005, how to get tables in other databases on which view depends?

In SQL Server 2008, for a view in a given database, I can get the tables in other databases on which the view depends by executing:
select distinct referenced_database_name, referenced_schema_name, referenced_entity_name
from sys.dm_sql_referenced_entities ('dbo.NameOfView', 'OBJECT')
where referenced_database_name is not null
However, sys.dm_sql_referenced_entities is not available in SQL Server 2005. Is there a way to obtain, from the metadata, a list of tables in other databases on which a view depends?
From Aaron Bertrand's vaguely related blog entry:
The system table sysdepends does not maintain dependency information
for any objects located outside of the local database. This means
that if you want to check the validity of objects with three- or
four-part names in SQL Server 2005 or earlier, you are going to have
to manually parse the definitions of all of your objects.
He goes on to recommend a parser from here - but it's still a nasty job on 2005 I'm afraid.
You can use sys.sql_dependencies.
It will give you per column dependencies though so you'd have to unique the major id to get the table.
select distinct object_name(referenced_major_id)
from sys.sql_dependencies
where object_id = object_id('viewName')
As far as getting the results from other databases, that will be tricky as I don't know how the data is stored cross-database and can't test it as I don't have a SQL Server 2005 install readily available to see. In SQL Server 2008+, the new view is sys.sql_expression_dependencies which is what drives sys.dm_sql_referenced_entities that you mentioned.
I'll do more digging to see if I can figure any more out.
I found something that might be of interest to you.
Try sp_depends. BOL states that you can derive the dependencies of the view using this sproc in SQL Server 2005. BOL Link
Yes Zhenny, we can achieve this from sqlserver2008+ edition. onwards using below query to list all the objects that are used from the other databases also.
select referenced_entity_name,referenced_database_name
from sys.sql_expression_dependencies
where referencing_id = object_id('SCHEMA.OBJECTNAME')

On Views and Indexes (SQL Server 2008 R2)

I have created a series of views that operate on a fairly large table. The table has been properly indexed and the performance is entirely sufficient. I would like to add an additional layer of views to the DB that are effectively views of views. What I want to know is whether SQL Server is smart enough to use the indexes on the underlying table when it builds the query, or whether I need to index the first tier of views somehow?
Yes it is. A view (not persisted) is nothing more than a query stored on your DB. If you have a view called view1 as select * from table1 and do select * from view1, SQL Server will automatically transform that to select * from table1 and then build a execution plan
Same rule applies if you increase the chain of objects adding more views.

SQL 2008 R2 Standard - Index View Support

Many years ago (almost ten) I thought Index View is more like an enterprise edition (SQL 2000) only feature but I was wrong and Index view were indtroducted in SQL 2000 to satifsy competivive product's support for materialize view.
However, You can still create index view and physically materialize that view in all the edition of SQL 2000/2005 and query will use that index on a view if you specify NOEXPAND query hint (which is not needed in enterprise/developer editon)
Here is the white paper on Index View (that confirm what I said earlier)
http://msdn.microsoft.com/en-us/library/dd171921.aspx
However, it appears to me that starting with SQL 2008/R2 index view indeed is an enterprise edition feature.
I did compare feature by different edition
http://msdn.microsoft.com/en-us/library/cc645993.aspx
so in SQL 2008 R2 Standard edition you are able to create index view but looks like NOEXPAND hint will not work so it is almost useless...
Is it possible to create index view and use that index (instead of index on base table) in SQL Server 2008 R2 (standard or express edition) using noexpand hint?
This other article on SQLServerCentral seems to suggest that yes, NOEXPAND continues to work perfectly on every edition of SQL Server from 2005 to 2012. I'll quote:
"Then NOEXPAND hint still works in non-Enterprise editions of SQL Server. I think there has been some confusion as to what this hint actually does. It forces the query optimizer to rely on the view, rather than the underlying table, for optimization. It does not force the query optimizer to use any given index on a view.
The sites I found online stating that NOEXPAND does not work did not include any testing methodology, so I can't say why it did not work for them.I can say that it can work in situations where the query optimizer decides the index is useful."

Is there a dynamic management view in SQL Server 2005 that pertains to column statistics?

The dynamic management views of SQL Server 2005 can give usage information about table indexes. Is there a similar method for getting usage information about column statistics? In specific, I'm curious if some of the older column statistics I've created are still being used. If not, I'd like to delete them.
no there isn't. there are however sys.stats_columns and sys.stats catalog views

Resources