11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 36 • INDEXES AND SEARCHINGwww.it-ebooks.infoCREATE TABLE bookmarks (id INT UNSIGNED NOT NULL AUTO_INCREMENT,name VARCHAR(75) NOT NULL,url VARCHAR(200) NOT NULL,UNIQUE(name, url),description MEDIUMTEXT NOT NULL,PRIMARY KEY(id));Given this configuration, the following name and URL value pairs could all simultaneously reside inthe same table:Apress site, www.apress.comApress site, http://blogs.apress.comBlogs, www.apress.comApress blogs, http://blogs.apress.comHowever, attempting to insert any of these combinations more than once will result in an errorbecause duplicate combinations of name and URL are illegal.Normal IndexesYou’ll often want to optimize a database’s ability to retrieve rows based on column criteria other thanthose designated as primary or even unique. The most effective way to do so is by indexing the columnin a way that allows the database to lookup a value in the fastest way possible. These indexes aretypically called normal, or ordinary.Single-Column Normal IndexesA single-column normal index should be used if a particular column in your table will be the focus of aconsiderable number of your selection queries. For example, suppose a table containing employeeinformation consists of four columns: a unique row ID, first name, last name, and e-mail address. Youknow that the majority of the searches will be specific to either the employee’s last name or the e-mailaddress. You should create one normal index for the last name and a unique index for the e-mailaddress, like so:CREATE 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),PRIMARY KEY(id));Building on this idea, <strong>MySQL</strong> offers the feature of creating partial-column indexes, based on theidea that the first N characters of a given column often are enough to ensure uniqueness, where N isspecified within the index creation statement. Creating partial-column indexes requires less disk spaceand is considerably faster than indexing the entire column. Revisiting the previous example, you canimagine that using the first five characters of the last name suffices to ensure accurate retrieval:696

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

Saved successfully!

Ooh no, something went wrong!