Levenshtein as a MySQL stored function

Edit distance in SQL works; O(n·m) will punish unfiltered scans.

Levenshtein counts edits between strings. As a stored function it is fine for small dictionaries and admin tools.

SELECT id, name, levenshtein(name, 'Open Query') AS dist
FROM organisations
HAVING dist <= 3
ORDER BY dist;

Without a prefilter (LIKE, phonetic keys, blocking), this is a CPU-heavy full scan.

At scale

  • External search
  • Trigram indexes where available
  • Normalise + block before expensive distance

Prototype in SQL. Do not put unfiltered Levenshtein on a public autocomplete hot path.