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.

CHAPTER 36 • INDEXES AND SEARCHINGwww.it-ebooks.info• Only index those columns that are required in WHERE and ORDER BY clauses. Indexingcolumns in abundance will only result in unnecessary consumption of hard drivespace, and will actually slow performance when altering table information.Performance degradation will occur on indexed tables because every time a record ischanged, the indexes must be updated.• If you create an index such as INDEX(firstname, lastname), don’t createINDEX(firstname) because <strong>MySQL</strong> is capable of searching an index prefix. However,keep in mind that only the prefix is relevant; this multiple-column index will not applyfor searches that only target lastname.• Use the attribute NOT NULL for those columns in which you plan on indexing, so thatNULL values will never be stored.• Use the --log-long-format option to log queries that aren’t using indexes. You canthen examine this log file and adjust your queries accordingly.• The EXPLAIN statement helps you determine how <strong>MySQL</strong> will execute a query, showingyou how and in what order tables are joined. This can be tremendously useful fordetermining how to write optimized queries and whether indexes should be added.Please consult the <strong>MySQL</strong> manual for more information about the EXPLAIN statement.Forms-Based SearchesThe ability to easily drill down into a web site using hyperlinks is one of the behaviors that made the Websuch a popular medium. However, as both Web sites and the Web grew exponentially in size, the abilityto execute searches based on user-supplied keywords evolved from convenience to necessity. Thissection offers several examples demonstrating how easy it is to build search interfaces for searching a<strong>MySQL</strong> database.Performing a Simple SearchMany effective search interfaces involve a single text field. For example, suppose you want to provide thehuman resources department with the ability to look up employee contact information by last name. Toimplement this task, the query will examine the lastname column found in the employees table. A sampleinterface for doing so is shown in Figure 36-1.Figure 36-1. A simple search interfaceListing 36-1 implements this interface, passing the requested last name into the search query. If thenumber of returned rows is greater than zero, each is output; otherwise, an appropriate message isoffered.702

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

Saved successfully!

Ooh no, something went wrong!