cmpt 354 views and indexes spring 2012 instructor: hassan khosravi
TRANSCRIPT
![Page 1: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/1.jpg)
CMPT 354 Views and Indexes
Spring 2012
Instructor: Hassan Khosravi
![Page 2: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/2.jpg)
1.2
Three level vision of a database
![Page 3: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/3.jpg)
1.3
What are views Relations that are defined with a create table statement exist in
the physical layer and do not change unless explicitly told so
Virtual views do not physically exist, they are defined by expression over the tables. Can be queries (most of the time) as if they were tables.
Why use views? Hide some data from users Make some queries easier Modularity of database
When not specified exactly based on tables.
![Page 4: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/4.jpg)
1.4
Defining and using Views
Create View <view name> As <view definition> View definition is defined in SQL From now on we can use the view almost as if it is just a normal
table
View V (R1,…Rn )
query Q involving V Conceptually
V (R1,…Rn ) is used to evaluate Q
In reality
The evaluation is performed over R1,…Rn
![Page 5: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/5.jpg)
1.5
Example of views
![Page 6: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/6.jpg)
1.6
Views Conceptually
Reality
Query optimizer
Users do not need to worry about any of this
![Page 7: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/7.jpg)
1.7
Views on Views
![Page 8: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/8.jpg)
1.8
Views make life easy
![Page 9: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/9.jpg)
1.9
Deleting views
Drop View <view name> Dropping a view does not affect any tuples of the in the underlying
relation. Dropping a table would remove all of the tuples of the table and views
that use that table.
What would happen if CSaccept is dropped? Some DBMS would not allow it because it has dependencies Some would delete it and the be unable to answer queries on
CSberk
![Page 10: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/10.jpg)
1.10
Modifying views
Can views be modified? Doesn’t make sense to modify a view since it doesn’t physically
exist Should be possible since many users can only observe views and
do not have access to tables, so they should be able to alter tables using views.
Solution Modifications to the view rewritten to modify base tables.
![Page 11: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/11.jpg)
1.11
Example
R(A,B) which has (1, 2)
V is just the A attribute of V = ∏ A (R)
Insert (3) into A What would be insert into R(A,B)?
(3,1)? (3,2)? (3,null)?
R(N) V = avg(N)
Update
![Page 12: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/12.jpg)
1.12
Example
![Page 13: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/13.jpg)
1.13
Example
![Page 14: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/14.jpg)
1.14
Approaches to View Modification
Rewriting process specified explicitly by view creator (user) + Can handle all modification - No guarantee of correctness - More work
Restrict views and modifications so that translation to base tables can be done automatically, correctly, and unambiguously + No User intervention - Restrictions are significant
![Page 15: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/15.jpg)
1.15
Rewriting process specified explicitly by view creator
Instead of Triggers are mainly used
We can write a trigger to perform deletion on this view
![Page 16: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/16.jpg)
1.16
This will produce an error without having a trigger for it There is nothing stopping you from making a mistake in your trigger.
![Page 17: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/17.jpg)
1.17
Restricting modifications in Views Showing students that are either in CS or EE
Inserting new students
![Page 18: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/18.jpg)
1.18
Automatic View Modification
Restrict views and modifications so that translation to base tables can be done automatically, correctly, and unambiguously The view should be on one table and not join of tables Attributes that are not in the view can be null or have default
values Sub-queries must not refer to T You cannot have group by or aggregation
MYSQL supports automatic updates on views Postgress and SQLite do not support automatic updates on views and
triggers should be used
![Page 19: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/19.jpg)
1.19
Deletion Example
The deletion will be done in the apply table and would show in the CSaccept view
![Page 20: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/20.jpg)
1.20
Insertion Example
You should not be able to add to view tuples that do not belong to the view
![Page 21: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/21.jpg)
1.21
Updatable Views -- Aggregation
What would this deletion mean? The system disallows this deletion.
![Page 22: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/22.jpg)
1.22
Updatable views -- Sub-queries
This is disallowed since it is vague It would have surprising effects on the base table
![Page 23: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/23.jpg)
1.23
Updatable views-- Joins
What would insertion or deletion to this table mean? Updates the apply table but not the student table so the first row
would not match any more.
![Page 24: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/24.jpg)
1.24
Indexes in SQL
Primary mechanism to get improved performance on database Data structure stored in the database
Implementation issues
Full scan vs. immediate tuple location
![Page 25: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/25.jpg)
1.25
![Page 26: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/26.jpg)
1.26
Implementation of Indexes
Underlying data structure Balance Trees
A = v A < v A > v Logarithm time
Hash tables Only A = v If designed properly, constant time
![Page 27: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/27.jpg)
1.27
![Page 28: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/28.jpg)
1.28
![Page 29: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/29.jpg)
1.29
![Page 30: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/30.jpg)
1.30
Downside of Indexes
Extra space Local hard space
Over head in Index creation Index maintenance
![Page 31: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/31.jpg)
1.31
Calculating the best indexes to create
Typically a relation is stored over many disk blocks
Principle cost of a query or modification is often the number of disks that have to be brought into the main memory
Indexes store where tuples are stored, so we know which block to restore instead of full scan of the table
However indexes themselves have to be stored and modified Modification in the actual relation needs one page access to
modify the relation and one page access to modify the index Assuming we have history of the previous queries and modifications
how can we decide what to have indexes on?
![Page 32: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/32.jpg)
1.32
Example StarIn(movieTitle, movieYear, starName) Three database operations are performed
(Q1)Look for title and year of movies in which a given star appeared (p1) (Q2) Look for stars that appear in a given movie (p2) (I) Insert a new tuple into the starIn table (1-p1-p2)
StarIn occupies 10 pages On average a star has appeared in 3 movies On average a movie has 3 stars Movies of a star and stars of a movie are speared over all pages One disk access is needed to read a page in the index Insertion requires 1 access to find page to write to and 1 to actually write it
Action No Index Star Index Movie Index
Both Indexes
Q1 10 4 10 4
Q2 10 10 4 4
I 2 4 4 6
Average 2 + 8p1 +8p2 4 + 6p2 4 + 6p1 6 - 2p1 - 2p2
![Page 33: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/33.jpg)
1.33
Action No Index Star Index Movie Index
Both Indexes
Q1 10 4 10 4
Q2 10 10 4 4
I 2 4 4 6
Average 2 + 8p1 +8p2 4 + 6p2 4 + 6p1 6 - 2p1 - 2p2
P1=0.33P2=0.33
7.28 6 6 4.67
P1=0.1 p2=0.1
3.6 4.6 4.6 5.6
P1=0.1 p2=0.5
6.8 7 4.6 4.8
![Page 34: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/34.jpg)
1.34
Automatic Selection of Indexes to create
![Page 35: CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi](https://reader034.vdocument.in/reader034/viewer/2022052603/56649c785503460f9492d95a/html5/thumbnails/35.jpg)
1.35
Materialized views
Materialized views
Periodic Maintenance of Materialized views Common for databases to serve two purposes
Record current inventory Analysts to study patterns
Analyst’s queries involve many aggregation and joins Not as sensitive to modifications They are updated (reconstructed) periodically
Once a day or week