Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 309 and 310: Navigating the Tree Figure 9-4 As I
- Page 311 and 312: there was no link between the data.
- Page 313 and 314: Root Non-Leaf Level Leaf Level Figu
- Page 315 and 316: The CREATE INDEX Statement The CREA
- Page 317 and 318: FILLFACTOR When SQL Server first cr
- Page 319 and 320: works only if tempdb is on a separa
- Page 321 and 322: Secondary XML Indexes Chapter 9: SQ
- Page 323 and 324: occur, and that one or more non-lea
- Page 325 and 326: isn’t room on the page, SQL Serve
- Page 327 and 328: more administrator oriented and usu
- Page 329 and 330: The Database Engine Tuning Advisor
- Page 331 and 332: The output is far more self-describ
- Page 333 and 334: We use a FILLFACTOR when we need to
- Page 335: Chapter 9: SQL Server Storage and I
- Page 338 and 339: Chapter 10: Views The preceding syn
- Page 340 and 341: Chapter 10: Views 302 columns to a
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 352 and 353: Chapter 10: Views 314 There are fou
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391 and 392: Out of the condition from inner con
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
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.