Kirkwood Center for Continuing Education By Fred McClurg, Introduction to PHP and MySQL Copyright © 2015, Fred McClurg, All Rights

Embed Size (px)

DESCRIPTION

Database Performance via Indexes What is an index? Defined: Mechanism that enables a database to locate a record in row rapidly (e.g. table of contents or textbook index) Best Practice Don’ts: 1.Don’t index every column a.Indexing uses extra storage space b.Additional time is required to create indexed columns during data insert c.Too many indexes increase search time to locate record 2.Don’t index primary keys, they are already indexed 3

Citation preview

Kirkwood Center for Continuing Education By Fred McClurg, Introduction to PHP and MySQL Copyright 2015, Fred McClurg, All Rights Reserved. Chapter Nine Database Tuningslides/chapter09e.performance.ppt (Improving Performance) 2 Database Performance via Indexes What is an index? Defined: Mechanism that enables a database to locate a record in row rapidly (e.g. table of contents or textbook index) Best Practice Donts: 1.Dont index every column a.Indexing uses extra storage space b.Additional time is required to create indexed columns during data insert c.Too many indexes increase search time to locate record 2.Dont index primary keys, they are already indexed 3 Database Performance via Index Selection What to index: 1.Columns in a WHERE clause SELECT * FROM authors WHERE author = 'Max Lucado'; WHERE author = 'Max Lucado'; // author should be indexed 2.Columns in an ORDER BY clause SELECT * FROM contacts ORDER BY person; ORDER BY person; // person should be indexed 3.Columns in MIN and MAX clauses SELECT MAX(elevation) FROM mountain; // elevation should be indexed 4 Database Performance via Index Creation Creating an Index Indexing upon table creation: CREATE TABLE employee ( id INT NOT NULL id INT NOT NULL AUTO_INCREMENT, // id AUTO_INCREMENT, // id name VARCHAR(11), name VARCHAR(11), INDEX name_ix(name) ); INDEX name_ix(name) ); Indexing an existing table: CREATE INDEX name_ix ON employee(name); employee(name); 5 Database Performance via Unique Index Creating an Unique Index Unique Index upon table creation: CREATE TABLE employee ( id INT NOT NULL id INT NOT NULL AUTO_INCREMENT, AUTO_INCREMENT, ss_number VARCHAR(11), ss_number VARCHAR(11), UNIQUE ss_uq(ss_number) ); UNIQUE ss_uq(ss_number) ); Unique Index for an existing table: CREATE UNIQUE INDEX ss_uq ON employee(ss_number); employee(ss_number); 6 Database Performance via Multiple Index Multiple Column Index Description: Unlike primary keys, an index can be applied to multiple columns. Multi-column unique key, for example, could assure that the combination of two columns is unique. Example: CREATE UNIQUE INDEX state_uq ON location(city, state); location citystate ManhattanKS ManhattanNY 7 Query Performance (Before Indexing) Explain: Analyzes the efficiency of the SQL statement. Example: EXPLAIN SELECT * FROM author WHERE name = 'John MacArthur'; idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra 1SIMPLEauthorALLNULL 7 Using where Explain Explained: type: ALL means every record is scanned to determine match. possible_keys: NULL means no index defined. key: NULL means no key is used by query. rows: Number of rows searched for query (there are 7 records in DB). 8 Query Performance (After Indexing) Example: ALTER TABLE author ADD CONSTRAINT name_uq UNIQUE(name); DESCRIBE author; EXPLAIN SELECT * FROM author WHERE name = 'John MacArthur'; FieldTypeNullKeyDefaultExtra idint(11)NOPRINULLauto_increment namevarchar(40)NOUNINULL idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra 1SIMPLEauthorconstname_uq 42const1 9 to be continued...slides/chapter09f.administration.pptslides/chapter09f.administration.ppt10