Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Indexing 1 After this lecture, you should be able to:  Understand why we need database indexing.  Define indexes for your tables in MySQL. 

Similar presentations


Presentation on theme: "Database Indexing 1 After this lecture, you should be able to:  Understand why we need database indexing.  Define indexes for your tables in MySQL. "— Presentation transcript:

1 Database Indexing 1 After this lecture, you should be able to:  Understand why we need database indexing.  Define indexes for your tables in MySQL.  See the performance improvement of Indexing over No-Indexing. Database Indexing

2 2 Employee (EID(auto-increment), Age, Salary)  Easy to retrieve employees by EID in increasing (or decreasing) order.  Difficult to retrieve all employees who are 55 years old (age) -> scan the entire table file. One Example… Solution: Create an index on age field -> the same idea with book index.

3 Database Indexing 3  Database indexing is a technique to help efficiently access a collection of records in multiple ways.  A database index is a data structure that improves the speed of operations on a database table.  Indexes can be created using one or more columns of a database table.  Note: in MySQL, a primary key column is automatically indexed for efficiency. Database Indexing Overview

4 Database Indexing 4  Table is stored in a file.  File of records is a collection of records that may reside on several pages.  Record id (rid) is sufficient to physically locate record.  Pages (Data Blocks) (4KB or 8KB) are stored on disk.  Indexes are data structures that allow us to find the record ids of records with given values in index search key fields. Data on External Storage

5 Database Indexing 5  Indexes: Data structures to organize records via trees or hashing.  Like sorted files, they speed up searches for a subset of records, based on values in certain (“search key”) fields  Updates are much faster than in sorted files.  Examples of indexing techniques: ordered files, B+ trees, hash based structures.  Typically, index contains auxiliary information (access method) that directs searches to the desired data entries. Indexes

6 Database Indexing 6 Index Structure Examples Index on the primary key field

7 Database Indexing 7 Index Structure Examples A search tree

8 Database Indexing 8 Employee (EID, Age, Salary)  Difficult to retrieve all employees who are 55 years old (age) -> scan the entire table file.  Solution: Create an index on age field -> the same idea with book index. Defining Index in MySQL create index emp_age_idx using btree on Employee(Age); drop index emp_age_idx on Employee;

9 Database Indexing 9  8 tables s_100_nones_100_btree s_1000_nones_1000_btree s_10000_nones_10000_btree s_100000_nones_100000_btree  Table s_10000_btree (sno,sname,status,city): contains 10000 records and have a B-tree index on sname field. An experiment with database index create index s_10000_btree_sname_idx using btree on s_10000_btree(sname);

10 Database Indexing 10 Populating data $chars = "abcdefghijkmnopqrstuvwxyz023456789"; for ($i = 0; $i < 10000; $i++) { $sname = substr(str_shuffle($chars), 0, 15); $status = rand(1, 2000); $city = substr(str_shuffle($chars), 0, 10); $query = "Insert into s_10000_btree (sname, status, city) values ('$sname', $status, '$city')"; $result = mysql_query($query); echo "Executing: $query - result: $result "; }

11 Database Indexing 11 Random Searches - Queries select SQL_NO_CACHE * from s_100_none where sname = 'abcdefghijkmnop‘; … select SQL_NO_CACHE * from s_100000_none where sname = 'abcdefghijkmnop‘; /* */ select SQL_NO_CACHE * from s_100_btree where sname = 'abcdefghijkmnop' select SQL_NO_CACHE * from s_100000_btree where sname = 'abcdefghijkmnop’;

12 Database Indexing 12 Random Searches - Result

13 Database Indexing 13 Sequential Searches - Queries … select SQL_NO_CACHE max(sname) from s_100000_none where sname > 'abcdefghijkmnop' and sname < 'rmo8s5jh4ciyxfe'; /* */ … select SQL_NO_CACHE max(sname) from s_100000_btree where sname > 'abcdefghijkmnop' and sname < 'rmo8s5jh4ciyxfe';

14 Database Indexing 14 Sequential Searches - Result


Download ppt "Database Indexing 1 After this lecture, you should be able to:  Understand why we need database indexing.  Define indexes for your tables in MySQL. "

Similar presentations


Ads by Google