I've tried analyzing the EXPLAIN
but unsure if I'm missing some helpful index. Queries take 20-30 seconds across 14 million rows.
CREATE TABLE `domains` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`domain` varchar(255) NOT NULL,
`last_seen_at` timestamp NULL DEFAULT '1980-01-01 00:00:00',
`failures` int(9) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `domain_UNIQUE` (`domain`),
KEY `last_seen_index` (`last_seen_at`),
KEY `failures_index` (`failures` DESC),
) ENGINE=InnoDB AUTO_INCREMENT=15917835 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
my query:
SELECT
domains.id,
domains.domain,
domains.last_seen_at,
domains.failures
FROM domains
WHERE
domains.failures < 10
ORDER BY domains.last_seen_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
explain:
+----+-------------+---------+------------+-------+------------------------------------------+-----------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+------------------------------------------+-----------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | domains | NULL | index | failures_index | last_seen_index | 5 | NULL | 2 | 50.00 | Using where |
+----+-------------+---------+------------+-------+------------------------------------------+-----------------+---------+------+------+----------+-------------+
mysql> SELECT VERSION();
+---------------+
| VERSION() |
+---------------+
| 8.0.18-google |
+---------------+
failures < 10
. Do you expect that the rows which have the smallestlast_seen_at
values to havefailures < 10
?failures > 10
, but no correlation betweenlast_seen_at
andfailures
. i have workers cycling through domains regularly.last_seen_at
value first, you would expect to find a row which matches yourfailures
predicate very soon?