Mahks
Mahks

Reputation: 6789

Should replace be done in mysql or php?

To reduce the size of downloaded data I would like to replace some repetitive sections of the data with a code, reconverting on the client.

Should the replace be done within the mysql query or after in PHP, or does it matter?

Within mysql:

SELECT IF(SUM(val)=16,0,CONCAT("[",GROUP_CONCAT(TRIM(TRAILING ".000" FROM val) ORDER BY itm),"]"))

PHP:

$q=str_replace("1.000","1",$q);
$q=str_replace("[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]","0",$q);

Upvotes: 1

Views: 103

Answers (2)

Sedat Kapanoglu
Sedat Kapanoglu

Reputation: 47680

Wherever you choose to do it, do not do it inline. Contain it in a function so you can reuse and manipulate the logic without breaking the actual query and vice versa.

I'd use my vote for PHP code because when you switch to somethingElseDB tomorrow you won't need to figure out to implement the same logic on an entirely different SQL syntax. I think PHP implementation is also easier to read.

Upvotes: 3

John Conde
John Conde

Reputation: 219894

MySQL will be faster than PHP and will keep your code a bit cleaner.

Upvotes: 3

Related Questions