Is there any reason you didn't use MySQL's built-in full-text search for this? It seems like it would have saved a lot of time and would almost certainly be much faster. There are also nice CPAN modules that do very efficient text searching in a MySQL database, like Search::InvertedIndex or DBIx::FullTextSearch.