Can someone explain EXPLAIN?

Discussion in 'MySQL' started by mz906, Jan 22, 2009.

  1. #1
    Ok, so maybe i'm missing something, but can someone please explain WHAT i'm looking for when i do an EXPLAIN? i mean should it be used as a tool to optimize your query? or just a better understanding of what is going on?

    below is my code, that generates a post title, id, author name, date created and category name

    mysql> explain select c.id, c.title, s_name, c.created, cat_name
        -> from content as c
        -> inner join cat_relationship as cr on cr.content_id = c.id
        -> inner join category as cat on cr.cat_id = cat.id
        -> inner join user as u on c.user_id = u.id
        -> order by cat_name asc;
    +----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+----------------------------------------------+
    | id | select_type | table | type   | possible_keys | key     | key_len | ref                       | rows | Extra                                        |
    +----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+----------------------------------------------+
    |  1 | SIMPLE      | cr    | index  | PRIMARY       | PRIMARY |       8 | NULL                      |    3 | Using index; Using temporary; Using filesort |
    |  1 | SIMPLE      | c     | eq_ref | PRIMARY       | PRIMARY |       4 | db20671_cms.cr.content_id |    1 |                                              |
    |  1 | SIMPLE      | cat   | eq_ref | PRIMARY       | PRIMARY |       4 | db20671_cms.cr.cat_id     |    1 |                                              |
    |  1 | SIMPLE      | u     | eq_ref | PRIMARY       | PRIMARY |       4 | db20671_cms.c.user_id     |    1 |                                              |
    +----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+----------------------------------------------+
    4 rows in set (0.01 sec)
    Code (markup):
     
    mz906, Jan 22, 2009 IP
  2. Zytran

    Zytran Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
  3. Gangsta

    Gangsta Active Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    it explain your the proccess of query
    for example your output tell that mysql first of all selected 3 rows from table cr
    than 1 row from c
    than 1 row from cat
    than 1 row from u
     
    Gangsta, Jan 22, 2009 IP