The first deleted bug

Core · primary-key, anti-join, limit

Given a table bugs(id, token, title, category, device, reported_at, created_at, updated_at)

with about 10 million rows, some bugs were deleted and left holes in the id sequence.

Find the smallest id greater than 5,000,000 that no bug has. Output the column (missing_id).

Expected columns

missing_id

Hints

Hint 1 — what a hole looks like

An id x is missing right after a bug b when no bug has id = b.id + 1. Checking that is a single primary-key lookup, for example LEFT JOIN bugs n ON n.id = b.id + 1 ... WHERE n.id IS NULL.

Hint 2 — stop at the first one

Walk the bugs from id 5,000,000 upwards in primary-key order and stop at the first bug whose next id is missing: ORDER BY b.id LIMIT 1. MIN() returns the same number but has to look at every candidate first.