CREATE TABLE `idxitest` ( `id` int (10) UNSIGNED NOT NULL AUTO_INCREMENT, `a` int (11) NOT NULL, `b` int (11) NOT NULL, PRIMARY KEY (`id`), KEY `a` (` a`) ) ENGINE = InnoDB AUTO_INCREMENT = 6029313 DEFAULT CHARSET = latin1 mysql> SELECT count (*) FROM idxitest WHERE a = 5 AND b = 5; + ---------- + | count (*) | + ---------- + | 60434 | + ---------- + 1 row IN SET (0.69 sec) mysql> EXPLAIN SELECT count (*) FROM idxitest WHERE a = 5 AND b = 5; + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra | + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + | 1 | SIMPLE | idxitest | ref | a | a | 4 | const | 707820 | USING WHERE | + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + 1 row IN SET (0.00 sec)
mysql> ALTER TABLE idxitest DROP KEY a, ADD KEY (a, b);
Query OK, 0 rows affected (24.84 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT count (*) FROM idxitest WHERE a = 5 AND b = 5;
+ ---------- +
| count (*) |
+ ---------- +
| 60434 |
+ ---------- +
1 row IN SET (0.02 sec)
mysql> EXPLAIN SELECT count (*) FROM idxitest WHERE a = 5 AND b = 5;
+ ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------------- + -------- + ------ ------- +
| id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra |
+ ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------------- + -------- + ------ ------- +
| 1 | SIMPLE | idxitest | ref | a | a | 8 | const, const | 120640 | USING INDEX |
+ ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------------- + -------- + ------ ------- +
1 row IN SET (0.00 sec)
mysql> SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + --------- + ----- + --- + | id | a | b + --------- + ----- + --- + | 3000000 | 100 | 7 + --------- + ----- + --- + 1 row IN SET (0.00 sec) mysql> EXPLAIN SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + | 1 | SIMPLE | idxitest | ref | a | a | 4 | const | 126074 | USING WHERE + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + -------- + ------------ - + 1 row IN SET (0.00 sec)
mysql> SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + --------- + ----- + --- + | id | a | b + --------- + ----- + --- + | 3000000 | 100 | 7 + --------- + ----- + --- + 1 row IN SET (1.01 sec) mysql> EXPLAIN SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + | 1 | SIMPLE | idxitest | INDEX | a | PRIMARY | 4 | NULL | 36 | USING WHERE + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + 1 row IN SET (0.00 sec) # The plan also can look something like this: mysql> EXPLAIN SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + ------ + -------------- ---------------------------- + | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + ------ + -------------- ---------------------------- + | 1 | SIMPLE | idxitest | ref | a | a | 4 | const | 1 | USING WHERE; USING INDEX; USING filesort + ---- + ------------- + ---------- + ------ + ------------ --- + ------ + --------- + ------- + ------ + -------------- ---------------------------- + 1 row IN SET (0.01 sec)
CREATE TABLE `idxitest` ( `id` int (10) UNSIGNED NOT NULL AUTO_INCREMENT, `a` int (11) NOT NULL, `b` int (11) NOT NULL, PRIMARY KEY (`id`), KEY `a` (` a`), KEY `a_2` (` a`, `b`) ) ENGINE = InnoDB AUTO_INCREMENT = 6029313 DEFAULT CHARSET = latin1 mysql> SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + --------- + ----- + --- + | id | a | b + --------- + ----- + --- + | 3000000 | 100 | 7 + --------- + ----- + --- + 1 row IN SET (1.03 sec) mysql> EXPLAIN SELECT * FROM idxitest WHERE a = 100 ORDER BY id DESC LIMIT 1; + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + | 1 | SIMPLE | idxitest | INDEX | a, a_2 | PRIMARY | 4 | NULL | 2247 | USING WHERE + ---- + ------------- + ---------- + ------- + ----------- ---- + --------- + --------- + ------ + ------ + ----------- - + 1 row IN SET (0.00 sec)
Source: https://habr.com/ru/post/99310/
All Articles