標籤: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