Index & Query Optimization

download Index & Query Optimization

of 21

Transcript of Index & Query Optimization

  • 8/10/2019 Index & Query Optimization

    1/21

    DBMS

    Index and uer

    1

    Optimization

    Abel M. Ingaw

    Ronald T. Bragas

  • 8/10/2019 Index & Query Optimization

    2/21

    Index: a data structure that improves the speed of

    data retrieval operations

    Definition of Terms

    2

    ,

    contains only the field/s a user is interested in

    sorting on

    Indexing: a data structure, which orders data of

    one or more columns in database table

  • 8/10/2019 Index & Query Optimization

    3/21

    defined based on its indexing attributes

    can be one of the following types

    Primary Index: If index is built on ordering

    'key-field' of file

    Indexing

    3

    Secondary Index: If index is built on non-

    ordering field of file

    Clustering Index: If index is built on ordering

    non-key field of file

  • 8/10/2019 Index & Query Optimization

    4/21

    Dense Index: there is an index record for every

    search key value in the database

    Sparse Index: index records are not created

    for every search key. An index record here

    Ordered Indexing

    4

    contains search key and actual pointer to the

    data on the disk

    Clustering Index: If index is built on orderingnon-key field of file

  • 8/10/2019 Index & Query Optimization

    5/21

    Multilevel Index:

    Index records are comprised of search-key

    value and data pointers. This index itself is

    Ordered Indexing

    5

    s ore on e s a ong w e ac ua

    database files.

  • 8/10/2019 Index & Query Optimization

    6/21

    Part 2

    6

    uery p m za on

  • 8/10/2019 Index & Query Optimization

    7/21

    Query:

    is a re uest or in ormation rom a database

    Query Optimization

    7

  • 8/10/2019 Index & Query Optimization

    8/21

  • 8/10/2019 Index & Query Optimization

    9/21

    Aims to choose a query which minimizes the usage

    of resources

    Query Optimization

    9

    Helps to find the near optimum solution

  • 8/10/2019 Index & Query Optimization

    10/21

    Scenario:

    Table Name: employee

    Columns:

    Query Optimization

    10

    employee_number, firstname, surname, address ,

    tel_no varchar, salary,overtime_rate

  • 8/10/2019 Index & Query Optimization

    11/21

    Example 1:

    Find employee Fred Jone's salary(employee number

    101832)

    Query Optimization

    11

    SQL:

    SELECT salary FROM employee WHEREemployee_number = '101832';

  • 8/10/2019 Index & Query Optimization

    12/21

    Problem:

    No clue where to find this record

    Solution:

    Query Optimization

    12

    SELECT employee_number, firstname, surname FROM

    employee WHERE employee_number= '10875';

  • 8/10/2019 Index & Query Optimization

    13/21

    Example 2:

    What about if you want to select on more than one

    criteria?

    Query Optimization

    13

    SQL 1:

    SELECT firstname FROM employee

  • 8/10/2019 Index & Query Optimization

    14/21

    Problem:

    makes no use of an index at all. An index on firstname

    is useless

    Query Optimization

    14

    o ut on:

    SELECT firstname FROM employee WHERE

    surname="Madida";

  • 8/10/2019 Index & Query Optimization

    15/21

    Example 3:

    find all the employees where half their overtime rate is

    less than 20 pesos

    Query Optimization

    15

    SQL 1:

    SELECT firstname FROM employee WHEREovertime_rate

  • 8/10/2019 Index & Query Optimization

    16/21

    Problem:

    Every single employee record is being read

    School Algebra to the rescue:

    Query Optimization

    16

    x/2 = y' is the same as 'x = y*2

  • 8/10/2019 Index & Query Optimization

    17/21

    Solution:

    Add an index on overtime_rate

    SELECT firstname FROM employee WHERE

    Query Optimization

    17

    overtime_rate/2

  • 8/10/2019 Index & Query Optimization

    18/21

    Example 4:

    Having thousands of records, delete all records as fast

    as possible.

    Query Optimization

    18

    SQL 1:

    DELETE FROM employee

  • 8/10/2019 Index & Query Optimization

    19/21

    Problem:

    DELETE drops records one by one

    Solution:

    Query Optimization

    19

    TRUNCATE TABLE employee

    TRUNCATE employee

  • 8/10/2019 Index & Query Optimization

    20/21

    Conclusion

    It's not only getting the data in that

    20

    needs to be quick - sometimes you

    need to get it out quickly too

  • 8/10/2019 Index & Query Optimization

    21/21