11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 36 • INDEXES AND SEARCHINGCREATE TABLE employees (id INT UNSIGNED NOT NULL AUTO_INCREMENT,firstname VARCHAR(100) NOT NULL,lastname VARCHAR(100) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE,INDEX (lastname(5)),PRIMARY KEY(id));Often, however, selection queries are a function of including multiple columns. After all, morecomplex tables might require a query consisting of several columns before the desired data can beretrieved. Run time on such queries can be decreased greatly through the institution of multiple-columnnormal indexes.Multiple-Column Normal IndexesMultiple-column indexing is recommended when you know that a number of specified columns willoften be used together in retrieval queries. <strong>MySQL</strong>’s multiple-column indexing approach is based upona strategy known as leftmost prefixing. Leftmost prefixing states that any multiple-column indexincluding columns A, B, and C will improve performance on queries involving the following columncombinations:• A, B, C• A, B• AHere’s how you create a multiple-column <strong>MySQL</strong> index:CREATE TABLE employees (id INT UNSIGNED NOT NULL AUTO_INCREMENT,lastname VARCHAR(100) NOT NULL,firstname VARCHAR(100) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE,INDEX name (lastname, firstname),PRIMARY KEY(id));This creates two indexes (in addition to the primary key index). The first is the unique index for thee-mail address. The second is a multiple-column index, consisting of two columns, lastname andfirstname. This is useful because it increases the search speed when queries involve any of the followingcolumn combinations:• lastname, firstname• lastnameDriving this point home, the following queries would benefit from the multiple-column index:SELECT email FROM employees WHERE lastname="Geronimo" AND firstname="Ed";SELECT lastname FROM employees WHERE lastname="Geronimo";697

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!