透過 MySQL EXPLAIN 關鍵字獲取資訊?


我們首先建立一個表 -

mysql> create table DemoTable1541
   -> (
   -> EmployeeId int,
   -> EmployeeFirstName varchar(20) NOT NULL
   -> );
Query OK, 0 rows affected (0.94 sec)

以下是對列建立索引的查詢 -

mysql> create index emp_name_index on DemoTable1541(EmployeeFirstName);
Query OK, 0 rows affected (0.75 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用插入命令向表中插入一些記錄 -

mysql> insert into DemoTable1541 values(1,'Robert');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1541 values(2,'Adam');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1541 values(3,'Mike');
Query OK, 1 row affected (0.14 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select * from DemoTable1541;

這將生成以下輸出 -

+------------+-------------------+
| EmployeeId | EmployeeFirstName |
+------------+-------------------+
|          1 | Robert            |
|          2 | Adam              |
|          3 | Mike              |
+------------+-------------------+
3 rows in set (0.00 sec)

以下是使用 EXPLAIN 的查詢 -

mysql> explain select * from DemoTable1541 where EmployeeFirstName='Mike';

這將生成以下輸出 -

+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
| id | select_type | table         | partitions | type | possible_keys  | key            | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | DemoTable1541 | NULL       | ref  | emp_name_index | emp_name_index | 62      | const |    1 | 100.00   | NULL |
+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

更新日期:2019 年 12 月 12 日

128 次瀏覽

開啟您的職業生涯

透過完成課程取得認證

開始學習
廣告
© . All rights reserved.