I ran into an interesting issue today, while examining some EXPLAIN outputs, and wanted to share the findings, as some of this is undocumented.
Basically, you can start with a very simple InnoDB table – 2 INT columns, Primary Key (PK) on the 1st column, regular index on the 2nd:
CREATE TABLE `t1` ( `id1` int(10) unsigned NOT NULL, `id2` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id1`), KEY `id2` (`id2`) ) ENGINE=InnoDB;
The query is:
SELECT id1 FROM t1;
This is a straight-forward query with no WHERE clause.
Given no WHERE clause, we know there will be a full table or index scan. Let’s look at EXPLAIN:
mysql> EXPLAIN SELECT id1 FROM t1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: index
possible_keys: NULL
key: id2
key_len: 5
ref: NULL
rows: 1 …[Read more]