How we can design best optimized solution: Generate huge random numbers and check in database table whether this numbers are there or not
Put the million numbers in a table.  Then JOIN the tables together to see which ones exist.  Or LEFT JOIN ... IS NULL to see which are missing.