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.

Slides:



Advertisements
Similar presentations
Query optimisation.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Notes on Chapter 8 Transactions from Chapter 6 Views and Indexes from Chapter 8.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database Systems More SQL Database Design -- More SQL1.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Introduction. 
Practical Database Design and Tuning. Outline  Practical Database Design and Tuning Physical Database Design in Relational Databases An Overview of Database.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data This slides are from J. Ullman’s.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Data Warehousing and Decision Support Chapter 25, Part B.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Physical Database Design Chapter 6. Physical Design and implementation 1.Translate global logical data model for target DBMS  1.1Design base relations.
Query Optimization Arash Izadpanah. Introduction: What is Query Optimization? Query optimization is the process of selecting the most efficient query-evaluation.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
1 Notes on: Clusters Index and Cluster Creation in SQL Elisa Bertino CS Department and CERIAS Purdue University.
Indexes and Views Unit 7.
Lu Chaojun, SJTU Relational Data Model 1. Lu Chaojun, SJTU What’s a Data Model? A notation (collection of conceptual tools) for describing data as seen.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Views, Indexes Virtual and Materialized Views Speeding Accesses to Data.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
Relational Operator Evaluation. Overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g.,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Transactions, Views, Indexes Introduction to Transactions: Controlling Concurrent Behavior Virtual and Materialized Views Indexes: Speeding Accesses to.
IMS 4212: Database Implementation 1 Dr. Lawrence West, Management Dept., University of Central Florida Physical Database Implementation—Topics.
Chapter 8 Views and Indexes 第 8 章 视图与索引. 8.1 Virtual Views  Views:  “virtual relations”. Another class of SQL relations that do not exist physically.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
CS4432: Database Systems II
1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
SQL Triggers, Functions & Stored Procedures Programming Operations.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
CS4432: Database Systems II Query Processing- Part 1 1.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Virtual and Materialized Views Speeding Accesses to Data
Relational Data Model Lu Chaojun, SJTU.
CS 540 Database Management Systems
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
2018, Fall Pusan National University Ki-Joune Li
Unit I-2.
Virtual and Materialized Views Speeding Accesses to Data
Views and Indexes Controlling Concurrent Behavior
Speeding Accesses to Data
Views 1.
CPSC-608 Database Systems
Transactions, Views, Indexes
Chapter 8 Views and Indexes
Presentation transcript:

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

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.

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

Lu Chaojun, SJTU 6 View Declaration CREATE VIEW viewname(A1,...,An) AS query; Example CREATE VIEW CS_S AS SELECT sno,name,age FROM Student WHERE dept = ‘CS’; Only definition of views are stored.

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) Example SELECT 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?

Lu Chaojun, SJTU 8 Example: Join View Creation of view SN_C CREATE VIEW SN_C AS SELECT name,cno FROM S,SC WHERE S.sno = SC.sno; Query on SN_C SELECT name FROM SN_C WHERE cno = ‘C2’; –no join?

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;

Lu Chaojun, SJTU 10 Example: Views on Views CREATE VIEW CS_S_20 AS SELECT sno,name FROM CS_S WHERE age=20;

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?

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

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

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.

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

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

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

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

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

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

Rewriting Queries to Use MV Given MV V: SELECT L V FROM R V WHERE C V ; and query Q: SELECT L Q FROM R Q WHERE C Q ; If i) R V  R Q ii) C Q = C V AND C iii) (L Q  R V )  L V Then rewrite Q: SELECT L Q FROM V, R Q  R V WHERE C; Lu Chaojun, SJTU 21

Indexes in SQL 22

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

Lu Chaojun, SJTU 24 Why Index? Speed up queries, especially of forms R.A = value R.A <= value Also speed up joins –Use index to find matching tuples Make queries faster, but modifications slower.

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

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

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

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.

Example: Cost Estimation SC(sno,cno) Q 1 : SELECT cno FROM SC WHERE sno=‘xxx’; Q 2 : SELECT sno FROM SC WHERE cno=‘yyy’; I: INSERT INTO SC VALUES(‘xxx’,’yyy’); Prob(Q 1 ) = p 1, Prob(Q 2 ) = p 2, Prob(I ) = p 3 =1  p 1  p 2 SC occupies n blocks. On the average, ‘xxx’ takes c courses and ‘yyy’ is taken by s students. Lu Chaojun, SJTU 29 actionno indexindex on snoindex on cnoboth index Q1Q1 n1+cn Q2Q2 nn1+s I2446 Averagenp 1 +np 2 +2p 3 (1+c)p 1 +np 2 +4p 3 np 1 +(1+s)p 2 +4p 3 (1+c)p 1 +(1+s)p 2 +6p 3

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

End