200 likes | 822 Views
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. Employee ( EID (auto-increment), Age, Salary)
E N D
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
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.
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
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
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
Index Structure Examples Index on the primary key field
Index Structure Examples A search tree
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;
8 tabless_100_none s_100_btrees_1000_none s_1000_btrees_10000_none s_10000_btrees_100000_none s_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);
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</br>"; }
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’;
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';