I have one scenario where I want to have max value on date column,
eg. for table
CREATE TABLE IF NOT EXISTS `sample_table` (
`id` TINYINT UNSIGNED NOTNULL,
`name` VARCHAR(45) NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARYKEY(`id`))
ENGINE = InnoDB;
So I used a query as below:
SELECT MAX(created_at) AS latest FROM sample_table;
But I was suggested to index created_at column and use following query for faster result
SELECT created_at FROM sample_table ORDER BY created_at DESC LIMIT 1;
I am clueless which is best way to do so? If second query is better, than why there is inbuilt MAX function
id => 1 select_type => SIMPLE type => ALL possible_keys => NULL key => NULL key_len => NULL ref => NULL rows => 52134 Extra => ____
2nd query resultid => 1 select_type =>SIMPLE type => ALL possible_keys => NULL key => NULL key_len =>NULL ref=>NULL rows=>50883 Extra => Using filesort