Chapter 8 Views and Indexes 第 8 章 视图与索引. 8.1 Virtual Views  Views:  “virtual relations”. Another class of SQL relations that do not exist physically.

Slides:



Advertisements
Similar presentations
Query optimisation.
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
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)
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Notes on Chapter 8 Transactions from Chapter 6 Views and Indexes from Chapter 8.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
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.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 17 Methodology – Physical Database Design for Relational Databases Transparencies © Pearson Education Limited 1995, 2005.
Team Dosen UMN Physical DB Design Connolly Book Chapter 18.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data This slides are from J. Ullman’s.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Lecture 9 Methodology – Physical Database Design for Relational Databases.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Physical Database Design Chapter 6. Physical Design and implementation 1.Translate global logical data model for target DBMS  1.1Design base relations.
Database Management 9. course. Execution of queries.
SQL Server Indexes Indexes. Overview Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
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.
Module 4 Database SQL Tuning Section 3 Application Performance.
1 Database Systems Defining Database Schema Views.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
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.
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.
Structured Query Language
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
Transactions, Views, Indexes Introduction to Transactions: Controlling Concurrent Behavior Virtual and Materialized Views Indexes: Speeding Accesses to.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Retele de senzori Curs 2 - 1st edition UNIVERSITATEA „ TRANSILVANIA ” DIN BRAŞOV FACULTATEA DE INGINERIE ELECTRICĂ ŞI ŞTIINŢA CALCULATOARELOR.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Virtual and Materialized Views Speeding Accesses to Data
Chapter 6 - Database Implementation and Use
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
CPSC-608 Database Systems
Methodology – Physical Database Design for Relational Databases
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Lecture 12: SQL Friday, October 20, 2000.
Virtual and Materialized Views Speeding Accesses to Data
Views and Indexes Controlling Concurrent Behavior
Speeding Accesses to Data
CPSC-608 Database Systems
Transactions, Views, Indexes
Lecture 05: SQL Wednesday, October 9, 2002.
Lecture 14: SQL Wednesday, October 31, 2001.
Chapter 8 Views and Indexes
Presentation transcript:

Chapter 8 Views and Indexes 第 8 章 视图与索引

8.1 Virtual Views  Views:  “virtual relations”. Another class of SQL relations that do not exist physically. They are defined by an expression much like a query. They can be queried as if they existed physically, and in some cases, we can even modify views.  We can define several views in a database.  Why we need views?  Simplify computation.  Different user concern about different attributes in a relation.  Safety.

8.1.1 Declaring Views  How to declare views?  CREATE VIEW [ owner.]view-name [( column- name,... )] AS select-without-order-by [with check option]

8.1.1 Declaring Views  Example: CREATE View orderamount As select salesorder. orderno, signdate, empid, custid, Sum(quantity) as qty, Sum(unitprice*quantity) as amount from dba.salesitem,dba.salesorder where salesorder.orderno = salesitem.orderno group by salesorder.orderno,signdate,empid,custid;  The result is a virtual relation (view): orderamount(orderno, signdate, empid, custid, qty, amount)

8.1.2 Querying Views  The grammars of querying views and base tables are same.  Example: SELECT * FROM orderamount;  Example: Find the IDs and signing dates of order forms with maximum total. Select orderno, signdate, amount From orderamount Where amount = (Select Max (amount) From orderamount);

8.1.2 Querying Views  Example: Find the IDs, signing dates, salesmen’s IDs and names of order forms with maximum sales amount in October Select orderno, signdate, salesman.empid, name From orderamount natural join salesman Where signdate between ' ' and ' ' AND amount = ( Select Max(amount) From orderamount Where signdate between ' ' and ' ');  Queries of views will be converted to those of base tables by SQL system.

8.1.3 Renaming attributes  We can give a view’s attributes new names.  Example: CREATE View order(no, orderdate, salesman, customer, amount) As Select salesorder. orderno, signdate, empid, custid,Sum(unitprice*quantity) as amount From dba.salesitem,dba.salesorder Where salesorder.orderno = salesitem.orderno Group by salesorder.orderno, signdate, empid, custid; The result is a virtual table: order(no, orderdate, salesman, customer, amount)

8.2 Modifying Views  How to modify a view declaration?  ALTER VIEW [ owner.]view-name [( column- name,... )] AS select-without-order-by [with check option];  How to delete a view declaration?  DROP VIEW [owner.]view-name;

8.2 Modifying Views  Which views can be updated?  From a single table.  The attributes in the SELECT clause must include all attributes with NOT NULL.  No GROUP BY clause.  No aggregate.  No UNION.  No DISTINCT.  The WHERE clause must no involve R in a subquery. (no correlated subquery)  Modifications of views will be translated to equivalent modifications on base tables by SQL system.

8.2 Modifying Views  When a trigger is defined on a view, we can use INSTEAD OF in place of BEFORE or AFTER.  When an event awakens the trigger, the action of the trigger is done instead of the event itself.

8.4 Selection of Indexes  Index  data structure used to speed access to tuples of a relation, given values of one or more attributes.  Could be a hash table, but in a DBMS it is always a balanced search tree with giant nodes (a full disk page) called a B-tree.

8.4 Selection of Indexes  No standard!  Typical syntax:  CREATE INDEX SalesmanInd ON Salesman(name);  CREATE INDEX SalesorderInd ON Salesorder(signdate, empid);  Given a value v, the index takes us to only those tuples that have v in the attribute(s) of the index.

8.4 Selection of Indexes  Example: Use SalesmanInd and SalesorderInd to find the order No. of order forms sold by ‘zhangsan’ in Oct SELECT orderno FROM salesman, salesorder WHERE name = ‘zhangsan’ AND salesorder.empid = salesman.empid AND signdate between ‘ ’ and ‘ ’;  Use SalesmanInd to get all the salesmen named ‘Zhangsan’.  Then use SalesorderInd to get No's of those salesmen, with signing date is in Oct

8.4 Selection of Indexes  A major problem in making a database run fast is deciding which indexes to create.  Pro: An index speeds up queries that can use it.  Con: An index slows down all modifications on its relation because the index must be modified too.

8.4 Selection of Indexes  Suppose the only things we did with our sales database was:  Insert new facts into a relation (10%).  Find the order form of a given salesman’s ID in a given date (90%).  Then SalesorderInd on Salesorder(signdate, empid) would be wonderful, but SalesmanInd on Salesman(name) would be harmful.

8.4 Selection of Indexes  A major research thrust.  Because hand tuning is so hard.  An advisor gets a query load, e.g.:  Choose random queries from the history of queries run on the database, or  Designer provides a sample workload.

8.4 Selection of Indexes  The advisor generates candidate indexes and evaluates each on the workload.  Feed each sample query to the query optimizer, which assumes only this one index is available.  Measure the improvement/degradation in the average running time of the queries.