This algorithm won't work on large DBs

Yep.  Pretty disappointing. We get stack overflows.