Top Banner
View 1
31

View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

View

1

Page 2: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 2

View

• Three-level vision of DB

users Virtual DB views

DB Designer Logical DB relations

DBA

DBA Physical DB stored info

Page 3: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 3

What’s a View?

• An expression that defines a table without physically creating it.

• Base table vs. view– Base table: physically stored, persistent– (Virtual )View: not physically stored, defined

on base tables.

• Query and even modify views like tables

Page 4: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 4

Def. in SQL:2003

• SQL-data consists entirely of table variables, called base tables.

• An operation that references zero or more base tables and returns a table is called a query.

• The result of a query is called a derived table.• A view is a named query, which can be invoked by

use of this name. The result of such an invocation is called a viewed table.

Page 5: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 5

Why Views?

• To hide some data from the users

• To make certain queries easier or more natural to express

• For modularity

Page 6: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 6

View Declaration

CREATE VIEW viewname(A1,...,An)

AS query;

• ExampleCREATE VIEW CS_S AS

SELECT sno,name,age

FROM Student

WHERE dept = ‘CS’;

• Only definition of views are stored.

Page 7: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 7

Querying Views

• Query a view as if it were a stored table.

• Queries on views are translated into queries on base tables.– Simplest: replace view by its definition (a subquery)

• ExampleSELECT name FROM CS_S

WHERE age = 18;

SELECT name FROM (SELECT sno,name,age FROM Student

WHERE dept = ‘CS’) css

WHERE age = 18;

– Q: Two consecutive queries on CS_S always return the same result?

Page 8: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 8

Example: Join View

• Creation of view SN_CCREATE VIEW SN_C AS

SELECT name,cno FROM S,SC

WHERE S.sno = SC.sno;

• Query on SN_CSELECT name FROM SN_C

WHERE cno = ‘C2’;– no join?

Page 9: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 9

Example: Renaming Attributes

1. CREATE VIEW CS_S(id,sn,sa) AS SELECT sno, name, age

FROM Student

WHERE dept = ‘CS’;

2. CREATE VIEW S_G(sno,avggrade) AS SELECT sno, AVG(grade)

FROM SC

GROUP BY sno;

Page 10: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 10

Example: Views on Views

CREATE VIEW CS_S_20 AS

SELECT sno,name

FROM CS_S

WHERE age=20;

Page 11: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 11

Modifying Views

• Updatable view: it’s possible to translate the modification of the view into an equivalent modification on a base table.– Complex rules in SQL standard

• Example: S_G is not an updatable view.INSERT INTO S_G

VALUES (‘007’, 80);– Causes what tuple to be inserted into SC?

Page 12: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Updatable Views

• FROM only one relation R ( which may itself be an updatable view)

• WHERE must not involve R in a subquery

• SELECT (not SELECT DISTINCT) some attributes– Enough for insertion into the underlying R

Lu Chaojun, SJTU 12

Page 13: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Example

• CS_S is updatable, but has anomaly:INSERT INTO CS_S

VALUES (‘007’,’james’,18);– causes (‘007’,’james’,18,NULL) to be inserted

into Student, which can’t be restored into CS_S– Attributes not enough

Lu Chaojun, SJTU 13

Page 14: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 14

Drop Views

DROP VIEW viewname;

• Definition of view is dropped.

• Underlying base tables are not affected.

• DROP TABLE will affect views defined on it.

Page 15: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Instead-Of Triggers on Views

• Actions of trigger is done instead of the event (modification on a view) itself.

• The programmer can force whatever interpretation of a view modification is desired.

Lu Chaojun, SJTU 15

Page 16: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Example

CREATE TRIGGER insertCSS

INSTEAD OF INSERT ON CS_S

REFERENCING NEW ROW AS nr

FOR EACH ROW

INSERT INTO Student

VALUES(nr.sno,nr.name,nr.age,’CS’);

Lu Chaojun, SJTU 16

Page 17: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Materialized Views

• Views are logical descriptions of relations that are constructed from base tables by executing a query.– Querying a view = executing the query

• If a view is used frequently enough, it may be efficient to materialize it.CREATE MATERIALIZED VIEW CS_S

AS SELECT sno,name,age FROM S WHERE dept=‘CS’;

Lu Chaojun, SJTU 17

Page 18: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Maintaining a MV

• Problem: each time a base table changes, the materialized view may change.– Cannot afford to recompute the view with each change.

– Incremental maintainence:Base table R Materialized view V

R V

Example: S vs CS_S

insert into S insert into CS_S

values(a,b,c,’CS’) values(a,b,c)

Lu Chaojun, SJTU 18

Page 19: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Periodic Maintainence of MV

• Data warehouse: create MV (of aggregations), but not to keep it up-to-date.– Frequent modification: expensive– Used for analysis: unnecessary

• Periodic maintainence is OK, say each night.

Lu Chaojun, SJTU 19

Page 20: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Example

• Wal-Mart stores every sale at every store in a database.

• Overnight, the sales for the day are used to update the data warehouse = materialized views of the sales.

• The warehouse is used by analysts to predict trends and move goods to where they are selling best.

Lu Chaojun, SJTU 20

Page 21: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Rewriting Queries to Use MV

Given MV V:

SELECT LV FROM RV WHERE CV;

and query Q:

SELECT LQ FROM RQ WHERE CQ;

If i) RV RQ

ii) CQ = CV AND C

iii) (LQ RV) LV

Then rewrite Q:

SELECT LQ FROM V, RQ RV WHERE C;

Lu Chaojun, SJTU 21

Page 22: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Indexes in SQL

22

Page 23: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

What’s an Index?

• An index on R.A is a data structure that makes it efficient to find tuples having a fixed value for A.– A is the index key.– Index key can be any attribute or set of

attributes, not necessarily the key of R.

• Implementation of index– B-tree or B+-tree

Lu Chaojun, SJTU 23

key ptr

a

a b 23

Page 24: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 24

Why Index?

• Speed up queries, especially of formsR.A = value

R.A <= value

• Also speed up joins– Use index to find matching tuples

• Make queries faster, but modifications slower.

Page 25: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Declaring Indexes

• No standard!

• Typical syntax:CREATE INDEX nameInd ON S(name);

CREATE INDEX scInd ON SC(sno,cno);

• Dropping a index:DROP INDEX nameInd;

Lu Chaojun, SJTU 25

Page 26: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Selection of Indexes

• Design Issue: deciding which indexes to create.– Cost model: number of block I/O’s

• Pro: An index speeds up queries that can use it.

• Con: An index slows down modifications because the index must be modified too.

Lu Chaojun, SJTU 26

Page 27: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Useful Indexes

• Index on key of relation: e.g. S(sno)– Queries are often of the form:

SELECT … FROM … WHERE sno = …

– At most return one location (page)

• Index on attribute which is almost a key: e.g. S(name)– Return few locations

• Index on attribute on which tuples are clustered: e.g. S(dept)– Few blocks

Lu Chaojun, SJTU 27

Page 28: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Lu Chaojun, SJTU 28

Calculating the Best Indexes

• The more indexes the better?– No! Modification will be expensive.

• To create best indexes, we need information about which queries and modifications are most likely to be performed on DB.– Get info from history.– Get info from app code.

Page 29: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Example: Cost Estimation

SC(sno,cno)

Q1: SELECT cno FROM SC WHERE sno=‘xxx’;

Q2: SELECT sno FROM SC WHERE cno=‘yyy’;

I: INSERT INTO SC VALUES(‘xxx’,’yyy’);

Prob(Q1) = p1, Prob(Q2) = p2 , Prob(I ) = p3 =1 p1 p2

SC occupies n blocks.

On the average, ‘xxx’ takes c courses and ‘yyy’ is taken by s students.

Lu Chaojun, SJTU 29

action no index index on sno index on cno both index

Q1 n 1+c n 1+c

Q2 n n 1+s 1+s

I 2 4 4 6

Average np1+np2+2p3 (1+c)p1+np2+4p3 np1+(1+s)p2+4p3 (1+c)p1+(1+s)p2+6p3

Page 30: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

Automatic Selection of Indexes

• Tuning advisor– Establish a query workload.

According to history or application programs.

– Specify constraints, if any.– Generates candidate indexes and evaluates

each.Feed sample query to the query optimizer, which

assumes only this one index is available.

– The index set resulting the lowest cost is suggested, or automatically created.

Lu Chaojun, SJTU 30

Page 31: View 1. Lu Chaojun, SJTU 2 View Three-level vision of DB users Virtual DB views DB Designer Logical DB relations DBA DBA Physical DB stored info.

End