A blend of programming and seo

The most powerful mysql command

The most powerful command within mysql is explain. Explain can tell you exactly what mysql is doing when you execute a query and with this information, you can find slow queries and minimize execution time, which can significantly improve the speed of your web application.

How to use the explain command

Here is a simple example of its usage:

explain statement1 The most powerful mysql command

Database Schema:

(users table)

users schema The most powerful mysql command

(address table)

address schema The most powerful mysql command

In this simple example, I am selecting a record that is representing a user, based on a userid.

Here is the output:

Variable Details
explain id The most powerful mysql command The ID of this table in the query. EXPLAIN will create one output record for each table in the query.
explain select type The most powerful mysql command possible values: SIMPLE, PRIMARY, UNION, DEPENDENT UNION, SUBSELECT, and DERIVED.
explain table The most powerful mysql command The name of the table MYSQL will read the records from.
explain type The most powerful mysql command The type of join that mysql will use. Possible values: eq_ref,ref,range, index, or all.
explain possible keys The most powerful mysql command a list of indexes (or NULL if none) mysql can use to find rows in the table.
explain key The most powerful mysql command The name of the index MYSQL will use (after checking all possible indexes).
explain key len The most powerful mysql command The size of the key in bytes.
explain ref The most powerful mysql command The columns or values that are used to match against the key
explain rows The most powerful mysql command The number of rows mysql thinks it needs to examine to execute the query.
explain extra The most powerful mysql command extra information about the query

This example is pretty straight forward. Since we are peforming a search based on the primary key (userid), there can be only one record that can possible match (the rows variable is 1).

A more advanced example:

statement adv The most powerful mysql command

This query is more advanced than the first one. It is performing an inner join on the users and address table based on the userid. The userid is a primary key within the users table, but it is not an index in the address table. The output of the explain command shows us the following:

(users table)

Type: const
Possible_Keys: primary
Ref: const

(address table)

Type: all
Possible_Keys: (none)
Ref: (none)

The first table is optimized. It is using the primary key for this query. The second table, however is not optimized. The type is all and the Possible_keys=(none), which means it is going to have to go through a full table-scan. Adding an index on the user field optimizes this query.

Final output after index is added:

(users table)

Type: const
Possible_Keys: primary
Ref: const

(address table)

Type: const
Possible_Keys: primary
Ref: const

More information on this command can be found Here

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Reddit
  • Twitter
  • HackerNews
  • StumbleUpon
  • Technorati

3 comments

1 Keloran { 05.19.09 at 11:52 am }

I would have though ‘;DROP TABLES WHERE ‘1, to be much more powerful, hehe

2 matthew { 06.04.09 at 12:38 am }

I had never heard of this before, this will make my life debugging so much easier. Thank you for sharing.

3 Twitter Trackbacks for The most powerful mysql command | A blend of programming and seo [rawseo.com] on Topsy.com { 08.25.09 at 7:21 pm }

[...] The most powerful mysql command | A blend of programming and seo http://www.rawseo.com/news/2009/03/26/the-most-powerful-mysql-command – view page – cached The most powerful command within mysql is explain. Explain can tell you exactly what mysql is doing when you execute a query and with this information, you can find slow queries and minimize execution time, which can significantly improve the speed of your web application. — From the page [...]

Leave a Comment