Skip to content

Instantly share code, notes, and snippets.

@yoku0825
Created February 3, 2015 05:07
Show Gist options
  • Save yoku0825/d94e1b1af0b2a794fd54 to your computer and use it in GitHub Desktop.
Save yoku0825/d94e1b1af0b2a794fd54 to your computer and use it in GitHub Desktop.
mysql56> CREATE TABLE t1 (num int, val varchar(32), key(num));
Query OK, 0 rows affected (0.03 sec)
mysql56>
mysql56> INSERT INTO t1 VALUES (1, 'one');
Query OK, 1 row affected (0.01 sec)
mysql56>
mysql56> EXPLAIN SELECT * FROM t1 WHERE num = 1;
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
| 1 | SIMPLE | t1 | ref | num | num | 5 | const | 1 | NULL |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql56> EXPLAIN SELECT * FROM t1 WHERE num = 1 ORDER BY val;
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------+
| 1 | SIMPLE | t1 | ref | num | num | 5 | const | 1 | Using where; Using filesort |
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------+
1 row in set (0.00 sec)
mysql56>
mysql56> ALTER TABLE t1 ADD UNIQUE KEY (num);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql56>
mysql56> EXPLAIN SELECT * FROM t1 WHERE num = 1 ORDER BY val;
+----+-------------+-------+-------+---------------+-------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------+---------+-------+------+-------+
| 1 | SIMPLE | t1 | const | num_2,num | num_2 | 5 | const | 1 | NULL |
+----+-------------+-------+-------+---------------+-------+---------+-------+------+-------+
1 row in set (0.00 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment