Sie sind auf Seite 1von 40

Advance Database Systems Course Code: CSE 515 LTPC:3 10 4

M. Venkatesan Assistant Professor (Selection Grade) School of Computing Science & Engineering VIT University
1

Syllabus
CSE 515 ADVANCED DATABASE SYSTEMS LTPC3104 Contents: DATABASE DESIGN AND TUNING Introduction to physical database design Guideline for index selection- Overview of database tuning Conceptual schema tuning Queries and view tuning. PARALLEL AND DISTRIBUTED DATABASE Parallel database systems: Architecture of parallel databases, parallel Query evaluation, parallelizing joins and parallel query optimization. Distributed database systems: Distributed database architecture, Properties of distributed database, Types of distributed database, storing data in a distributed DBMS, distributed query processing EMERGING DATABASE TECHNOLOGIES Multimedia databases: Multimedia sources, Multimedia database queries, Multimedia database applications, Mobile databases: Architecture of mobile databases, characteristics of mobile computing, mobile DBMS, Object Database System: Abstract data types, object identity and reference types, inheritance, and Database design for ORDBMS DATA WAREHOUSING Data warehousing: Definition and terminology, Data Preprocessing, Main components of data warehouse, Data warehouse architecture, OLAP technology, Data mart. Text/ Reference Books 1. Raghu Ramakrishnan and Johannes Gehrke: Database Management Systems, III Edition, McGraw Hill,2000. 2. S.K.Singh, Database Systems: Concepts, Design & Applications, Pearson education, 2006 3. Ramez Elmasri & B.Navathe: Fundamentals of database systems, IV edition, Addison Wesley, 2005. 4. Jiawei Han and Micheline Kamber, Data Mining-Concepts and Techniques, Morgan kaufmann publishers, 2005.

Database Design and Tuning


Introduction to physical database design Guideline for index selection Overview of database tuning Conceptual schema tuning Queries and View tuning

Introduction to physical database design


Tune or adjust all aspects of a database design for Performance of Database

Based on Workload description and user requirement ,the database to be tuned


Database designer must understand working of indexing, query processing and understand the workload choose indexes, make clustering decisions, and to refine the conceptual and external schemas (if necessary) to meet performance goals

Database Workload
Understanding the workload: The most important queries and how often they arise. The most important updates and how often they arise. The desired performance for these queries and updates For each query in the workload: Which relations does it access? Which attributes are retrieved? Which attributes are involved in selection/join conditions? How selective are these conditions likely to be? For each update in the workload: Which attributes are involved in selection/join conditions? How selective are these conditions likely to be? The type of update (INSERT/DELETE/UPDATE), and the attributes that are affected. Note: while update, index may slow down the process
5

Physical Design and Tuning Decisions


Choice of indexes to create What indexes should we create? Which relations should have indexes? What field(s) should be the search key? Should we build several indexes? For each index, what kind of an index should it be? Clustered? Hash/tree? Tuning the conceptual Schema Should we make changes to the conceptual schema? Consider alternative normalized schemas? (Remember, there are many choices in decomposing into BCNF, etc.) Should we ``undo some decomposition steps and settle for a lower normal form? (Denormalization.) Horizontal partitioning, replication, views ... Queries and Transaction tuning Rewrite frequently executed queries and transaction to run 6 faster

Index
Is a schema object Is used by the Oracle server to speed up the retrieval of rows by using a pointer Can reduce disk I/O by using a rapid path access method to locate data quickly Is independent of the table it indexes Is used and maintained automatically by the Oracle server How Are Indexes Created? Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition. Manually: Users can create nonunique indexes on columns to 7 speed up access to the rows.

When to Create an Index


You should create an index if: A column contains a wide range of values A column contains a large number of null values One or more columns are frequently used together in a WHERE clause or a join condition The table is large and most queries are expected to retrieve less than 2 to 4 percent of the rows

When Not to Create an Index


It is usually not worth creating an index if: The table is small The columns are not often used as a condition in the query Most queries are expected to retrieve more than 2 to 4 percent of the rows in the table The table is updated frequently The indexed columns are referenced as part of an expression The USER_INDEXES data dictionary view contains the name of the index and its uniqueness. The USER_IND_COLUMNS view contains the index name, the table name, and the column name.
9

Indexes
An index on a file speeds up selections on the search key fields for the index. Any subset of the fields of a relation can be the search key for an index on the relation. Search key is not the same as key (minimal set of fields that uniquely identify a record in a relation). An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k.

10

Alternatives for Data Entry k* in Index


In a data entry k* we can store: 1. Actual data record with search key value k, -Clustered 2. A data entry is a <k, rid> pair, where rid is the record id of data record with search key value k 3. A data entry is a <k, rid-list> where rid-list is a list of record ids of data records with search key value k Note: Alternative 2 & 2 are unclustered

11

Index Classification
Primary vs. secondary: If search key contains primary key, then called primary index. Unique index: Search key contains a candidate key. Clustered vs. unclustered: If order of data records is the same as, or `close to, order of data entries, then called clustered index. Alternative 1 implies clustered A file can be clustered on at most one search key. Cost of retrieving data records through index varies greatly based on whether index is clustered or not!
CLUSTERED
Index entries direct search for data entries

UNCLUSTERED

Data entries

Data entries (Index File) (Data file)

Data Records

Data Records

12

Choice of Indexes
Choice of Indexes One approach: consider the most important queries in turn. Consider the best plan using the current indexes, and see if a better plan is possible with an additional index. If so, create it. Before creating an index, must also consider the impact on updates in the workload Trade-off: indexes can make queries go faster, updates slower. Require disk space, too.

13

Guidelines for Index Selection


G1: Whether to Index? Do not build index if query component is more on updates Create index to speed up for query operation G2:Choice of Search key Attributes in WHERE clause are candidates for index keys. Exact match condition suggests hash index. Equality query or exact -match: Every field value is equal to a constant value. E.g. wrt <sal,age> index: age=20 and sal =75 Range query suggests tree index.

Range query: Some field value is not a constant. E.g.:

age>20 and sal > 10 Benefits from B+ Tree Index, Clustering benefits range queries Clustering is especially useful for range queries; can also help on equality queries if there are many duplicates.

14

Guidelines for Index Selection


G3:Multi-attribute search keys should be considered when a WHERE clause contains several conditions. Order of attributes is important for range queries. Such indexes can sometimes enable index-only strategies for important queries. For index-only strategies, clustering is not important! G4: Whether to Cluster:
At most one index on a given relation can be clustered & clustering affects performance greatly, so the choice of clustered index is important Range queries benefits from clustering If an index enables an index only evaluation strategy, no need of clustered index

15

Guidelines for Index Selection


G5:Hash versus Tree Index
B+Tee index is preferable ,it supports range queries and equality queries When considering a join condition:
Hash index is better in the case of Index nested loop join and equality queries B+-tree on inner is very good for Index Nested Loops

Should be clustered if join column is not key for inner, and inner tuples need to be retrieved.
Clustered B+ tree on join column(s) is good for Sort-Merge.

G6: Balancing the Cost of Index Maintenance


If index slow down the frequent update operation, drop the index Adding an index may well speed up given update operation. For example an index on employee id could speed up the operation of the increasing the salary of a given employee.
16

Example for Index Selection E.dno Index on


Index on D.dname instead of d.dno

SELECT E.ename, D.mgr FROM Emp E, Dept D WHERE D.dname=Toy AND E.dno=D.dno Hash index on D.dname supports Toy selection. Given this, index on D.dno is not needed. Index on E.age no need of Hash index on E.dno allows us to get matching (inner) Empindex on E.dno tuples for each selected (outer) Dept tuple. What if WHERE included: `` ... AND E.age=25 ? Could retrieve Emp tuples using index on E.age, then join with Dept tuples satisfying dname selection. Comparable to strategy that used E.dno index. So, if E.age index is already created, this query provides much less motivation for adding an E.dno index.
17

Example for Index Selection


SELECT E.ename, D.mgr FROM Emp E, Dept D WHERE E.sal BETWEEN 10000 AND 20000 AND E.hobby=Stamps AND E.dno=D.dno Clearly, Emp should be the outer relation. Suggests that we build a hash index on D.dno. What index should we build on Emp? B+ tree on E.sal could be used, OR an index on E.hobby could be used. Only one of these is needed, and which is better depends upon the selectivity of the conditions. As a rule of thumb, equality selections more selective than range selections. Selective: number of tuple from database-less number of tuplegood selective-more number of tuple-poor selectivy
18

Examples of Clustered Indexes


B+ tree index on E.age can be used to get qualifying tuples. SELECT E.dno How selective is the condition? FROM Emp E Is the index clustered? WHERE E.age>40 Consider the GROUP BY query. If many tuples have E.age > 10, using E.age index and sorting the retrieved tuples may be costly. Clustered E.dno index may be better! SELECT E.dno, COUNT (*) Equality queries and duplicates: FROM Emp E Clustering on E.hobby helps! WHERE E.age>10

GROUP BY E.dno

SELECT E.dno FROM Emp E WHERE E.hobby=Stamps 19

Clustering and Joins


SELECT E.ename, D.mgr FROM Emp E, Dept D WHERE D.dname=Toy AND E.dno=D.dno

Clustering is especially important when accessing inner tuples in INL. Should make index on E.dno clustered. Suppose that the WHERE clause is instead: WHERE E.hobby=Stamps AND E.dno=D.dno If many employees collect stamps, Sort-Merge join may be worth considering. A clustered index on D.dno would help. Summary: Clustering is useful whenever many tuples are to be retrieved.
20

Composite Search Keys


To retrieve Emp records with age=30 AND sal=4000, an index on <age,sal> would be better than an index on age or an index on sal. If condition is: 20<age<30 AND 3000<sal<5000: Clustered tree index on <age,sal> or <sal,age> is best. If condition is: age=30 AND 3000<sal<5000: Clustered <age,sal> index much better than <sal,age> index! Composite indexes are larger, updated more often.

21

Co-Clustering Two Relations


Records from more than one relation to be stored in a single file Co-clustering. Parts(pid,pname,cost,supplierid) Assembly(partid,componentid,quantity) Select P.Pid, A.Componentid from Parts P,Assembly A where P.pid=A.partid and P.supplierid= KAVEN Co-cluster two tables, store records of two table together, with each Parts record P followed by all the Assembly records A.
22

Co-Clustering
It can speed of Joins, in particular key-foreign key joins corresponding to 1:N rel A sequential scan of either relation becomes slower . All inserts deletes and updates that alter records lengths become slower

23

Indexes that Enable Index Only Plans


<E.dno>

SELECT D.mgr FROM Dept D, Emp E WHERE D.dno=E.dno

SELECT D.mgr, E.eid A number of <E.dno,E.eid> FROM Dept D, Emp E queries can be answered WHERE D.dno=E.dno without SELECT E.dno, COUNT(*) retrieving any <E.dno> FROM Emp E tuples from one GROUP BY E.dno or more of the relations SELECT E.dno, MIN(E.sal) <E.dno,E.sal> FROM Emp E involved if a suitable index B-tree trick! GROUP BY E.dno is available. <E. age,E.sal> SELECT AVG(E.sal) or FROM Emp E <E.sal, E.age> WHERE E.age=25 AND 24 E.sal BETWEEN 3000 AND 5000

Tuning the Conceptual Schema


The choice of conceptual schema should be guided by the workload, in addition to redundancy issues: We may settle for a 3NF schema rather than BCNF. Workload may influence the choice we make in decomposing a relation into 3NF or BCNF. We may further decompose a BCNF schema! We might denormalize (i.e., undo a decomposition step), or we might add fields to a relation. We might consider horizontal decompositions. If such changes are made after a database is in use, called schema evolution; might want to mask some of these changes from applications by defining views
25

Example Schemas
Contracts (Cid, Sid, Jid, Did, Pid, Qty, Val) Depts (Did, Budget, Report) Suppliers (Sid, Address) Parts (Pid, Cost) Projects (Jid, Mgr) We will concentrate on Contracts, denoted as CSJDPQV. The following ICs are given to hold: JP C, SD P, C is the primary key. What are the candidate keys for CSJDPQV? What normal form is this relation schema in?
26

Settling for 3NF vs BCNF


CSJDPQV can be decomposed into SDP and CSJDQV, and both relations are in BCNF. (Which FD suggests that we do this?) Lossless decomposition, but not dependency-preserving. Adding CJP makes it dependency-preserving as well. Suppose that this query is very important: Find the number of copies Q of part P ordered in contract C. Requires a join on the decomposed schema, but can be answered by a scan of the original relation CSJDPQV. Could lead us to settle for the 3NF schema CSJDPQV
27

Denormalization
Suppose that the following query is important: Is the value of a contract less than the budget of the department? To speed up this query, we might add a field budget B to Contracts. This introduces the FD D B wrt Contracts. Thus, Contracts is no longer in 3NF.

28

Choice of Decompositions
There are 2 ways to decompose CSJDPQV into BCNF: SDP and CSJDQV; lossless-join but not dep-preserving. SDP, CSJDQV and CJP; dep-preserving as well. The difference between these is really the cost of enforcing the FD JP C. 2nd decomposition: Index on JP on relation CJP

29

Choice of Decompositions (cont) Choice of Decompositions


The following ICs were given to hold: JP C, SD P, C is the primary key. Suppose that, in addition, a given supplier always charges the same price for a given part: SPQ V. If we decide that we want to decompose CSJDPQV into BCNF, we now have a third choice: Begin by decomposing it into SPQV and CSJDPQ. Then, decompose CSJDPQ (not in 3NF) into SDP, CSJDQ. This gives us the lossless-join decomp: SPQV, SDP, CSJDQ. To preserve JP C, we can add CJP, as before. Choice: { SPQV, SDP, CSJDQ } or { SDP, CSJDQV } ? 30

Decomposition of a BCNF Relation


Suppose that we choose { SDP, CSJDQV }. This is in BCNF, and there is no reason to decompose further (assuming that all known ICs are FDs). However, suppose that these queries are important: Find the contracts held by supplier S. Find the contracts that department D is involved in. Decomposing CSJDQV further into CS, CD and CJQV could speed up these queries. (Why?) On the other hand, the following query is slower: Find the total value of all contracts held by supplier S.
31

Horizontal Decompositions
Our definition of decomposition: Relation is replaced by a collection of relations that are projections. Most important case. Sometimes, might want to replace relation by a collection of relations that are selections. Each new relation has same schema as the original, but a subset of the rows. Collectively, new relations contain all rows of the original. Typically, the new relations are disjoint.

32

Horizontal Decompositions
Suppose that contracts with value > 10000 are subject to different rules. This means that queries on Contracts will often contain the condition val>10000. One way to deal with this is to build a clustered B+ tree index on the val field of Contracts. A second approach is to replace contracts by two new relations: LargeContracts and SmallContracts, with the same attributes (CSJDPQV). Performs like index on such queries, but no index overhead. Can build clustered indexes on other attributes, in addition!
33

Tuning Queries
If a query runs slower than expected, check if an index needs to be re-built, or if statistics are too old. Sometimes, the DBMS may not be executing the plan you had in mind. Common areas of weakness: Selections involving null values. Selections involving arithmetic or string expressions. Selections involving OR conditions. Lack of evaluation features like index-only strategies or certain join methods or poor size estimation. Check the plan that is being used! Then adjust the choice of indexes or rewrite the query/view
34

Tuning Queries
Complicated by interaction of: NULLs, duplicates, aggregation, subqueries. Guideline: Use only one query block, if possible
SELECT DISTINCT * FROM Sailors S WHERE S.sname IN (SELECT Y.sname FROM YoungSailors Y)

SELECT DISTINCT S.* FROM Sailors S, YoungSailors Y WHERE S.sname = Y.sname

35

Guidelines for Query Tuning


Minimize the use of DISTINCT: dont need it if duplicates are acceptable, or if answer contains a key. Minimize the use of GROUP BY and HAVING
SELECT MIN (E.age) FROM Employee E GROUP BY E.dno HAVING E.dno=102 SELECT MIN (E.age) FROM Employee E WHERE E.dno=102

Consider DBMS use of index when writing arithmetic expressions: E.age=2*D.age will benefit from index on E.age, but might not benefit from index on D.age!
36

Guidelines for Query Tuning


Avoid using intermediate relations:
SELECT * INTO Temp FROM Emp E, Dept D WHERE E.dno=D.dno AND

D.mgrname=Joe
SELECT T.dno, AVG(T.sal) FROM Temp T GROUP BY T.dno

SELECT E.dno, AVG(E.sal) FROM Emp E, Dept D WHERE E.dno=D.dno AND D.mgrname=Joe GROUP BY E.dno

If there is a dense B+ tree index on <dno, sal>, an index-only plan can be used to avoid retrieving Emp tuples in the second query 37

Understanding the nature of the workload for the application, and the performance goals, is essential to developing a good design. What are the important queries and updates? What attributes/relations are involved? Indexes must be chosen to speed up important queries (and perhaps some updates!). Index maintenance overhead on updates to key fields. Choose indexes that can help many queries, if possible. Build indexes to support index-only strategies. Clustering is an important decision; only one index on a given relation can be clustered! Order of fields in composite index key can be important. Static indexes may have to be periodically re-built. 38 Statistics have to be periodically updated

Conclusion Important Points in Physical Database Design

Conclusion Important Points in Physical Database Design


Over time, indexes have to be fine-tuned (dropped, created, reclustered, ...) for performance. Should determine the plan used by the system, and adjust the choice of indexes appropriately. System may still not find a good plan: Only left-deep plans? Null values, arithmetic conditions, string expressions, the use of ORs, nested queries, etc. can confuse an optimizer. So, may have to rewrite the query/view: Avoid nested queries, temporary relations, complex conditions, and operations like DISTINCT and GROUP BY
39

Tune U Minds to Solve the Problems

40

Das könnte Ihnen auch gefallen