I am trying to figure out the settings in mySQL on indexes. When does mySQL ignore an index?
Here are the results of an experiment. I have a table with an index on the AGE column as follows.
CREATE TABLE `USERS` (
`ID` int(11) NOT NULL,
`FIRSTNAME` varchar(45) NOT NULL,
`LASTNAME` varchar(45) DEFAULT NULL,
`USERNAME` varchar(45) DEFAULT NULL,
`ROLE` int(11) DEFAULT NULL,
`PASSWORD` varchar(45) DEFAULT NULL,
`AGE` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `USERS`
ADD PRIMARY KEY (`ID`),
ADD KEY `AGE` (`AGE`);
Results of EXPLAIN queries. The first three statements use the index. The second set of statements ignore the index and do a full table scan.
The range of AGE is random between 20 and 100 years. There are 1000 rows in the table.
/* utilizes the index on AGE */
/* case 1 */
SELECT ID, AGE FROM USERS WHERE AGE > 20;
/* case 2 */
SELECT AGE FROM USERS WHERE AGE > 44;
/* case 3 */
SELECT * FROM USERS WHERE AGE > 84;
/* does not use index on AGE */
/* case 4 */
SELECT AGE, FIRSTNAME FROM USERS WHERE AGE > 83;
/* case 5 */
SELECT * FROM USERS WHERE AGE > 83;
/* case 6 */
SELECT AGE FROM USERS WHERE AGE > 18;
Some observations that I have seen. Can anyone confirm my conclusions are correct?
1) SELECT * will use the index when 15% or fewer of the rows are selected. 2) SELECT AGE will use the index when 1 or more rows are selected.
code blocking
for code and log and error texts and bold and italics to highlight things