In the last past days I noticed something weird optimizing my query.
I have a simple query which does something like:
SELECT id,name,amount FROM reservations WHERE NOT canceled ORDER BY name ASC
I noticed mysql wasn’t using any index, so I started doing some experiments.
Accidentally I replaced the “NOT canceled” with “canceled=false”, and then, Mysql started using “canceled” as index.
After that I tried using the opposite:
SELECT ... FROM reservations WHERE canceled ORDER BY ...
Same result! When I change that to “canceled=true” the index works again.
My question is: HOW COME?! Isn’t using “NOT” the “elegant” way? Anyhow I didn’t expect for it to make any difference.
I’m using InnoDB as the engine, but i get same result using MyISAM.
Can someone clarify things up?
Thanks.
Edit: Table structure
CREATE TABLE `reservations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trip_code` varchar(10) DEFAULT NULL,
`departure_date` date DEFAULT NULL,
`amount` float DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`canceled` tinyint(1) NOT NULL DEFAULT '0',
`created_date` date NOT NULL,
`creator_user` int(11) NOT NULL DEFAULT '1',
`last_update_user` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `trip_code` (`trip_code`),
KEY `departure_date` (`departure_date`),
KEY `created_date` (`created_date`),
KEY `canceled` (`canceled`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=123181 ;
I am not familiar with MYSQL, but thinking logically, I understand it like this:
Index is like a phone book, when you are searching for “Cohen”, you can get it right away.
But if you are looking for NOT “Cohen”, you will have to run over every entry, and check if it’s different from “Cohen”.
So when you are looking for specific value, it looks just for it. And when you are using NOT, it looks for any other value that can fit inside
tinyint(1)(as I understand it’s not only1or0, is it?).