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 />

You can get the yellow popup by hovering your mouse over the top-left node of the execution plan. This<br />

is the top node of the execution plan, and looking at the Estimated Subtree Cost on this node shows us<br />

the estimated coast for the entire query (for me, it is 3.49114). Now that we’ve seen how complex the<br />

query is and seen the estimated cost, we’re ready to move on to creating the index.<br />

When we create the index, the first index created on the view must be both clustered and unique:<br />

CREATE UNIQUE CLUSTERED INDEX ivCustomerOrders<br />

ON CustomerOrders_vw(AccountNumber, SalesOrderID, ProductID);<br />

Once this command has executed, we have a clustered index on our view (Figure 10-6), and we’re ready<br />

to again check out our Estimated Execution Plan for our basic SELECT.<br />

Figure 10-6<br />

Notice that this is a substantially more simplistic query. The estimated cost of execution has also<br />

dropped by over 50%. Does this mean our index is a good idea? Well, simply put — no. It means it might<br />

be. Much like any index, you need to keep in mind the maintenance cost of the index. How much is<br />

maintaining this index going to slow down INSERT, UPDATE, and DELETE statements against the underlying<br />

tables? Basically, it’s a balancing act, and each database and each index is a separate decision. That<br />

said, indexed views can be a powerful tool to have in your arsenal, so weigh them carefully!<br />

Summary<br />

322<br />

Views tend to be either the most over- or most under-used tools in most of the databases I’ve seen. Some<br />

people like to use them to abstract seemingly everything (often forgetting that they are adding another<br />

layer to the process when they do this). Others just seem to forget that views are even an option. Personally,<br />

like most things, I think you should use a view when it’s the right tool to use — not before, not after.<br />

Things to remember with views include:<br />

❑ Stay away from building views based on views — instead, adapt the appropriate query information<br />

from the first view into your new view.<br />

❑ Remember that a view using the WITH CHECK OPTION provides some flexibility that can’t be<br />

duplicated with a normal CHECK constraint.

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

Saved successfully!

Ooh no, something went wrong!