F28DM Indexes in Oracle 1 F28DM : Database Management Systems Indexes in Oracle Monica Farrow Room: EMG30, Ext: 4160 Material on Vision.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Advertisements

Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
F28DM Database Management Systems Query Optimisation
CpSc 3220 File and Database Processing Lecture 17 Indexed Files.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Hashing and Indexing John Ortiz.
1 Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes November 14, 2007.
Introduction to Structured Query Language (SQL)
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
IS 4420 Database Fundamentals Chapter 6: Physical Database Design and Performance Leon Chen.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part A Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
1 Lecture 20: Indexes Friday, February 25, Outline Representing data elements (12) Index structures (13.1, 13.2) B-trees (13.3)
Indexes Rose-Hulman Institute of Technology Curt Clifton.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Introduction to Structured Query Language (SQL)
DBMS Internals: Storage February 27th, Representing Data Elements Relational database elements: A tuple is represented as a record CREATE TABLE.
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.
The Relational Model These slides are based on the slides of your text book.
Lecture 8 Index Organized Tables Clusters Index compression
Oracle Data Block Oracle Concepts Manual. Oracle Rows Oracle Concepts Manual.
1 Physical Data Organization and Indexing Lecture 14.
1 © Prentice Hall, 2002 Physical Database Design Dr. Bijoy Bordoloi.
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.
Chapter 6 1 © Prentice Hall, 2002 The Physical Design Stage of SDLC (figures 2.4, 2.5 revisited) Project Identification and Selection Project Initiation.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
© Pearson Education Limited, Chapter 13 Physical Database Design – Step 4 (Choose File Organizations and Indexes) Transparencies.
10/10/2012ISC239 Isabelle Bichindaritz1 Physical Database Design.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Implementation of Relational Operators/Estimated Cost 1.Select 2.Join.
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.
Methodology – Physical Database Design for Relational Databases.
University of Sunderland COM 220 Lecture Ten Slide 1 Database Performance.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Database Indexing 1 After this lecture, you should be able to:  Understand why we need database indexing.  Define indexes for your tables in MySQL. 
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Chapter 5 Index and Clustering
Session 1 Module 1: Introduction to Data Integrity
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
CS 440 Database Management Systems Lecture 6: Data storage & access methods 1.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
1 Indexes ► Sort data logically to improve the speed of searching and sorting operations. ► Provide rapid retrieval of specified rows from the table without.
CS4432: Database Systems II
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Select Operation Strategies And Indexing (Chapter 8)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Indexes By Adrienne Watt.
Indexing Structures for Files and Physical Database Design
Record Storage, File Organization, and Indexes
CS 540 Database Management Systems
Indexing and hashing.
COMP 430 Intro. to Database Systems
Methodology – Physical Database Design for Relational Databases
Database Management Systems (CS 564)
File organization and Indexing
Chapter 11: Indexing and Hashing
Lecture 12 Lecture 12: Indexing.
Chapter 4 Indexes.
CH 4 Indexes.
Lecture 19: Data Storage and Indexes
CH 4 Indexes.
The Physical Design Stage of SDLC (figures 2.4, 2.5 revisited)
Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes May 16, 2008.
Lecture 20: Indexes Monday, February 27, 2006.
Chapter 11: Indexing and Hashing
Presentation transcript:

F28DM Indexes in Oracle 1 F28DM : Database Management Systems Indexes in Oracle Monica Farrow Room: EMG30, Ext: 4160 Material on Vision & my web page Content taken from HW lecturers, + books by Rob & Coronel, and by Connolly & Begg For more info on everything, see Oracle9i Database Concepts (on the web)

F28DM Indexes in Oracle 2 Basic Use of Indexes Primary, Secondary, Composite When to use

F28DMIndexes in Oracle3 Indexes An index helps speed up retrieval. A column that is frequently referred to in the WHERE clause is a potential candidate for indexing. Why use indexing? It's easy and provides immediate value It can be used to speed up existing applications without changing any of their logic or queries One of the main reasons for the success of relational databases is that they have efficient indexing systems.

F28DMIndexes in Oracle4 Index recap An index typically consists of the search key (one or more attributes) and the row ID(s) of record(s) which match this key An index on a file speeds up selections on the search key fields specified for that index Any subset of the fields of a relation can be the search key for an index on the relation A primary index allows efficient searching on the primary key of a relation A secondary index allows efficient searching on other attributes which are often used in queries

F28DMIndexes in Oracle5 Example: the “Sailors and Boats” Domain Information needs to be stored about sailors, boats and reservations that sailors are making for boats There are three relations with the following schemas: Sailor (id:int, name:string, rating:int, age:int, base:string) Boat (id:int, name:string, colour:string, base:string) Reservation (sid:int, bid:int, day:date)

F28DMIndexes in Oracle6 Automatically Created Indexes On every relation, Oracle creates a non-clustered B+tree index for the primary key A B-tree is shown on the next slide A B-tree stays balanced when items are added and removed Indexes will be created on Sailor table, id column Boat table, id column Reservations table, a composite index on sid, bid and day Additionally Oracle will also create an index for every UNIQUE constraint

F28DMIndexes in Oracle7 B-tree index Diagram just to give you an idea, you don’t have to memorise this

F28DMIndexes in Oracle8 Composite index keys If the key consists of more than one attribute, the index is a composite or concatenated index i.e. an index for several attributes A composite index is sorted lexicographically according to the order of the attributes E.g for a composite key (name,age), we have (Kelly, 22) < (Kelly, 63) < (Smith, 18) < (Smith,36) This is different to a composite key (age, name)

F28DMIndexes in Oracle9 Secondary indexes Create secondary indexes for other attributes in your tables which are frequently searched by In the SQL WHERE clause, not the SQL SELECT line In this case, you must explicitly create the index, as shown on the next slide

F28DMIndexes in Oracle10 Explicitly Created Indexes An index Has a name. Choose an informative one. Is created for a sequence of attributes over a table which must already exist Can be DROPped Examples CREATE INDEX sailor_name_idx ON Sailor(name); DROP INDEX sailor_name_idx; CREATE INDEX sailor_name_and_age_idx ON Sailor(name, age)

F28DMIndexes in Oracle11 Using CREATE INDEX (1) The CREATE INDEX statement creates a sorted index on the specified columns of the named table. Indexes are automatically used to improve the performance of queries issued to the database, and to sort queries with an ORDER BY clause. Once an index is created, it is never referenced in a SQL statement again except to validate it (VALIDATE INDEX) or delete it (DROP INDEX). Indexes cannot be created for views. Add an index to the underlying base table

F28DMIndexes in Oracle12 Using CREATE INDEX (2) Indexes are always owned by the owner of the table. The index name must be unique for each table and owner. Exclusive table use. You can’t create an index if the table is in use. CREATE INDEX can be time consuming and the server will not process requests referencing the same table while the statement is being processed. As with all other CREATE commands, store the command in a text file To remind you what the index is like So it can be rerun if you decide to re-create your DB

F28DMIndexes in Oracle13 Overheads v performance There is an overhead involved in the maintenance and use of secondary indexes. The index must be updated whenever the table is updated Disk space is needed for the index Balance the overhead against the performance benefits Faster data retrieval

F28DMIndexes in Oracle14 How to choose secondary indexes (1) Add a secondary index to a foreign key if it is frequently accessed E.g. Boat id in Reservations, if we frequently want to know the name of the boat in a reservation Add a secondary index to any attribute that is frequently used as a search key E.g. day in Reservations (what reservations do we have for today or tomorrow?) E.g. name in Sailor (what is the rating for the sailor called ‘Lubber’?). People often search by name rather than id.

F28DMIndexes in Oracle15 How to choose secondary indexes (2) Add a secondary index to any attributes frequently used in order by, group by, min, max, avg E.g. age in Sailor if frequently want list in age order Add a composite secondary index to a small set of attributes if the index would provide the full details for a frequently used query without having to search the base table E.g. rating and age in Sailor if a frequent query is SELECT rating, AVG(age) FROM Sailor GROUP BY rating;

F28DMIndexes in Oracle16 When not to use secondary indexes If the relation is small – not many rows If the relation or attribute is frequently updated If periodic large updates, drop the index, update the data, re-create the index If the attribute is always used in queries that retrieve a significant proportion of the rows in the relation E.g. If the attribute has low sparsity (i.e. the number of different values is small, such as gender F or M).

F28DM Indexes in Oracle 17 More Advanced Indexing Topics Index-organised tables Bitmap indexes Clusters

F28DMIndexes in Oracle18 Index organised tables Entries of index are table records rather than row IDs Useful if frequent access to complete table via the index In Oracle, the default table organisation is heap To store the table in primary key order, add the words ORGANIZATION INDEX to the end of the CREATE TABLE command In the B-tree, the leaf nodes are replaced by all the non- primary key attributes in the row

F28DMIndexes in Oracle19.Bitmap indexes Bitmap indexes An example on the next slide For each value of a domain, there is a bitmap identifying the row Ids of satisfying tuples Usually applied to attributes with a sparse domain In Oracle, <100 distinct values E.g. bitmaps for all tuples with sex = male and for sex=female Updating a bitmap takes a lot of time, so use for tables with hardly any updates, inserts, deletes Ideal for data warehousing See later lecture, if time

F28DMIndexes in Oracle20 Bitmap indexes example The first table is the Sailors table The second table shows a bitmap index for rating, assuming values are only from IdRatingetc 221Other data 232Other data 313Other data 351Other data

F28DMIndexes in Oracle21 Clusters in Oracle Oracle supports 2 types of clusters Indexed clusters Hashed clusters

F28DMIndexes in Oracle22 Clusters “Clusters are groups of 1 or more tables physically stored together because they share common columns and are often used together” (Connolly & Begg) This improves disk access The related columns of the tables are called the cluster key

F28DMIndexes in Oracle23 Saiilor/Reservation cluster example Sailor and Reservation relations could be stored together, with the cluster key = Sailor id Each relation can be retrieved independently Whenever the joined relations are required, access is faster nameratingageidbidday Dustin /2/ /4/ /5/08 Rusty /5/ /6/08

F28DMIndexes in Oracle24 Should we cluster Sailors and Reservations? A cluster for Sailors and Reservations could be created if many queries in the application joined these tables together. A cluster for Sailors and Reservations would be inefficient if reservations were only a minor part of the application, and many more queries involved Sailors joined with other tables

F28DMIndexes in Oracle25 Indexed clusters Use when Queries retrieve records over a range of cluster key values Clustered tables may grow unpredictably With an indexed table or index cluster, Oracle locates the rows in a table using key values that Oracle stores in a separate index. E.g. No idea how many reservations there are going to be Queries like SELECT * FROM Sailors S, Reservations R WHERE S.id = R.sid AND S.id > 50;

F28DMIndexes in Oracle26 Defining a indexed cluster in SQL(1) Create a cluster first CREATE CLUSTER sailor_id_cluster (id int); A cluster can be made for a fixed number of typed attributes (here just one) Define an index on the cluster CREATE INDEX sailor_id_idx ON CLUSTER sailor_id_cluster;

F28DMIndexes in Oracle27 Defining an indexed cluster in SQL (2) Create table(s) for the cluster CREATE TABLE Sailor (id INT NOT NULL, name VARCHAR(20), ranking INT, age INT) CLUSTER sailor_id_cluster(id); CREATE TABLE Reservation (sid INT, bid INT as before....) CLUSTER sailor_id_cluster(sd);

F28DMIndexes in Oracle28 Hash Clusters The key of a hash cluster, like the key of an index cluster, can be a single column or composite key (multiple column key). A hash function is applied to the cluster key to obtain a hash value. The hash value determines the location where the rows of a table are physically stored. So all records with the same cluster key are stored together

F28DMIndexes in Oracle29 Creating a hash cluster Create a hash cluster CREATE CLUSTER sailor_id_hashcluster (id int) HASH IS id HASHKEYS 10000; Add table(s) as before CREATE TABLE Sailor (id INT NOT NULL, name VARCHAR(20), ranking INT, age INT) CLUSTER sailor_id_hashcluster(id);

F28DMIndexes in Oracle30 Use hashing when (1) Most queries are equality queries on the cluster key: SELECT... WHERE cluster_key =...; the cluster key in the equality condition is hashed, and the corresponding hash key is usually found with a single read. In comparison, for an indexed table the key value must first be found in the index (usually several reads), and then the row is read from the table (another read). AND WHEN...

F28DMIndexes in Oracle31 Use hashing when (2) AND The tables in the hash cluster are primarily static in size so that you can determine the number of rows and amount of space required for the tables in the cluster. If tables in a hash cluster require more space than the initial allocation for the cluster, performance degradation can be substantial because overflow blocks are required.

F28DMIndexes in Oracle32 Hashing v Indexing To find or store a row in an indexed table or cluster, a minimum of two (there are usually more) I/Os must be performed: One or more I/Os to find or store the key value in the index Another I/O to read or write the row in the table or cluster In contrast, if a hash function is used to locate a row in a hash cluster; no I/O is required. As a result, a minimum of one I/O operation is necessary to read or write a row in a hash cluster.