Schemas Tables Indexes Views and JoOracle Objects ins

download Schemas Tables Indexes Views and JoOracle Objects  ins

of 24

Transcript of Schemas Tables Indexes Views and JoOracle Objects ins

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    1/24

    Schema

    Tables

    Indexes

    Views

    Joins

    Oracle Objects

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    2/24

    Schema A schema is a collection of database objects like tables, indexes, sequences, procedures,

    functions, packages, etc.

    A schema is created when a user is created and dropped when the user is dropped.

    Each user has only one schema and a schema is owned by only one user. ie. There is one-to-one

    mapping between schemas and users.

    The schema is named after the user who owns it.

    Users can access objects across the schemas if the privileges are granted.

    Users objects are stored entirely in side a single schema and never span across schemas.

    Tables

    Indexes

    Sequences

    Views

    procedures

    Packages

    Functions

    Schema A

    Tables

    Indexes

    Sequences

    Views

    procedures

    Packages

    Functions

    Schema B

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    3/24

    Different types of Tables

    Heap

    Index Organized

    Clustered

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    4/24

    ROW ID 1 FORD Marketing Mumbai

    ROW ID 2 JONES Manufacturing Chennai

    ROW ID 3 MILLER Finance Chennai

    ROW ID 4 JAMES Manufacturing Mumbai

    ROW ID 5 BLAKE Marketing Chennai

    ROW ID 6 ADAMS Manufacturing Mumbai

    ROW ID 7 SCOTT Manufacturing Hyderabad

    ROW ID 8 SMITH Finance Chennai

    ROW ID 9 TURNER Administration Mumbai

    ROW ID 10 KING Manufacturing Mumbai

    ROW ID 11 ALLEN Finance Chennai

    ROW ID 12 MARTIN Manufacturing Hyderabad

    ROW ID 13 WARD Marketing Mumbai

    ROW ID 14 CLARK Manufacturing Chennai

    Structure of Heap Table

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    5/24

    Row ID 6 ADAMS Manufacturing Mumbai

    Row ID 7 ALLEN Finance Chennai

    Row ID 8 BLAKE Marketing Chennai

    Row ID 9 CLARK Manufacturing Chennai

    Row ID 12 FORD Marketing Mumbai

    Row ID 13 JAMES Manufacturing Chennai

    Row ID 14 JONES Manufacturing Chennai

    Row ID 15 KING Manufacturing Chennai

    Row ID 45 MARTIN Manufacturing Hyderabad

    Row ID 46 MILLER Finance Chennai

    Row ID 47 SCOTT Manufacturing Hyderabad

    Row ID 67 SMITH Finance Chennai

    Row ID 68 TURNER Administration Mumbai

    Row ID 70 WARD Marketing Mumbai

    Structure of Index Organized Table

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    6/24

    Structure of Clustered Vs Non-Clustered Table

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    7/24

    Different types of Index

    B-Tree

    Bitmap

    Functional

    Reverse

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    8/24

    ADAMS ROW ID 6

    ALLEN ROW ID 11

    BLAKE ROW ID 5

    CLARK ROW ID 14ADAMS Block 3

    FORD Block 6

    Structure of B-Tree Index

    ADAMS Block 1MARTIN Block 2

    MARTIN Block 5

    SMITH Block 4

    FORD ROW ID 1

    JAMES ROW ID 4

    JONES ROW ID 2

    KING ROW ID 10

    MARTIN ROW ID 12

    MILLER ROW ID 3

    SCOTT ROW ID 7

    SMITH ROW ID 8

    TURNER ROW ID 9

    WARD ROW ID 13Branch Block

    Leaf Block

    Root Block

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    9/24

    Chennai201400

    1010101010101111111111111000000011000000

    00000000001110001111

    11111111111000011111

    11001111111111111111

    10101010101011111111

    11111000000011000000

    00000000001110001111

    11111111111000011111

    11001111111111111111

    Structure of Bitmap Index

    Mumbai201400

    0100010001010000000000000111111100111111

    11100000110001110000

    00000000000111100000

    00110000000000000000

    01010101010100000000

    00000111111100111111

    11111111110001110000

    00000000000111100000

    00110000000000000000

    Column

    value

    Starting

    RowID

    Ending

    RowID

    Bitmap of

    Column

    values

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    10/24

    adams ROW ID 6

    allen ROW ID 11

    blake ROW ID 5

    clark ROW ID 14adams Block 3

    ford Block 6

    Structure of Function Based Index

    adams Block 1martin Block 2

    martin Block 5

    smith Block 4

    ford ROW ID 1

    james ROW ID 4

    jones ROW ID 2

    king ROW ID 10

    martin ROW ID 12

    miller ROW ID 3

    scott ROW ID 7

    smith ROW ID 8

    turner ROW ID 9

    ward ROW ID 13

    Branch Block

    Leaf Block

    Root Block

    Function based index on

    lower(name)

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    11/24

    196 Block 3

    200 Block 6

    196 ROW ID 12

    197 ROW ID 3

    198 ROW ID 7

    199 ROW ID 76

    200 ROW ID 8

    201 ROW ID 9

    202 ROW ID 13

    203 ROW ID 19

    196 Block 1

    Normal Index

    Reverse key Index Compared with Normal Index

    Normal Reverse

    196 691197 791

    198 891

    199 991

    200 002

    201 102

    202 202

    203 302

    002 ROW ID 8

    051 ROWID 201

    090 ROWID 11

    102 ROW ID 19002 Block 3

    691 Block 6002 Block 1

    200 ROW ID 120

    202 ROW ID 43

    350 ROW ID 27

    Reverse key Index

    691 ROW ID 12

    695 ROWID 2

    Normal key converted

    to Reverse key

    Note:- Reverse key indexes are used to prevent index blocks become hotspot. ie. Normal

    index stores consecutive values adjacently. In reverse key index the values are reversed to

    scatter while storing. So, reverse key index does not support range scans.

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    12/24

    Indexing

    Consider indexing keys that are used frequently in WHERE

    clauses. Consider indexing keys that are used frequently to join tables in

    SQL statements

    Index keys that have high selectivity. The selectivity of an index

    is the percentage of rows in a table having the same value for theindexed key.

    Consider indexing foreign keys of referential integrity

    constraints

    Do not modify indexed columns with functions such as RTRIM,TO_CHAR, UPPER, TRUNC as this will disable the index.

    Instead perform the modification on the constant side of the

    condition.

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    13/24

    Composite IndexWhen?

    Improved selectivity

    Two or more columns or expressions, each withpoor selectivity, can be combined to form a composite

    index with higher selectivity.

    Reduced I/O

    If all columns selected by a query are in acomposite index, then Oracle can return these valuesfrom the index without accessing the table.

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    14/24

    INDEX - Facts

    Too many indexes will slowdown data manipulation.

    Unique indexes are more efficient than non-unique indexes.

    NULL values are not indexed in B-tree but in bitmap.

    Index based access are not always fast.

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    15/24

    What is a View?

    A view is stored query that may refer one or more of the table

    column and table rows.

    The view can be used to ease the work of the programmer /user.

    The view can be used to secure the data.

    EMPNO ENAME JOB SAL

    ----- ------ ------- -----

    7788 SCOTT ANALYST 3000

    7902 FORD ANALYST 3000

    7369 SMITH CLERK 800

    7876 ADAMS CLERK 1100

    7934 MILLER CLERK 1300

    7900 JAMES CLERK 950

    7566 JONES MANAGER 2975

    7782 CLARK MANAGER 2450

    7698 BLAKE MANAGER 2850

    Select empno, ename

    From emp

    Where job=CLERK

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    16/24

    SQL> select * from emp;

    EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO

    ----- -------- --------- ----- --------- ----- ------

    7369 SMITH CLERK 7902 17-DEC-80 800 20

    7499 ALLEN SALESMAN 7698 20-FEB-81 1600 30

    7566 JONES MANAGER 7839 02-APR-81 2975 20

    7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30

    7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

    7788 SCOTT ANALYST 7566 19-APR-87 3000 20

    7839 KING PRESIDENT 17-NOV-81 5000 10

    7844 TURNER SALESMAN 7698 08-SEP-81 1500 30

    7876 ADAMS CLERK 7788 23-MAY-87 1100 20

    7900 JAMES CLERK 7698 03-DEC-81 950 30

    7934 MILLER CLERK 7782 23-JAN-82 1300 10

    ViewSample table

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    17/24

    SQL> create view clerk_listasSelect empno, enameFrom empWhere job='CLERK' ;

    SQL> select * from clerk_list;EMPNO ENAME

    ---------- ----------7369 SMITH7876 ADAMS7934 MILLER7900 JAMES

    SQL> select * from clerk_list where ename like 'S%';EMPNO ENAME

    ---------- ----------7369 SMITH

    ViewExample

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    18/24

    Nested Loops

    Sort Merge

    Hash Join

    JOINS - Methods

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    19/24

    Architecture The profession of designing buildings and environments

    with consideration for their esthetic effectCarex Large genus of plants found in damp woodlands and

    bogs and ditches or at water margins.

    Integrity Moral soundness; honesty; freedom from corrupting

    influence or motive;

    Intellect the power to judge and comprehend; the thinking

    faculty; the understanding.

    Linux An open-source version of the UNIX operating system

    Oracle The communications, revelations, or messages

    delivered by God to the prophets; also, the entiresacred Scriptures.

    Polarimeter An instrument for determining the amount of

    polarization of light, or the proportion of polarized light,

    in a partially polarized ray.

    PolarisThe polestar. See North star, under North.

    Polariscope An instrument consisting essentially of a polarizer and

    an analyzer, used for polarizing light, and analyzing its

    properties.

    Steeple A tall tower that forms the superstructure of a building

    Nested Loop

    select ox.*

    from my_words mw,

    oxford oxwhere mw.word=ox.word ;

    my_words (Driving table) oxford (Driven table)

    The small table is taken as driving table.

    The bigger indexed table is taken as the driven table

    PolarisOracle

    Linux

    Steeple

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    20/24

    Polaris RowID 5

    Infosys RowID 20Wipro RowID 7

    Infosys RowID 20

    Polaris RowID 5

    Wipro RowID 7

    Polaris RowID 3

    Wipro RowID 37

    Oracle RowID 202

    IBM RowID 34

    IBM RowID 34

    Oracle RowID 202

    Polaris RowID 3

    Wipro RowID 37

    IBM RowID 34 Table B

    Infosys RowID 20 Table A

    Oracle RowID 202 Table B

    Polaris RowID 5 Table A

    Polaris RowID 3 Table B

    Wipro RowID 7 Table A

    Wipro RowID 37 Table B

    Sort Merge

    Sorting Sorting

    Merging

    Table A Table B

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    21/24

    Apply Hash

    Function to

    Build Hash

    Table

    Big Table

    Small Table

    Hash Join

    Hash Table

    Apply

    Hash

    Function

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    22/24

    Joins

    Nested loops usually offer better response time. Effective onlywhen the index is available and can be used.

    The larger the subset of rows being processed, the more likelythat a sort merger or hash join will be faster.

    Recommendations:

    Both tables A and B are smallsort merge/hash

    Only selecting a small sub-set of rows from Bnested loops Want the first row as quickly as possiblenested loops

    Getting rows from A by an index lookupsort merge/hash

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    23/24

    Choosing Join Plans

    CBO decides the following for all the joins:

    Access path

    Full scan, Index range scan, Index fast full scan, etcJoin Method

    Nested loop, Sort merge, Hash join

    Join Order

    Which table will drive the join process

  • 8/22/2019 Schemas Tables Indexes Views and JoOracle Objects ins

    24/24

    Thank You