-
SETP 1:建立測試用的 users Table
CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, password VARCHAR(255), INDEX index_name (name), PRIMARY KEY (id) ); INSERT INTO users (name,password) VALUES ("寶寶不說","123");
-
SETP 2:下 SQL 指令了解是否使用 Index
- SQL A:
EXPLAIN SELECT id FROM users WHERE name= "寶寶不說";
- SQL B:
EXPLAIN SELECT password FROM users WHERE name= "寶寶不說";
- SQL A:
-
SETP 3:建立 Coverage Index 優化 SQL B
-
先將資料庫 drop 掉:
DROP TABLE users;
-
重新建立測試資料以及「Coverage Index」
CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, password VARCHAR(255), INDEX name_password(name,password), PRIMARY KEY (id) ); INSERT INTO users (name,password) VALUES ("寶寶不說","123");
-
再次執行 SQL B:
EXPLAIN SELECT password FROM users WHERE name= "寶寶不說";
-