I am a bit confused about 2 queries.
First query I am running on a 50m rows table by index on a timestamp
row with a limit, I would expect the query to be immediate regardless of the size of the table, since I run on an index with small limit. Am I wrong?
SELECT 1
FROM indicators.fileso fo
WHERE fo.lastSeen < NOW() - INTERVAL 2 MONTH
limit 100;
Create table:
CREATE TABLE `fileso` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`hostId` int(11) unsigned NOT NULL COMMENT 'code',
`sha256` binary(32) NOT NULL COMMENT 'meir',
`fileName` varchar(150) DEFAULT NULL COMMENT 'meir',
`fullPath` varchar(350) DEFAULT NULL COMMENT 'meir',
`lastSeen` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uniqueness` binary(32) NOT NULL DEFAULT '0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
PRIMARY KEY (`id`),
UNIQUE KEY `uniqueness` (`uniqueness`),
KEY `lastSeen` (`lastSeen`)
) ENGINE=InnoDB AUTO_INCREMENT=1842469487 DEFAULT CHARSET=utf8
Second query when I select by timestamp row with index, with order by desc limit 1
its immediate, but when I do order by asc limit 1
it takes alot of time, dont understand that.
Immediate query:
SELECT 1
FROM indicators.fileso fo
ORDER BY fo.lastSeen DESC
LIMIT 1;
Takes time:
SELECT 1
FROM indicators.fileso fo
ORDER BY fo.lastSeen ASC
LIMIT 1;
lastseen
is missing from the table definition. Is the table partitioned?ORDER BY
? (It rarely makes sense to have aLIMIT
without anORDER BY
, and I think the optimizer missed that combo.)