I have a table Cars with datetime (DATE) and bit (PUBLIC).
Now i would like to take rows ordered by DATE and with PUBLIC = 1 so i use:
select
c.*
from
Cars c
WHERE
c.PUBLIC = 1
ORDER BY
DATE DESC
But unfortunately when I use explain to see what is going on I have this:
1 SIMPLE a ALL IDX_PUBLIC,DATE NULL NULL NULL 103 Using where; Using filesort
And it takes 0,3 ms to take this data while I have only 100 rows. Is there any other way to disable filesort?
If i goes to indexes I have index on (PUBLIC, DATE) not unique.
Table def:
CREATE TABLE IF NOT EXISTS `Cars` (
`ID` int(11) NOT NULL auto_increment,
`DATE` datetime NOT NULL,
`PUBLIC` binary(1) NOT NULL default '0'
PRIMARY KEY (`ID`),
KEY `IDX_PUBLIC` (`PUBLIC`),
KEY `DATE` (`PUBLIC`,`DATE`)
) ENGINE=MyISAM AUTO_INCREMENT=186 ;
You need to have a composite index on
(public, date)This way,
MySQLwill filter onpublicand sort ondate.From your
EXPLAINI see that you don’t have a composite index on(public, date).Instead you have two different indexes on
publicand ondate. At least, that’s what their namesIDX_PUBLICandDATEtell.Update:
You
publiccolumn is not aBIT, it’s aBINARY(1). It’s a character type and uses character comparison.When comparing integers to characters,
MySQLconverts the latter to the former, not vice versa.These queries return different results:
Either change your
publiccolumn to be abitor rewrite your query as this:, i. e. compare characters with characters, not integers.