1.使用explain查询select查询语句执行计划
mysql> select * from baba where name =‘fjdsjf‘;
+------+--------+
| id | name |
+------+--------+
| 1 | fjdsjf |
+------+--------+
查询该sql语句的执行计划
mysql> explain select * from baba where name =‘fjdsjf‘ \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: baba
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2
Extra: Using where
1 row in set (0.00 sec) //该条sql语句没有走索引
增加索引:
mysql> create index index_baba on baba(name);
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc baba;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> explain select * from baba where name =‘fjdsjf‘ \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: baba
type: ref
possible_keys: index_baba
key: index_baba
key_len: 33
ref: const
rows: 1 //搜索到一行,就找到所
Extra: Using where
2. 学会使用执行计划帮助
mysql> help explain
Name: ‘EXPLAIN‘
Description:
Syntax:
EXPLAIN [explain_type] SELECT select_options
explain_type:
EXTENDED
| PARTITIONS
Or:
EXPLAIN tbl_name
The EXPLAIN statement can be used either as a way to obtain information
about how MySQL executes a statement, or as a synonym for DESCRIBE:
o When you precede a SELECT statement with the keyword EXPLAIN, MySQL
displays information from the optimizer about the query execution
plan. That is, MySQL explains how it would process the statement,
including information about how tables are joined and in which order.
EXPLAIN EXTENDED can be used to obtain additional information.
For information about using EXPLAIN and EXPLAIN EXTENDED to obtain
query execution plan information, see
http://dev.mysql.com/doc/refman/5.1/en/using-explain.html.
本文出自 “技术在手,天下我有” 博客,请务必保留此出处http://xin521long.blog.51cto.com/11884590/1882496
原文地址:http://xin521long.blog.51cto.com/11884590/1882496