I have kind of a special problem using a levenshtein algorithm in MySql. But I don't think that it's special for levenshtein. This query:
SELECT *,levenshtein(word,'Facbook') FROM Words WHERE length(word) between 6 and 8 and levenshtein(word,'Facbook') <=1
is pretty slow with around 5000 rows it takes about 1s.
But on the other hand this one:
SELECT *,levenshtein(word,'Facbook') FROM Words WHERE length(word) between 6 and 8
is kind of fast (0.02s).
The explain statments looks nearly identical only the number of rows differs a single bit.
The explain table for the first query:
| id select_type table type possible_keys key key_len ref rows Extra |
----------------------------------------------------------------------------------------
| 1 SIMPLE Words index NULL word 302 NULL 4711 Using where; Using index|
The second explain:
| id select_type table type possible_keys key key_len ref rows Extra |
------------------------------------------------------------------------------------------
| 1 SIMPLE Words index NULL word 302 NULL 4621 Using where; Using index
Btw I have an index on the word column.
Now the question is why is the first one so much slower? It only has to use the second query and "print out" only the words that matching the second where clause: levenshtein(word,'Facbook') <=1
Right?
Strange: I used PHPMyAdmin to check the time and now I used a simple php file to send the query and now both queries took 1s. So maybe this question is totally bullshit...