mysql的explain

來源:互聯網
上載者:User

標籤:com   poi   import   聯集查詢   語句   瞭解   copy   syntax   imp   

explain 一般用於分析sql.  
如下

[SQL] 純文字查看 複製代碼?
010203040506070809101112131415161718192021222324252627282930 drop table if exists test1;CREATE TABLE test1 (    id INT NOT NULL primary key  auto_increment, -- 自動遞增    username varchar(5) not null -- 使用者名稱) ENGINE=innodb ; insert into test1 (username) values (concat(‘test‘,rand()));insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;insert into test1 (username)   select username from test1;


先建立一部分資料. 然後執行

[SQL] 純文字查看 複製代碼?
1 explain select * from test1;


結果

explain select username from test1;
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
|  1 | SIMPLE      | test1 | ALL  | NULL          | NULL | NULL    | NULL | 4187248 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.00 sec)


再執行

[SQL] 純文字查看 複製代碼?
1 explain select username from test1 where username = ‘test‘;

 

+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | test1 | ALL  | NULL          | NULL | NULL    | NULL | 4187248 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)


接下來再給表的username建立一個索引

[SQL] 純文字查看 複製代碼?
1 ALTER TABLE `test1` ADD INDEX `i_name` (`username`) comment ‘‘;

   過程 會比較 卡.需要等待幾秒到幾十秒.取決於機器. 也可以少插入幾條資料. 然後再執行剛才的兩條語句

mysql> explain select username from test1 where username = ‘test‘;
+----+-------------+-------+------+---------------+--------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key    | key_len | ref   | rows | Extra                    |
+----+-------------+-------+------+---------------+--------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | test1 | ref  | i_name        | i_name | 7       | const |    1 | Using where; Using index |
+----+-------------+-------+------+---------------+--------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select username from test1;
+----+-------------+-------+-------+---------------+--------+---------+------+---------+-------------+
| id | select_type | table | type  | possible_keys | key    | key_len | ref  | rows    | Extra       |
+----+-------------+-------+-------+---------------+--------+---------+------+---------+-------------+
|  1 | SIMPLE      | test1 | index | NULL          | i_name | 7       | NULL | 4187248 | Using index |
+----+-------------+-------+-------+---------------+--------+---------+------+---------+-------------+
1 row in set (0.00 sec)


發現 type 變了   key也變了.  extra也不一樣了. 對比一下就瞭解情況

其中,

type=const表示通過索引一次就找到了;

key=primary的話,表示使用了主鍵;

type=all,表示為全表掃描;

key=null表示沒用到索引。
type=ref,因為這時認為是多個匹配行,在聯集查詢中,一般為REF。

經過分析後就可以判斷出哪些欄位需要建 索引,哪些條件可以最佳化等. 以及條數等



system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

mysql的explain

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.