module 8: implementing views. overview introduction advantages definition modifying data through...
Post on 21-Dec-2015
219 views
TRANSCRIPT
Module 8: Implementing Views
Overview
Introduction
Advantages
Definition
Modifying Data Through Views
Optimizing Performance by Using Views
Introduction to Views
EmployeeViewEmployeeViewEmployeeViewEmployeeView
Lastname Lastname Firstname Firstname
Davolio Fuller Leverling
Davolio Fuller Leverling
Nancy Andrew Janet
Nancy Andrew Janet
EmployeesEmployeesEmployeesEmployees
EmployeeIDEmployeeID LastName LastName FirstnameFirstname TitleTitle
123
123
DavolioFullerLeverling
DavolioFullerLeverling
NancyAndrewJanet
NancyAndrewJanet
~~~~~~~~~
~~~~~~~~~
User’s ViewUser’s View
USE NorthwindGOCREATE VIEW dbo.EmployeeViewAS SELECT LastName, FirstnameFROM Employees
USE NorthwindGOCREATE VIEW dbo.EmployeeViewAS SELECT LastName, FirstnameFROM Employees
Advantages of Views
Focus the Data for Users Focus on important or appropriate data only Limit access to sensitive data (hide SSN from professors)
Mask Database Complexity Hide complex database design Simplify complex queries, including distributed queries to
heterogeneous data by embedding them in views Simplify Management of User Permissions
Different user access DB from different views Improve Performance
Reduce data access Organize Data for Export to Other Applications
Defining Views
Creating Views
Example: View of Joined Tables
Altering and Dropping Views
Avoiding Broken Ownership Chains
Locating View Definition Information
Hiding View Definitions
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
Example: View of Joined Tables
OrderIDOrderIDOrderIDOrderID
1066310827104271045110515
1066310827104271045110515
CustomerIDCustomerIDCustomerIDCustomerID
BONAP BONAP PICCO QUICKQUICK
BONAP BONAP PICCO QUICKQUICK
~~~ ~~~ ~~~ ~~~ ~~~
~~~ ~~~ ~~~ ~~~ ~~~
RequiredDateRequiredDateRequiredDateRequiredDate
1997-09-241998-01-261997-02-241997-03-051997-05-07
1997-09-241998-01-261997-02-241997-03-051997-05-07
ShippedDateShippedDateShippedDateShippedDate
1997-10-031998-02-061997-03-031997-03-121997-05-23
1997-10-031998-02-061997-03-031997-03-121997-05-23
Orders Customers
ShipStatusViewUSE 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
CustomerIDCustomerIDCustomerIDCustomerID
BONAPPICCOQUICK
BONAPPICCOQUICK
CompanyNameCompanyNameCompanyNameCompanyName
Bon app'Piccolo und mehrQUICK-Stop
Bon app'Piccolo und mehrQUICK-Stop
ContactNameContactNameContactNameContactName
Laurence LebihanGeorg PippsHorst Kloss
Laurence LebihanGeorg PippsHorst Kloss
OrderIDOrderIDOrderIDOrderID
102641027110280
102641027110280
1996-08-211996-08-291996-09-11
1996-08-211996-08-291996-09-11
ShippedDateShippedDateShippedDateShippedDate
1996-08-231996-08-301996-09-12
1996-08-231996-08-301996-09-12
ContactNameContactNameContactNameContactName
Laurence LebihanGeorg PippsHorst Kloss
Laurence LebihanGeorg PippsHorst Kloss
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
GRANT SELECT ON view2 TO azharGRANT SELECT ON view2 TO azhar
SELECT * FROM reif.view2SELECT * FROM reif.view2
Dependent Objects with Different Owners
Example:
Reif executes:
Azhar executes:
Avoiding Broken Ownership Chains
reif.view2
lewis.view1
lewis.table1
azhar does not have rights tolewis.view1 and lewis.table1
azhar does not have rights tolewis.view1 and lewis.table1
Locating View Definition Information
Locating View Definitions
Not available if view was created using WITH ENCRYPTION optionsysobjects: view namesysdepends: base object namessyscomments: view definitionsyscolumns: columns in the viewsp_helptext: provide text used in view definition
Locating View Dependenciessp_depends objectname
Lists objects upon which view depends Lists objects that depend on a view
Hiding View Definitions
Use the WITH ENCRYPTION Option
Do Not Delete Entries in the syscomments Table
USE NorthwindGOCREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTIONASSELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS SubtotalFROM [Order Details]GROUP BY OrderIDGO
USE NorthwindGOCREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTIONASSELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS SubtotalFROM [Order Details]GROUP BY OrderIDGO
Modifying Data Through Views
Can Affect
Only One Underlying Table Cannot Be Made to
Certain Columns (such as computed columns) Can Cause Errors
If They Affect Columns That Are Not Referenced in the View
Are Verified
If the WITH CHECK OPTION Has Been Specified
Optimizing Performance By Using Views
Performance Considerations
Using Indexed Views
Using Views to Partition Data
Performance Considerations
USE NorthwindGOCREATE VIEW dbo.TopSalesViewASSELECT *FROM dbo.TotalPurchaseViewWHERE Subtotal > 50000GO
USE NorthwindGOCREATE VIEW dbo.TopSalesViewASSELECT *FROM dbo.TotalPurchaseViewWHERE Subtotal > 50000GO
TotalPurchaseTotalPurchaseViewViewTotalPurchaseTotalPurchaseViewView
11 ~~ ~~ ~~ ~~
22 ~~ ~~ ~~ ~~
33 ~~ ~~ ~~ ~~
44 ~~ ~~ ~~ ~~
55 ~~ ~~ ~~ ~~
66 ~~ ~~ ~~ ~~
CustomersCustomersCustomersCustomers
11 ~~ ~~ ~~ nn
22 ~~ ~~ ~~ nn
33 ~~ ~~ ~~ yy
44 ~~ ~~ ~~ yy
55 ~~ ~~ ~~ nn
66 ~~ ~~ ~~ yy
OrdersOrdersOrdersOrders
11 ~~ ~~ ~~ nn
22 ~~ ~~ ~~ nn
33 ~~ ~~ ~~ yy
44 ~~ ~~ ~~ yy
55 ~~ ~~ ~~ nn
66 ~~ ~~ ~~ yy
Order DetailsOrder DetailsOrder DetailsOrder Details
11 ~~ ~~ ~~ ~~
22 ~~ ~~ ~~ ~~
33 ~~ ~~ ~~ ~~
44 ~~ ~~ ~~ ~~
55 ~~ ~~ ~~ ~~
66 ~~ ~~ ~~ ~~
SELECT *FROM dbo.TopSalesViewWHERE CompanyName = 'Ernst Handel'
SELECT *FROM dbo.TopSalesViewWHERE CompanyName = 'Ernst Handel'
TopSalesViewTopSalesViewTopSalesViewTopSalesView
~~ ~~ ~~
~~ ~~ ~~
~~ ~~ ~~
TopSalesView depends on TotalPurchaseView: Any
performance problems in the underlying view can be hidden.
Using Indexed Views
Indexed Views Store the Result Sets in the Database
Creating an Indexed View
Guidelines for Creating Indexed Views
Note: Query optimizer can determine using the access statistic.. let it do that.
Use explicitly when:
Performance gains outweigh maintenance costs
Underlying data is infrequently updated
Queries perform many joins and aggregations
Using Indexed Views
Restrictions on Creating Indexed Views
First index must be UNIQUE CLUSTERED
Create view with SCHEMABINDING option
View cannot reference other views
Using Views to Partition Data
You Can Use Views to Partition Data Across Multiple Servers or Instances of SQL Server
Use UNION to combine multiple tables across servers
How SQL Server Uses Views to Partition Data
Tables can be partitioned on separate servers (or processors) for parallel scanning
How Partitioned Views Improve Performance
Parallel processing
Recommended Practices
dbo Should Own All Viewsdbo Should Own All Views
Verify Object Dependencies Before You Drop ObjectsVerify Object Dependencies Before You Drop Objects
Carefully Evaluate Creating Views Based on ViewsCarefully Evaluate Creating Views Based on Views
Never Delete Entries in the syscomments TableNever Delete Entries in the syscomments Table
Use a Standard Naming ConventionUse a Standard Naming Convention
Review
Introduction to Views
Advantages of Views
Defining Views
Modifying Data Through Views
Optimizing Performance by Using Views