views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/fall09/lectures/lecture-08-views.pdf · views...

39
Views Materialised Views Views T. M. Murali September 21, 2009 T. M. Murali September 21, 2009 CS 4604: Views

Upload: others

Post on 24-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Views

T. M. Murali

September 21, 2009

T. M. Murali September 21, 2009 CS 4604: Views

Page 2: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

What are Views?

I A view is a relation that does not exist physically.

I A view is defined by a query over other relations (tables and/or views).

I Just like a table, a view can be queried: the query processor replacesthe view by its definition.

I Just like a table, a view can be used in other queries.

I Unlike a table, a view cannot be updated unless it satisfies certainconditions.

T. M. Murali September 21, 2009 CS 4604: Views

Page 3: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Defining a View

I CREATE VIEW ViewName AS Query;

I Suppose we want to perform a set of queries on those students whohave taken courses in the computer science departments.

I Let us create a view to store the PIDs of these students and thenumbers of the CS courses they are taking.

CREATE VIEW CSStudents

(PID, CSCourseNumber)

AS

SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

I Can name attributes in the view’s schema.

T. M. Murali September 21, 2009 CS 4604: Views

Page 4: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Defining a View

I CREATE VIEW ViewName AS Query;

I Suppose we want to perform a set of queries on those students whohave taken courses in the computer science departments.

I Let us create a view to store the PIDs of these students and thenumbers of the CS courses they are taking.

CREATE VIEW CSStudents

(PID, CSCourseNumber)

AS

SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

I Can name attributes in the view’s schema.

T. M. Murali September 21, 2009 CS 4604: Views

Page 5: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Defining a View

I CREATE VIEW ViewName AS Query;

I Suppose we want to perform a set of queries on those students whohave taken courses in the computer science departments.

I Let us create a view to store the PIDs of these students and thenumbers of the CS courses they are taking.

CREATE VIEW CSStudents

(PID, CSCourseNumber)

AS

SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

I Can name attributes in the view’s schema.

T. M. Murali September 21, 2009 CS 4604: Views

Page 6: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Defining a View

I CREATE VIEW ViewName AS Query;

I Suppose we want to perform a set of queries on those students whohave taken courses in the computer science departments.

I Let us create a view to store the PIDs of these students and thenumbers of the CS courses they are taking.

CREATE VIEW CSStudents(PID, CSCourseNumber) AS

SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

I Can name attributes in the view’s schema.

T. M. Murali September 21, 2009 CS 4604: Views

Page 7: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Querying Views

I We can query views just as we can query tables.

I How many students took a CS course, i.e., what was the totalenrollment in CS courses?

SELECT COUNT (PID)

FROM CSStudents;

I Interpreting this query: replace view by its definition.

SELECT COUNT (StudentPID)

FROM(SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 8: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Querying Views

I We can query views just as we can query tables.

I How many students took a CS course, i.e., what was the totalenrollment in CS courses?

SELECT COUNT (PID)

FROM CSStudents;

I Interpreting this query: replace view by its definition.

SELECT COUNT (StudentPID)

FROM(SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 9: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Querying Views

I We can query views just as we can query tables.

I How many students took a CS course, i.e., what was the totalenrollment in CS courses?

SELECT COUNT (PID)

FROM CSStudents;

I Interpreting this query: replace view by its definition.

SELECT COUNT (StudentPID)

FROM(SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 10: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Querying Views

I We can query views just as we can query tables.

I How many students took a CS course, i.e., what was the totalenrollment in CS courses?

SELECT COUNT (PID)

FROM CSStudents;

I Interpreting this query: replace view by its definition.

SELECT COUNT (StudentPID)

FROM(SELECT StudentPID, NumberFROM TakeWHERE (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 11: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Modifying Views

I What does it mean to modify a view?

I How is tuple deletion from a view executed?

I Can we insert a tuple into a view? Where will it be inserted, since aview does not physically exist?

I Can we insert tuples into any view?

SQL includes rules that specifywhich views are updatable.

T. M. Murali September 21, 2009 CS 4604: Views

Page 12: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Modifying Views

I What does it mean to modify a view?

I How is tuple deletion from a view executed?

I Can we insert a tuple into a view? Where will it be inserted, since aview does not physically exist?

I Can we insert tuples into any view? SQL includes rules that specifywhich views are updatable.

T. M. Murali September 21, 2009 CS 4604: Views

Page 13: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Views

I DROP VIEW CSStudents;

I Only view definition deleted.

I Underlying tables are not affected.

T. M. Murali September 21, 2009 CS 4604: Views

Page 14: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Tuples from CSStudents

I Delete tuples for students taking ’CS 4604’.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604);

I Deletion is executed as if were executing

DELETE FROM Take

WHERE (Number = 4604);

I Incorrect: non-CS tuples where (Number = 4604) will be deleted.

I Correct deletion adds condition in the WHERE clause of the viewdefinition to the WHERE clause of the DELETE FROM statement.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604) AND (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 15: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Tuples from CSStudents

I Delete tuples for students taking ’CS 4604’.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604);

I Deletion is executed as if were executing

DELETE FROM Take

WHERE (Number = 4604);

I Incorrect: non-CS tuples where (Number = 4604) will be deleted.

I Correct deletion adds condition in the WHERE clause of the viewdefinition to the WHERE clause of the DELETE FROM statement.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604) AND (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 16: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Tuples from CSStudents

I Delete tuples for students taking ’CS 4604’.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604);

I Deletion is executed as if were executing

DELETE FROM Take

WHERE (Number = 4604);

I Incorrect: non-CS tuples where (Number = 4604) will be deleted.

I Correct deletion adds condition in the WHERE clause of the viewdefinition to the WHERE clause of the DELETE FROM statement.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604) AND (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 17: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Tuples from CSStudents

I Delete tuples for students taking ’CS 4604’.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604);

I Deletion is executed as if were executing

DELETE FROM Take

WHERE (Number = 4604);

I Incorrect: non-CS tuples where (Number = 4604) will be deleted.

I Correct deletion adds condition in the WHERE clause of the viewdefinition to the WHERE clause of the DELETE FROM statement.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604) AND (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 18: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Deleting Tuples from CSStudents

I Delete tuples for students taking ’CS 4604’.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604);

I Deletion is executed as if were executing

DELETE FROM Take

WHERE (Number = 4604);

I Incorrect: non-CS tuples where (Number = 4604) will be deleted.

I Correct deletion adds condition in the WHERE clause of the viewdefinition to the WHERE clause of the DELETE FROM statement.

DELETE FROM CSStudents

WHERE (CSCourseNumber = 4604) AND (DeptName = ’CS’);

T. M. Murali September 21, 2009 CS 4604: Views

Page 19: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Inserting a Tuple into CSStudents

INSERT INTO CSStudents

VALUES (’123-45-6789’, 4604);

I Insertion is executed as if we were executing

INSERT INTO Take(StudentPID, Number)

VALUES (’123-45-6789’, 4604);I Two problems:

1. Key for Take is (PID, DeptName, Number). What value does thenew tuple get for DeptName?

2. Even if we had not declared key for Take, new tuple gets NULL valuefor DeptName ⇒ it is not a part of the view after insertion!

T. M. Murali September 21, 2009 CS 4604: Views

Page 20: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Inserting a Tuple into CSStudents

INSERT INTO CSStudents

VALUES (’123-45-6789’, 4604);

I Insertion is executed as if we were executing

INSERT INTO Take(StudentPID, Number)

VALUES (’123-45-6789’, 4604);

I Two problems:

1. Key for Take is (PID, DeptName, Number). What value does thenew tuple get for DeptName?

2. Even if we had not declared key for Take, new tuple gets NULL valuefor DeptName ⇒ it is not a part of the view after insertion!

T. M. Murali September 21, 2009 CS 4604: Views

Page 21: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Inserting a Tuple into CSStudents

INSERT INTO CSStudents

VALUES (’123-45-6789’, 4604);

I Insertion is executed as if we were executing

INSERT INTO Take(StudentPID, Number)

VALUES (’123-45-6789’, 4604);I Two problems:

1. Key for Take is (PID, DeptName, Number). What value does thenew tuple get for DeptName?

2. Even if we had not declared key for Take, new tuple gets NULL valuefor DeptName ⇒ it is not a part of the view after insertion!

T. M. Murali September 21, 2009 CS 4604: Views

Page 22: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Correcting the Definition of CSStudents

I Include DeptName in the view’s schema.

CREATE VIEW CSStudents ASSELECT StudentPID, DeptName, NumberFROM TakeWHERE (DeptName = ’CS’);

I Write INSERT INTO as follows:

INSERT INTO CSStudents

VALUES (’123-45-6789’, ’CS’, 4604);

T. M. Murali September 21, 2009 CS 4604: Views

Page 23: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Correcting the Definition of CSStudents

I Include DeptName in the view’s schema.

CREATE VIEW CSStudents ASSELECT StudentPID, DeptName, NumberFROM TakeWHERE (DeptName = ’CS’);

I Write INSERT INTO as follows:

INSERT INTO CSStudents

VALUES (’123-45-6789’, ’CS’, 4604);

T. M. Murali September 21, 2009 CS 4604: Views

Page 24: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Updatable Views

I SQL has complex rules for when a view can be modified.

I Defined by selecting some attributes from one relation R.

I R may itself be an updatable view.

I Use SELECT and not SELECT DISTINCT.

I WHERE clause must not involve R in a sub-query.

I FROM clause can contain only one occurrence of R and must notcontain any other relation.

I SELECT clause must contain enough attributes so that for every tupleinserted into the view, other attributes can get NULL values or defaultvalues.

I An attribute that is declared NOT NULL and has no default must bementioned in the SELECT clause.

T. M. Murali September 21, 2009 CS 4604: Views

Page 25: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Materializing Views

I Some views may be frequently used in queries.

I It may be efficient to materialise such a view, i.e., maintain its valueat all times as a physical table.

I Cost of materialising views:

I Recomputing it when the underlying tables change.I Materialised view may be much larger than original relations, e.g., in

the case of joins.

T. M. Murali September 21, 2009 CS 4604: Views

Page 26: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Materializing Views

I Some views may be frequently used in queries.

I It may be efficient to materialise such a view, i.e., maintain its valueat all times as a physical table.

I Cost of materialising views:I Recomputing it when the underlying tables change.I Materialised view may be much larger than original relations, e.g., in

the case of joins.

T. M. Murali September 21, 2009 CS 4604: Views

Page 27: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Maintaining Materialised Views

CREATE MATERIALIZED VIEW CSStudents AS

SELECT StudentPID, DeptName, Number

FROM Take

WHERE (DeptName = ’CS’);

I When does CSStudents need to be maintained?

Insertion/deletion/update of Take.I How expensive is the modification of CSStudents?

I Insertion of tuple: Insert tuple into CSStudents only if new tuple hasDeptName = ’CS’.

I Deletion of tuple: same reasoning as insertion.I Update of tuple: View as delete followed by insert.

I Key idea is that many materialised views can be updatedincrementally.

I Incremental maintenance of a view that involves a join: read Chapter8.5.1 of the textbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 28: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Maintaining Materialised Views

CREATE MATERIALIZED VIEW CSStudents AS

SELECT StudentPID, DeptName, Number

FROM Take

WHERE (DeptName = ’CS’);

I When does CSStudents need to be maintained?Insertion/deletion/update of Take.

I How expensive is the modification of CSStudents?

I Insertion of tuple: Insert tuple into CSStudents only if new tuple hasDeptName = ’CS’.

I Deletion of tuple: same reasoning as insertion.I Update of tuple: View as delete followed by insert.

I Key idea is that many materialised views can be updatedincrementally.

I Incremental maintenance of a view that involves a join: read Chapter8.5.1 of the textbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 29: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Maintaining Materialised Views

CREATE MATERIALIZED VIEW CSStudents AS

SELECT StudentPID, DeptName, Number

FROM Take

WHERE (DeptName = ’CS’);

I When does CSStudents need to be maintained?Insertion/deletion/update of Take.

I How expensive is the modification of CSStudents?I Insertion of tuple: Insert tuple into CSStudents only if new tuple has

DeptName = ’CS’.

I Deletion of tuple: same reasoning as insertion.I Update of tuple: View as delete followed by insert.

I Key idea is that many materialised views can be updatedincrementally.

I Incremental maintenance of a view that involves a join: read Chapter8.5.1 of the textbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 30: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Maintaining Materialised Views

CREATE MATERIALIZED VIEW CSStudents AS

SELECT StudentPID, DeptName, Number

FROM Take

WHERE (DeptName = ’CS’);

I When does CSStudents need to be maintained?Insertion/deletion/update of Take.

I How expensive is the modification of CSStudents?I Insertion of tuple: Insert tuple into CSStudents only if new tuple has

DeptName = ’CS’.I Deletion of tuple: same reasoning as insertion.I Update of tuple: View as delete followed by insert.

I Key idea is that many materialised views can be updatedincrementally.

I Incremental maintenance of a view that involves a join: read Chapter8.5.1 of the textbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 31: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Maintaining Materialised Views

CREATE MATERIALIZED VIEW CSStudents AS

SELECT StudentPID, DeptName, Number

FROM Take

WHERE (DeptName = ’CS’);

I When does CSStudents need to be maintained?Insertion/deletion/update of Take.

I How expensive is the modification of CSStudents?I Insertion of tuple: Insert tuple into CSStudents only if new tuple has

DeptName = ’CS’.I Deletion of tuple: same reasoning as insertion.I Update of tuple: View as delete followed by insert.

I Key idea is that many materialised views can be updatedincrementally.

I Incremental maintenance of a view that involves a join: read Chapter8.5.1 of the textbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 32: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Periodic Maintenance

I Consider a database that records the inventory of a department store.

I A view may aggregate buyer patterns for further analysis.

I A (materialised) view is a good candidate for storing these aggregateddata.

I Since such analysis is performed periodically, materialised view neednot be maintained after each customer transaction. It can be updateat regular intervals, for instance once every day.

I Automatic creation of materialised views: Read Chapter 8.5.4 of thetextbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 33: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Periodic Maintenance

I Consider a database that records the inventory of a department store.

I A view may aggregate buyer patterns for further analysis.

I A (materialised) view is a good candidate for storing these aggregateddata.

I Since such analysis is performed periodically, materialised view neednot be maintained after each customer transaction. It can be updateat regular intervals, for instance once every day.

I Automatic creation of materialised views: Read Chapter 8.5.4 of thetextbook.

T. M. Murali September 21, 2009 CS 4604: Views

Page 34: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rewriting Queries Using Materialised Views

I In practice, views are materialised because they are helpful to answercommon queries.

I Can we rewrite a query to use a materialised view rather than theoriginal relations?

I Find names and addresses of students taking CS courses?

SELECT Name, AddressFROM Students, TakeWHERE (Students.PID = Take.StudentPID) AND (DeptName =’CS’);

I Can we rewrite the query using the view CSStudents?

SELECT Name, AddressFROM Students, CSStudentsWHERE (Students.PID = CSStudents.StudentPID);

T. M. Murali September 21, 2009 CS 4604: Views

Page 35: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rewriting Queries Using Materialised Views

I In practice, views are materialised because they are helpful to answercommon queries.

I Can we rewrite a query to use a materialised view rather than theoriginal relations?

I Find names and addresses of students taking CS courses?

SELECT Name, AddressFROM Students, TakeWHERE (Students.PID = Take.StudentPID) AND (DeptName =’CS’);

I Can we rewrite the query using the view CSStudents?

SELECT Name, AddressFROM Students, CSStudentsWHERE (Students.PID = CSStudents.StudentPID);

T. M. Murali September 21, 2009 CS 4604: Views

Page 36: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rewriting Queries Using Materialised Views

I In practice, views are materialised because they are helpful to answercommon queries.

I Can we rewrite a query to use a materialised view rather than theoriginal relations?

I Find names and addresses of students taking CS courses?

SELECT Name, AddressFROM Students, TakeWHERE (Students.PID = Take.StudentPID) AND (DeptName =’CS’);

I Can we rewrite the query using the view CSStudents?

SELECT Name, AddressFROM Students, CSStudentsWHERE (Students.PID = CSStudents.StudentPID);

T. M. Murali September 21, 2009 CS 4604: Views

Page 37: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rewriting Queries Using Materialised Views

I In practice, views are materialised because they are helpful to answercommon queries.

I Can we rewrite a query to use a materialised view rather than theoriginal relations?

I Find names and addresses of students taking CS courses?

SELECT Name, AddressFROM Students, TakeWHERE (Students.PID = Take.StudentPID) AND (DeptName =’CS’);

I Can we rewrite the query using the view CSStudents?

SELECT Name, AddressFROM Students, CSStudentsWHERE (Students.PID = CSStudents.StudentPID);

T. M. Murali September 21, 2009 CS 4604: Views

Page 38: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rules for Rewriting Queries

I Complete sets of rules is very complex. We discuss a simple rule torewrite a query using a materialised view.

View V :SELECT LV

FROM RV

WHERE CV .

Query Q:SELECT LQ

FROM RQ

WHERE CQ .

New query Q ′:SELECT LQ

FROM V , RQ − Rv

WHERE C .

I We can replace Q by the new query Q ′ ifI RV ⊆ RQ .I CQ ≡ CV AND C , for some condition C , which may be empty.I If C is not empty, then attributes of relations in RV that C mentions

are also in LV .I Attributes in LQ that come from relations in RV are also in the list of

attributes LV .

T. M. Murali September 21, 2009 CS 4604: Views

Page 39: Views - courses.cs.vt.educourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-08-views.pdf · Views Materialised Views What are Views? I A view is a relation that does not exist physically

Views Materialised Views

Rules for Rewriting Queries

I Complete sets of rules is very complex. We discuss a simple rule torewrite a query using a materialised view.

View V :SELECT LV

FROM RV

WHERE CV .

Query Q:SELECT LQ

FROM RQ

WHERE CQ .

New query Q ′:SELECT LQ

FROM V , RQ − Rv

WHERE C .

I We can replace Q by the new query Q ′ ifI RV ⊆ RQ .I CQ ≡ CV AND C , for some condition C , which may be empty.I If C is not empty, then attributes of relations in RV that C mentions

are also in LV .I Attributes in LQ that come from relations in RV are also in the list of

attributes LV .

T. M. Murali September 21, 2009 CS 4604: Views