module 7: implementing views. overview introducing views defining and using views using views to...
TRANSCRIPT
![Page 1: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/1.jpg)
Module 7: Implementing Views
![Page 2: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/2.jpg)
Overview
Introducing Views
Defining and Using Views
Using Views to Optimize Performance
![Page 3: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/3.jpg)
Lesson: Introducing Views
What Are Views?
Advantages of Views
![Page 4: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/4.jpg)
What Are Views?
EmployeeViewEmployeeView
Lastname Lastname Firstname Firstname
Davolio Fuller Leverling
Davolio Fuller Leverling
Nancy Andrew Janet
Nancy Andrew Janet
EmployeesEmployees
EmployeeIDEmployeeID LastName LastName FirstnameFirstname TitleTitle
123
123
DavolioFullerLeverling
DavolioFullerLeverling
NancyAndrewJanet
NancyAndrewJanet
~~~~~~~~~
~~~~~~~~~
USE NorthwindGOCREATE VIEW dbo.EmployeeViewAS SELECT LastName, FirstnameFROM Employees
USE NorthwindGOCREATE VIEW dbo.EmployeeViewAS SELECT LastName, FirstnameFROM Employees
User’s View
![Page 5: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/5.jpg)
Advantages of Views
Focus the Data for Users
Mask Database Complexity
Simplify Management of User Permissions
Improve Performance
Organize Data for Export to Other Applications
![Page 6: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/6.jpg)
Lesson: Defining and Using Views
The Process of Creating Views
Example: View of Joined Tables
The Process of Altering and Dropping Views
Broken Ownership Chains
Location of View Information
Encrypted View Definition
Restrictions for Modifying Data Through Views
![Page 7: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/7.jpg)
The Process of Creating Views
Creating a View
Restrictions on View Definitions
Cannot include ORDER BY clause Cannot include INTO keyword
CREATE VIEW dbo.OrderSubtotalsView (OrderID, Subtotal)ASSELECT OD.OrderID, SUM(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100)FROM [Order Details] ODGROUP BY OD.OrderIDGO
CREATE VIEW dbo.OrderSubtotalsView (OrderID, Subtotal)ASSELECT OD.OrderID, SUM(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100)FROM [Order Details] ODGROUP BY OD.OrderIDGO
![Page 8: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/8.jpg)
USE NorthwindGOCREATE VIEW dbo.ShipStatusViewASSELECT OrderID,ShippedDate,ContactNameFROM Customers c INNER JOIN Orders o ON c.CustomerID = O.CustomerIDWHERE RequiredDate < ShippedDate
USE NorthwindGOCREATE VIEW dbo.ShipStatusViewASSELECT OrderID,ShippedDate,ContactNameFROM Customers c INNER JOIN Orders o ON c.CustomerID = O.CustomerIDWHERE RequiredDate < ShippedDate
Example: View of Joined Tables
Orders Customers
ShipStatusView
OrderIDOrderID102641027110280
102641027110280
1996-08-211996-08-291996-09-11
1996-08-211996-08-291996-09-11
ShippedDateShippedDate1996-08-231996-08-301996-09-12
1996-08-231996-08-301996-09-12
ContactNameContactNameLaurence LebihanGeorg PippsHorst Kloss
Laurence LebihanGeorg PippsHorst Kloss
BONAPPICCOQUICK
BONAPPICCOQUICK
Laurence LebihanGeorg PippsHorst Kloss
Laurence LebihanGeorg PippsHorst Kloss
CustomerIDCustomerID ContactNameContactName
Bon app'Piccolo und mehrQUICK-Stop
Bon app'Piccolo und mehrQUICK-Stop
CompanyNameCompanyName
1066310827104271045110515
1066310827104271045110515
BONAP BONAP PICCO QUICKQUICK
BONAP BONAP PICCO QUICKQUICK
~~~ ~~~ ~~~ ~~~ ~~~
~~~ ~~~ ~~~ ~~~ ~~~
1997-10-031998-02-061997-03-031997-03-121997-05-23
1997-10-031998-02-061997-03-031997-03-121997-05-23
OrderIDOrderID CustomerIDCustomerID ShippedDateShippedDate
1997-09-241998-01-261997-02-241997-03-051997-05-07
1997-09-241998-01-261997-02-241997-03-051997-05-07
RequiredDateRequiredDate
![Page 9: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/9.jpg)
The Process of Altering and Dropping Views
Altering Views
Retains assigned permissions Causes new SELECT statement and options to replace
existing definition
Dropping Views
USE NorthwindGOALTER VIEW dbo.EmployeeViewAS SELECT LastName, FirstName, ExtensionFROM Employees
USE NorthwindGOALTER VIEW dbo.EmployeeViewAS SELECT LastName, FirstName, ExtensionFROM Employees
DROP VIEW dbo.ShipStatusViewDROP VIEW dbo.ShipStatusView
![Page 10: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/10.jpg)
Dependent Objects with Different Owners
Example:
Maria executes:
Pierre executes:
Broken Ownership Chains
GRANT SELECT ON view2 TO pierreGRANT SELECT ON view2 TO pierre
SELECT * FROM maria.view2SELECT * FROM maria.view2
maria.view2maria.view2
lucia.view1lucia.view1
lucia.table1lucia.table1
![Page 11: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/11.jpg)
Location of View Information
Locating View Definitions
Not available if view was created using WITH ENCRYPTION option
Locating View Dependencies
Lists objects upon which view depends
Lists objects that depend on a view
![Page 12: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/12.jpg)
Encrypted View Definition
Use the WITH ENCRYPTION Option
Do Not Delete Entries in the syscomments Table
CREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTIONASSELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS SubtotalFROM [Order Details]GROUP BY OrderIDGO
CREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTIONASSELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS SubtotalFROM [Order Details]GROUP BY OrderIDGO
![Page 13: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/13.jpg)
Restrictions for Modifying Data Through Views
Views Do Not Maintain a Separate Copy of Data
Modifications are done to the actual base table
Restrictions
Must not include aggregate functions or GROUP BY Cannot affect more than one underlying table Cannot be made to certain columns Are verified if the WITH CHECK OPTION has been
specified
![Page 14: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/14.jpg)
Lesson: Using Views to Optimize Performance
Potential Performance Problems
Indexed Views
Partition Data Using Views
Best Practices
![Page 15: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/15.jpg)
Potential Performance Problems
TotalPurchaseViewTotalPurchaseView
11 ~~ ~~ ~~ ~~
22 ~~ ~~ ~~ ~~
33 ~~ ~~ ~~ ~~
44 ~~ ~~ ~~ ~~
55 ~~ ~~ ~~ ~~
66 ~~ ~~ ~~ ~~
CustomersCustomers11 ~~ ~~ ~~ nn
22 ~~ ~~ ~~ nn
33 ~~ ~~ ~~ yy
44 ~~ ~~ ~~ yy
55 ~~ ~~ ~~ nn
66 ~~ ~~ ~~ yy
OrdersOrders11 ~~ ~~ ~~ nn
22 ~~ ~~ ~~ nn
33 ~~ ~~ ~~ yy
44 ~~ ~~ ~~ yy
55 ~~ ~~ ~~ nn
66 ~~ ~~ ~~ yy
Order DetailsOrder Details11 ~~ ~~ ~~ ~~
22 ~~ ~~ ~~ ~~
33 ~~ ~~ ~~ ~~
44 ~~ ~~ ~~ ~~
55 ~~ ~~ ~~ ~~
66 ~~ ~~ ~~ ~~ TopSalesViewTopSalesView
~~ ~~ ~~
~~ ~~ ~~
~~ ~~ ~~
USE NorthwindGOCREATE VIEW dbo.TopSalesViewAS SELECT * FROM dbo.TotalPurchaseView WHERE Subtotal > 50000GO
USE NorthwindGOCREATE VIEW dbo.TopSalesViewAS SELECT * FROM dbo.TotalPurchaseView WHERE Subtotal > 50000GO
SELECT *FROM dbo.TopSalesWHERE CompanyName = 'Ernst Handel'
SELECT *FROM dbo.TopSalesWHERE CompanyName = 'Ernst Handel'
![Page 16: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/16.jpg)
Indexed Views
Indexed Views Store the Result Sets in the Database
Creating an Indexed View
Guidelines for Creating Indexed Views
Use when:
Performance gains outweigh maintenance costs
Underlying data is infrequently updated
Queries perform many joins and aggregations
Restrictions on Creating Indexed Views
![Page 17: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/17.jpg)
Partition Data Using Views
You Can Use Views to Partition Data Across Multiple Servers or Instances of SQL Server
How SQL Server Uses Views to Partition Data
How Partitioned Views Improve Performance
![Page 18: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/18.jpg)
Best Practices
Use a Standard Naming ConventionUse a Standard Naming Convention
dbo Should Own All Viewsdbo Should Own All Views
Verify Object Dependencies Before You Drop ObjectsVerify Object Dependencies Before You Drop Objects
Never Delete Entries In The syscomments TableNever Delete Entries In The syscomments Table
Carefully Evaluate Creating Views Based On ViewsCarefully Evaluate Creating Views Based On Views
![Page 19: Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance](https://reader036.vdocuments.site/reader036/viewer/2022082414/5697bf8a1a28abf838c8ac69/html5/thumbnails/19.jpg)
Lab A: Implementing Views
Exercise 1: Creating and Testing Views
Exercise 2: Encrypting a View Definition
Exercise 3: Modifying Data Through Views
Exercise 4: Locating View Definitions