Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server indexed views

I am trying to create an indexed view in SQL Server and I was wondering If I needed to index the view columns.

I am asking this because the view is composed of a tables that already have the columns indexed.

So if TABLE1 has the column FOO already indexed as a non clustered index, do I have to add an index for the column FOO to the newly created view for SQL Server to use the index?

Or will SQL Server know to use the index in TABLE1 when searching the view?

The view looks like this

CREATE VIEW [dbo].[v_eventActivity] 
WITH SCHEMABINDING 
AS 
  SELECT ea.id, 
         e.eventID, 
         e.name, 
         ea.userID, 
         ea.activityTypeID, 
         ea.timeStamp, 
         ea.visitDuration 
  FROM   dbo.table1 e, 
         dbo.table2 ea 
  WHERE  e.eventID = ea.eventID 

I am going to be searching on all of those columns together.

As stated before, table1 and table2 all have already have those columns indexed.

like image 884
KDV Avatar asked Feb 08 '12 18:02

KDV


People also ask

Can views have indexes in SQL?

Indexes can only be created on views which have the same owner as the referenced table or tables. This is also called an intact ownership-chain between the view and the table(s). Typically, when table and view reside within the same schema, the same schema-owner applies to all objects within the schema.

How do I create an indexed view in SQL?

To create an indexed view, you use the following steps: First, create a view that uses the WITH SCHEMABINDING option which binds the view to the schema of the underlying tables. Second, create a unique clustered index on the view. This materializes the view.

What is indexed view explain in short?

What does Indexed view mean? This is also a simple view which has a unique clustered index defined on it. When a clustered index is created on a view, the result set is stored in the database just like a table with a clustered index.

What is the restriction on indexed view?

It can't contain COUNT, MIN, MAX, TOP, outer joins, or a few other keywords or elements. You can't modify the underlying tables and columns. The view is created with the WITH SCHEMABINDING option.


1 Answers

The view will simply utilize the table index unless the NOEXPAND hint is supplied (documentation here).

You can test this yourself as follows:

CREATE TABLE [test].[TestTable] (
    id INT IDENTITY PRIMARY KEY,
    foo INT
)

CREATE NONCLUSTERED INDEX ixFoo
ON [test].[TestTable] (foo)

CREATE VIEW [test].[TestTableView] WITH SCHEMABINDING
AS
    SELECT
        t.id,
        t.foo
    FROM [test].[TestTable] t
GO

CREATE UNIQUE CLUSTERED INDEX ixFooId
ON [test].[TestTableView] (id)

CREATE NONCLUSTERED INDEX ixFooView
ON [test].[TestTableView] (foo)

Here's the execution plan for three separate queries:

SELECT
    t.[id],
    t.[foo]
FROM [test].[TestTable] t
ORDER BY t.[foo]

The table query execution plan

SELECT
    v.[id],
    v.[foo]
FROM [test].[TestTableView] v
ORDER BY v.[foo]

The view with no hint

SELECT
    v.[id],
    v.[foo]
FROM [test].[TestTableView] v WITH (NOEXPAND)
ORDER BY v.[foo]

The view with the NOEXPAND hint

like image 98
LiquidPony Avatar answered Nov 15 '22 16:11

LiquidPony