Forum Moderators: coopster

Message Too Old, No Replies

PHP vs MySQL regex speed

         

Code Sentinel

10:03 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



I have an array of strings, I wish to compare these strings to a pattern and delete the ones which match.

Is it faster to do this in PHP then insert into MySQL or would MySQL be better for this task?

php does the work:
-loop over array matching and deleting
-dump left over strings into database

mysql does the work:
-php dumps whole array into database
-delete rows ending in pattern, something like: DELETE from FILES WHERE fname REGEXP 'tar\.gz$'

I've never used mysql regexp before so I'm not sure how well or how fast it is compared to just using PHP, would it be faster doing a delete like the above over several rows in mysql or just iterate over an array in PHP and unset the keys where it matched with preg_match and dump the remaining into the database? This table doesn't have an index.. I'm not even sure mysql regexp uses indexes.. does it?

coopster

2:15 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would loop over the array and INSERT after I had cleaned things up. No sense INSERTing rows just to DELETE them again.

Code Sentinel

3:07 am on Jul 6, 2005 (gmt 0)

10+ Year Member



that makes sense heh

I was just wondering if perhaps Mysql would be more efficient or quicker since its purpose is manipulating and storing data.

coopster

10:09 am on Jul 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The primary purpose of the database is to store and retrieve data. Manipulation is a bonus ;)

The only way to know for sure would be to write your application both ways and time them to see which is faster. Any regular expressions require a bit more server resource as they fire up the regex engine.

My humble opinion is that I would rather scrub the data prior to writing it to the database as opposed to writing and then deleting. Remember that the time to delete individual records is exactly proportional to the number of indexes, too.