17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: Views<br />

320<br />

from the first index as a reference point. That said, nothing comes for free — there are some restrictions<br />

about when you can and can’t build indexes on views (I hope you’re ready for this one — it’s an awfully<br />

long list!):<br />

❑ The view must use the SCHEMABINDING option.<br />

❑ If it references any user-defined functions (more on these later), then these must also be schema<br />

bound.<br />

❑ The view must not reference any other views — just tables and UDFs.<br />

❑ All tables and UDFs referenced in the view must utilize a two-part (not even three-part and<br />

four-part names are allowed) naming convention (for example dbo.Customers, BillyBob<br />

.SomeUDF) and must also have the same owner as the view.<br />

❑ The view must be in the same database as all objects referenced by the view.<br />

❑ The ANSI_NULLS and QUOTED_IDENTIFIER options must have been turned on (using the SET<br />

command) at the time the view and all underlying tables were created.<br />

❑ Any functions referenced by the view must be deterministic.<br />

To create an example indexed view, let’s start by making a few alterations to the CustomerOrders_vw<br />

object that we created earlier in the chapter:<br />

ALTER VIEW CustomerOrders_vw<br />

WITH SCHEMABINDING<br />

AS<br />

SELECT sc.AccountNumber,<br />

soh.SalesOrderID,<br />

soh.OrderDate,<br />

sod.ProductID,<br />

pp.Name,<br />

sod.OrderQty,<br />

sod.UnitPrice,<br />

sod.UnitPriceDiscount * sod.UnitPrice * sod.OrderQty AS TotalDiscount,<br />

sod.LineTotal<br />

FROM Sales.Customer AS sc<br />

INNER JOIN Sales.SalesOrderHeader AS soh<br />

ON sc.CustomerID = soh.CustomerID<br />

INNER JOIN Sales.SalesOrderDetail AS sod<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

INNER JOIN Production.Product AS pp<br />

ON sod.ProductID = pp.ProductID;<br />

The big thing to notice here is that we had to make our view use the SCHEMABINDING option. This is<br />

really just the beginning though — we don’t have an indexed view as yet. Instead, what we have is a<br />

view that can be indexed. Before we actually build the index, let’s get a baseline of how queries against<br />

our view would currently be executed. Take a simple SELECT against the view:<br />

SELECT * FROM CustomerOrders_vw;<br />

Seems simple enough, but remember that there are four tables underlying this view. If we choose to display<br />

the estimated execution plan (one of the icons on the toolbar) as shown in Figure 10-5, we see a<br />

somewhat complex set of steps to execute our query.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!