I have 2 queries.
First:
SELECT * FROM `table` WHERE col='xyz' LIMIT 100
//Time Taken: 0.0047s
Second:
SELECT * FROM `table` WHERE col='xyz' ORDER BY Id DESC LIMIT 100
//Time Taken: 1.8208s
The second takes a much longer time. I know why that is, it is because first I have to select the whole table, then do the ordering, whereas the first query only returns the first 100 rows.
Is there any way to ORDER BY using another method, like selecting the last 100 rows and then doing the order? Or am I doing the query wrong and it can be made faster?
Note the Id is Autoincrement, so selecting the last rows will still return the correct data when it is ordered.
CREATE TABLE `table`(
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`dateReg` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM