Presentation is loading. Please wait.

Presentation is loading. Please wait.

Views Lesson 7.

Similar presentations


Presentation on theme: "Views Lesson 7."— Presentation transcript:

1 Views Lesson 7

2 Skills Matrix

3 View A view can be queried just like a table.
Microsoft describes a view as either a virtual table or a stored SELECT query, but you might prefer to think of it as similar to a filter. A view reveals data from one or more columns from one or more base tables, usually for limiting user data access to the totality of the base table. You use an indexed view which calculates aggregated data and for use when the cost of materializing the view proves less than the cost of aggregating data in a standard view.

4

5 View Yet another great reason to use views has to do with security. Permissions can be established on the view, which are independent of the base table(s) permissions.

6 View Not only can you use views to retrieve data, you can also modify data through them. Inserting records Updating records Deleting records

7 Modifying Data through a View
If you use a view to modify data, the modification can affect only one base table at a time. You can’t modify data in a view that uses aggregate functions. Aggregates are functions that return a summary value of some kind, such as SUM( ) or AVG( ). If you try to modify such a view, you’ll get an error.

8 Modifying Data through a Views
You saw earlier that views don’t necessarily present all the fields in a table. If you try to insert a record into a view that doesn’t show all fields, you could run into a problem. Some of the fields that aren’t shown in the view may not accept null values, but you can’t insert a value into those fields if they aren’t represented in the view. Because you can’t insert values in those fields, and they don’t allow null values, your insert will fail. You can still use such a view for UPDATEs and DELETEs, however.

9 Modifying Data through a Views
To overcome these limitations, you need to use INSTEAD OF triggers.

10 Working with Indexed Views
In reality, you’ll use queries that require a lot of calculation and data manipulation; such complex queries can take a toll on your system resources and thus slow it. Use indexed views to get around this bottleneck. Doing so does, however, slow data entry, as both the data itself and the materialized view must be updated.

11 Working with Indexed Views
Using indexes on complex views has its benefits, the first being performance. Every time a view is queried, SQL Server must materialize the view. Materialization is the process of performing all the JOINs and calculations necessary to return a resultset to the user. If the view is complex (requiring a large number of calculations and JOINs), indexing it can speed up access because the resultset will never again need to be materialized,

12 Query Optimizer The Query Optimizer is the component in SQL Server that analyzes your queries, compares them with available indexes, and decides which index returns a result set the fastest. Once you’ve indexed a view, the Query Optimizer considers this view in all future queries no matter what you’re querying. This means queries on other tables may benefit from the index you create on the view.

13 Disadvantage of Indexing a View
The disadvantage of indexing a view comes from the overhead it incurs on the system. First, indexed views take up disk space because they’re stored as separate objects in the database that look just like tables with a clustered index. Because clustered indexes store the actual data rather than just a pointer to the data in the base tables, they require extra disk space.

14 Disadvantage of Indexing a View
When you update the underlying table or tables of the view, however, the indexed view must be immediately updated to reflect the changes to the base table. This means if you create an indexed view on a table and then make changes to the records in that table, SQL Server automatically updates the view at the same time.

15 Partitioned View A partitioned view displays horizontally divided data from a set of member tables across one or more servers, making the data appear as if from one table. The partitioned view uses the UNION ALL clause to combine the results of SELECT statements on all the member tables in a single result set.

16 Partitioned View SQL Server distinguishes between:
Local partitioned views Distributed partitioned views In a local partitioned view, all participating tables and the view itself reside on the same instance of SQL Server. Use partitioned data locally by using partitioned tables rather than partitioned views. In a distributed partitioned view, at least one of the participating tables resides on a different (remote) server.

17 Summary You learned your view doesn’t actually contain any data—it’s just a filter through which you see the data in underlying tables. After that, you actually created a simple view based on a single table.

18 Summary Next you learned how to use a view to modify data. Don’t forget that modifying data through a view has a few caveats: You can’t modify more than one table at a time through a view. You can’t use views to modify aggregated data. If your view is based on a table that contains fields that don’t allow null values, yet your view doesn’t display those fields, then you won’t be able to insert new data. You can, however, update and delete data.

19 Summary Then you discovered you can index views. Doing so proves particularly useful if your view becomes complex; but be aware indexing can take a while to materialize. If you create an index on a view, SQL Server won’t need to populate the view every time someone queries it because the result set is stored in the database the same way a table with a clustered index is stored. Just remember that creating and maintaining indexed views has many caveats, so be absolutely sure you need them, and check the restrictions listed in Books Online.

20 Summary You can partition views in the same way you can partition tables and for the same reasons: store parts of your views on different spindles or even different servers.

21 Summary for Certification Examination
Understand views. It sounds basic but you should know what views are. Views do not actually contain data; they are used to display the data stored in one or more columns in one or more base tables in a different format with different security controls. Know how to index a view. Views can be indexed to speed up query times, but they have a large number of restrictions. Review the list of considerations for indexing a view and make sure you are familiar with them.

22 Summary for Certification Examination
Know how to make an updateable view. You can update the values in an underlying table used to create a view, but you need to consider a few issues. If you use a view to modify data, the modification can affect only one base table at a time. You can’t modify data in a view that uses aggregate functions such as Sum( ) or Avg( ). If you try to insert a record into a view that doesn’t show all fields, and if any of those missing fields do not accept null values, the insert fails. You can still use such a view for UPDATEs and DELETEs, though.

23 Summary for Certification Examination
Know the advantages of partitioned views and when to use them.


Download ppt "Views Lesson 7."

Similar presentations


Ads by Google