On 13/11/06, David Tulloh <david@xxxxxxxxxxxx> wrote:
Dotan Cohen wrote: > If I have to perform 30 LIKE searches for different keywords in a > varchar field, which strategy would be recommended: > 1) 30 searches, one for each keyword > 2) To select the varchar field from all the rows, and search through > them with php's array functions? It's not going to make a great deal of difference if you do the processing in the MySQL or the PHP, in this case it's basically the same operation in each. I suspect that efficiently recreating the LIKE functionality in PHP wouldn't be trivial to do, if you are just doing straight comparisons the MySQL STRCMP function should be faster. If you are worried about the speed of this query I'd suggest rethinking your database structure. Text field comparisons will always be relatively slow compared to numeric comparisons or numeric lookups. My reading of this query however is that it should be 30 OR comparisons, no joins involved. In this case the query will scale linearly with your database size so you shouldn't worry too much about it slowing down over time.
By that do you mean that it is O=n? Yes, it is linear. It's a matter of knowing if 30 LIKE queries are more efficient than one big query that brings down three fields (an INT and two VARCHAR's) for every row in the table. Now that I'm thinking about it, I'm probably better off with one big query, as it is only one query, and I'd imagine that flipping through an array 30 times is faster than making 30 database calls. Dotan Cohen http://what-is-what.com/what_is/digg.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php