13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

302 Chapter 12 Advanced <strong>MySQL</strong> Administration<br />

Table 12.9<br />

Type<br />

Continued<br />

Description<br />

index_merge<br />

unique_subquery<br />

index_subquery<br />

range<br />

index<br />

ALL<br />

A specific optimization, the Index Merge, has been used.<br />

This join type is used to replace ref for some IN subqueries where<br />

one unique row is returned.<br />

This join type is similar to unique_subquery but is used for indexed<br />

nonunique subqueries.<br />

For every set of rows from the other tables in the join, you read a set of<br />

table rows that fall into a particular range.<br />

The entire index is scanned.<br />

Every row in the table is scanned.<br />

In the previous example, you can see that one of the tables is joined using eq_ref<br />

(books), one is joined using ref (order_items), <strong>and</strong> the other two (orders <strong>and</strong> customers)<br />

are joined using ALL—that is, by looking at every single row in the table.<br />

The rows column backs this up: It lists (roughly) the number of rows of each table<br />

that has to be scanned to perform the join.You can multiply these numbers together to<br />

get the total number of rows examined when a query is performed.You multiply these<br />

numbers because a join is like a product of rows in different tables. Check out Chapter<br />

10,“Working with Your <strong>MySQL</strong> Database,” for details. Remember that this is the<br />

number of rows examined, not the number of rows returned, <strong>and</strong> that it is only an estimate;<br />

<strong>MySQL</strong> can’t know the exact number without performing the query.<br />

Obviously, the smaller you can make this number, the better. At present, you have a<br />

negligible amount of data in the database, but when the database starts to increase in size,<br />

this query would increase in execution time.We return to this matter shortly.<br />

The possible_keys column lists, as you might expect, the keys that <strong>MySQL</strong> might<br />

use to join the table. In this case, you can see that the possible keys are all PRIMARY keys.<br />

The key column is either the key from the table <strong>MySQL</strong> actually used or NULL if no<br />

key was used. Notice that, although there are possible PRIMARY keys for the customers<br />

<strong>and</strong> orders table, none were used in this query.<br />

The key_len column indicates the length of the key used.You can use this number<br />

to tell whether only part of a key was used.The key length is relevant when you have<br />

keys that consist of more than one column. In this case, where the keys were used, the<br />

full key was used.<br />

The ref column shows the columns used with the key to select rows from the table.<br />

Finally, the Extra column tells you any other information about the way the join was<br />

performed. Some possible values you might see in this column are shown in Table 12.10.<br />

For a complete list of the more than 15 different possibilities, see the <strong>MySQL</strong> Manual at<br />

http://dev.mysql.com/doc/refman/5.1/en/using-explain.html.

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

Saved successfully!

Ooh no, something went wrong!