No indexes
Well, I am fudging a little here. I have a PRIMARY KEY on `seq`,
but that has no advantage on the query we are studying.
mysql> SHOW CREATE TABLE Presidents \G
CREATE TABLE `presidents` (
`seq` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`last_name` varchar(30) NOT NULL,
`first_name` varchar(30) NOT NULL,
`term` varchar(9) NOT NULL,
PRIMARY KEY (`seq`)
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8
mysql> EXPLAIN SELECT term
FROM Presidents
WHERE last_name = 'Johnson'
AND first_name = 'Andrew';
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | Presidents | ALL | NULL | NULL | NULL | NULL | 44 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
# Or, using the other form of display: EXPLAIN ... \G
id: 1
select_type: SIMPLE
table: Presidents
type: ALL <-- Implies table scan
possible_keys: NULL
key: NULL <-- Implies that no index is useful, hence table scan
key_len: NULL
ref: NULL
rows: 44 <-- That's about how many rows in the table, so table scan
Extra: Using where
Variants
⚈ What would happen if you shuffled the fields in the WHERE clause?
Answer: The order of ANDed things does not matter.
⚈ What would happen if you shuffled the fields in the INDEX?
Answer: It may make a huge difference. More in a minute.
⚈ What if there are extra fields on the the end?
Answer: Minimal harm; possibly a lot of good (eg, 'covering').
⚈ Reduncancy? That is, what if you have both of these: INDEX(a), INDEX(a,b)?
Answer: Reduncy costs something on INSERTs; it is rarely useful for SELECTs.
⚈ Prefix? That is, INDEX(last_name(5). first_name(5))
Answer: Don't bother; it rarely helps, and often hurts. (The details are another topic.)
More examples:
INDEX(last, first)
... WHERE last = '...' -- good (even though `first` is unused)
... WHERE first = '...' -- index is useless
INDEX(first, last), INDEX(last, first)
... WHERE first = '...' -- 1st index is used
... WHERE last = '...' -- 2nd index is used
... WHERE first = '...' AND last = '...' -- either could be used equally well
INDEX(last, first)
Both of these are handled by that one INDEX:
... WHERE last = '...'
... WHERE last = '...' AND first = '...'
INDEX(last), INDEX(last, first)
In light of the above example, don't bother including INDEX(last).