I’ve been using a php search class for years - it works really well and uses the levenshtein function to dynamically pull extremely relevant results for any number of DB columns. The problem is is pulls every single row in the database and runs an algorithm on them. This is actually faster than it sounds ( about 0.3 seconds on 30,000 rows), but I was hoping to optimize it by doing it entirely in sql.
Here is a query that runs extremely fast (about 0.02 seconds on 30,000 rows):
SELECT id, firstName, ((CHAR_LENGTH(firstName) - levenshtein('robert', firstName)) / CHAR_LENGTH(firstName)) AS matchPercentage
FROM Clients
The problem is now I need to order those results by the calculated field ‘matchPercentage’, and as soon as I do that it bogs down to around 18 seconds, i.e.
SELECT id, firstName, ((CHAR_LENGTH(firstName) - levenshtein('robert', firstName)) / CHAR_LENGTH(firstName)) AS matchPercentage
FROM Clients
GROUP BY id
ORDER BY matchPercentage DESC
I’ve also tried creating temporary tables, etc but everything seems to run around 18 seconds.
Is it possible to achieve what I’m wanting efficiently?