Optimize DB check

For some reason one of the DB checks became really slow while I was working on #2579, this PR rewrites that check in a much more efficient form.
Yes, "zero out known anomalous tip" is the commit message for this change.