I'm trying to create a scrabble-playing bot. So I thrown all (2,7 mln) polish words to a SQL Server database and now working on creating patterns for regex queries. I have just found out that adding some conditions to WHERE
clause could make search much more efficient.
As an example, executing query:
SELECT * FROM words WHERE dbo.[like](word, '^[def]{1,3}$') = 1;
lasts about 43 sec, but adding quite obvious length condition:
SELECT *
FROM words
WHERE dbo.[like](word, '^[def]{1,3}$') = 1 AND LEN(word) <= 3;
reduces execution time to 3 sec... could you tell me why, and advise some techniques that would help me make queries more efficient?
P.S. like function is CLR written in c#:
public static bool Like(string text, string pattern)
{
Match match = Regex.Match(text, pattern);
return (match.Value != String.Empty);
}