I am trying to make a similar query faster:

SELECT levenshtein(substring(keywords, 1,5),'texas') as distance   
FROM stuff ORDER BY distance ASC

Unfortunately and as expected, the query takes an eternity. How can I accomplish the same thing, but make it fast? The table is 5 million rows.

  • Whose version of levenshtein() are you using?
    – Rick James
    Sep 16, 2016 at 18:21

1 Answer 1


Functions are slow and it is a complex function. Try optimizing the function.

Or select down to like .NET and do it there.

If you get a lot of exact matches you can make that separate.

SELECT o as distance   
where substring(keywords, 1,5) = 'texas')


SELECT levenshtein(substring(keywords, 1,5),'texas') 
from FROM stuff
where substring(keywords, 1,5) <> 'texas')
FROM stuff ORDER BY distance ASC 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.