27
Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes •Types and applications of Indexes • Create Synonym Application of Index and Synonym

Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

Embed Size (px)

DESCRIPTION

3 INDEX Object used to speed up the retrieval of rows (can) Reduce the disk I/O Is independent of the table indexes Used and maintained by the system automatically (Oracle Server)

Citation preview

Page 1: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

Chapter FourteenINDEX and SYNONYM

Dr. Chitsaz

Objectives:• Create and maintain Indexes•Types and applications of Indexes• Create Synonym• Application of Index and Synonym

Page 2: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

2

INDEX

• We are interested in finding the location of an object

• For large tables INDEX speed up the time to locate an object.

Page 3: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

3

INDEX

• Object used to speed up the retrieval of rows

• (can) Reduce the disk I/O• Is independent of the table indexes• Used and maintained by the system

automatically (Oracle Server)

Page 4: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

4

INDEX CREATION:

• UNIQUE INDEXES:When using primary key or unique, index is created automatically

• NONUNIQUE INDEXES: Can be created by user on column(s)

Page 5: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

5

UNIQUE INDEXE

CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9) PRIMARY KEY,

GPA NUMBER(3,2),B_Date DATE,Major CHAR(4));

Page 6: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

6

CREATE INDEX:

• CREATE [BITMAP | UNIQUE ] INDEX indexName ON tableName

(col, col, __ ) [REVERSE];• REVERSE: to reverse the order of index• UNIQUE: use B_tree

• CREATE INDEX stud_indexON student (name);

Page 7: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

7

CREATE FUNCTION BASED INDEX:-Use of index with functions- SELECT *

FROM STUDENTWHERE LOWER (name)=‘ john’;

-CREATE INDEX stud_lower_indexON student (LOWER (name));

Page 8: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

8

Index Unique Scan

SELECT * FROM STUDENTWHERE ID=11111;

Page 9: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

9

Index Range Scan

SELECT * FROM STUDENTWHERE name LIKE ‘JO%’;

Page 10: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

10

When Indexes Are Used

• Use of Equal (or IN): SELECT *

FROM STUDENTWHERE name =‘JOHN’;

• Use of Range Values on Indexed ColumnSELECT * FROM STUDENTWHERE name <‘J’;

Page 11: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

11

When Indexes Are Used

• Use of MIN or Max

Page 12: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

12

When Indexes Are Not Used

• Use of Function: SELECT *

FROM STUDENTWHERE LOWER(name) =‘john’;

• Use of NULL or NOT NULLSELECT * FROM STUDENTWHERE name is NULL;

Page 13: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

13

When Indexes Are Not Used

• Use of Not Equal: SELECT *

FROM STUDENTWHERE name != ‘john’;

• OrSELECT * FROM STUDENTWHERE name NOT IN (SELECT name

FROM studentWHERE GPA=3);

Page 14: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

14

When to use an index:

• Columns that are used frequently in WHERE clause

• Columns containing a wide range of values• Columns containing a large number of

NULLS• In large tables, where most retrieves are

less than 5% of the rows• Tables with JOIN, or ORDER BY, MAX/MIN

Page 15: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

15

When to use an index:

Examples:Phone numberY/NZip codeFlagCity, State

Online transaction

Page 16: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

16

When to use an index:

Examples:Phone number indexY/N Bit mapZip code Depend on local or national zip code

Flag Bit mapCity, State

Online transaction (live access) do not use bit map

Page 17: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

17

When not to use an index:

• Small tables• Tables that are updated frequently• Retrieve is more than 5% of rows

Page 18: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

18

When not to use a bitmap index:

• Small tables• Frequently updated tables

Page 19: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

19

INDEXES

SELECT INDEX_NAMEFROM USER_INDEXES //INDWHERE TABLE_NAME=‘STUDENT’;

• SELECT *FROM USER_IND_COLUMNS;

Page 20: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

20

Confirming indexes:

• SELECT A.index_name, A.column_name,A.column_position, B.uniqueness

FROM user_indexes B,user_ind_columns A

WHERE A.index_name = B.index_nameAND A.table_name = ‘STUDENT';

Page 21: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

21

INDEXESUSER_INDEXES: (are grouped in 4 categories)

1-IDENTIFICATION: INDEX_NAMETABLE_NAMEINDEX_TYPE

2-SPACE RELATED:TABLESPACE_NAMEINITIAL_EXTENT

3-STATISTICS RELATED:USER_STATSLAST_ANALYZED

4-OTHERS:DROPPEDJOIN_INDEX

Page 22: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

22

Removing index:

• DROP INDEX stud_index;

• When a table is dropped, the index on that table is automatically drop.

Page 23: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

23

SYNONYMS

• Access an object by creating synonyms• Refer to a table owned by another user• Shorter length object names

CREATE[PUBLIC] SYNONYM synonymFOR object;

CREATE SYNONYM ssFOR student_view;

• DROP SYNONYM ss;

Page 24: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

24

SYNONYM

SELECT SYNONYM_NAME, TABLE_OWNER, TABLE_NAME, DB_LINK

FROM USER_SYNONYMS; //SYN

USER_SYNONYMS: Very useful for debugging

Page 25: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

25

SYNONYM

SELECT DISTINCT DB_LINKFROM USER_SYNONYMSWHERE DB_LINK IS NOT NULL;

Page 26: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

26

QUESTIONS:

How many indexes to use on a table?Advantages?Disadvantages?

Page 27: Chapter Fourteen INDEX and SYNONYM Dr. Chitsaz Objectives: Create and maintain Indexes Types and applications of Indexes Create Synonym Application of

27

QUESTIONS:

Do we need all three indexes?CREATE INDEX indx_1 on student (name, id, GPA)CREATE INDEX indx_2 on student (name, id)CREATE INDEX indx_3 on student (name)