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

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

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 10: Views You can get the yellow popup by hovering your mouse over the top-left node of the execution plan. This is the top node of the execution plan, and looking at the Estimated Subtree Cost on this node shows us the estimated coast for the entire query (for me, it is 3.49114). Now that we’ve seen how complex the query is and seen the estimated cost, we’re ready to move on to creating the index. When we create the index, the first index created on the view must be both clustered and unique: CREATE UNIQUE CLUSTERED INDEX ivCustomerOrders ON CustomerOrders_vw(AccountNumber, SalesOrderID, ProductID); Once this command has executed, we have a clustered index on our view (Figure 10-6), and we’re ready to again check out our Estimated Execution Plan for our basic SELECT. Figure 10-6 Notice that this is a substantially more simplistic query. The estimated cost of execution has also dropped by over 50%. Does this mean our index is a good idea? Well, simply put — no. It means it might be. Much like any index, you need to keep in mind the maintenance cost of the index. How much is maintaining this index going to slow down INSERT, UPDATE, and DELETE statements against the underlying tables? Basically, it’s a balancing act, and each database and each index is a separate decision. That said, indexed views can be a powerful tool to have in your arsenal, so weigh them carefully! Summary 322 Views tend to be either the most over- or most under-used tools in most of the databases I’ve seen. Some people like to use them to abstract seemingly everything (often forgetting that they are adding another layer to the process when they do this). Others just seem to forget that views are even an option. Personally, like most things, I think you should use a view when it’s the right tool to use — not before, not after. Things to remember with views include: ❑ Stay away from building views based on views — instead, adapt the appropriate query information from the first view into your new view. ❑ Remember that a view using the WITH CHECK OPTION provides some flexibility that can’t be duplicated with a normal CHECK constraint.

❑ Encrypt views when you don’t want others to be able to see your source code — either for commercial products or general security reasons, but also remember to keep a copy of your unencrypted code; it can’t be retrieved after you’ve encrypted it. ❑ Using an ALTER VIEW completely replaces the existing view other than permissions. This means you must include the WITH ENCRYPTION and WITH CHECK OPTION clauses in the ALTER statement if you want encryption and restrictions to be in effect in the altered view. ❑ Use sp_helptext to display the supporting code for a view — avoid using the system tables. ❑ Minimize the user of views for production queries — they add additional overhead and hurt performance. Common uses for views include: ❑ Filtering rows ❑ Protecting sensitive data ❑ Reducing database complexity ❑ Abstracting multiple physical databases into one logical database Chapter 10: Views In our next chapter, we’ll take a look at batches and scripting. Batches and scripting will lead us right into stored procedures — the closest thing that SQL Server has to its own programs. Exercises 1. Add a view called HumanResources.Managers in the AdventureWorks2008 database that shows only employees that supervise other employees. 2. Change the view you just created to be encrypted. 3. Build an index against your new view based on the ManagerID and EmployeeID columns. 323

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!