Imploding in PHP vs imploding in MySQL - which uses less cpu?
Which of these options is more optimal?
imploding in MySQL
$rsFriends = $cnn->Execute('SELECT CAST(GROUP_CONCAT(id_friend) AS CHAR) AS friends
FROM table_friend
WHERE id_user = '.q($_SESSION['id_user']));
$friends = $rsFriends->fields['friends'];
echo $friends;
vs.
imploding in PHP
$rsFriends = $cnn->Execute('SELECT id_friend
开发者_如何学Go FROM table_friend
WHERE id_user = '.q($_SESSION['id_user']));
while(!$rsFriends->EOF) {
$friends[] = $rsFriends->fields['id_friend'];
$rsFriends->MoveNext();
}
echo implode(',',$friends);
You should probably know that the correct ("most optimal") choice is going to be a factor of many variables:
- Your database and application hardware.
- The size of your data
- What load is already like on database and application servers.
- Indexes and other things that may affect query execution plan on your dataset
- How you actually want to use the data.
- Network latency between app server and database server
But you can definitely analyze the program flow to help you arrive at an answer:
Imploding in PHP:
- Execute a select query returning ALL friend IDs
- Return every single ID to your application.
- Build string in PHP as you read them from the result.
Pros:
- Flexibility in using raw IDs if you actually need them (meaning — if you go with creating the string in MySQL, but post-process it in PHP, the expensive post-process operations will likely negate benefit seen from bypassing PHP in the string-compilation stage)
- IDs could be (are?) returned to your application as numbers, not strings (
1234567890
is 10 bytes ASCII, 4 bytes as a 32-bit integer)
Imploding in MySQL:
- Execute an aggregate query
- Build a string as it aggregates the results
- Return one single string to your application
- Output that string
Pros:
- Might use less memory on the database server for large datasets
- Can't think of much else. As soon as you need anything but a big concatenated string, this solution is definitely sub-optimal.
Per the comments, optimal is heavily depended on the context. With that said, my philosophy is: if the database can do it, let it.
Specifically for your case, if you don't analyze or manipulate the results - meaning this is strictly to join all records together for output - then I definitely vote database.
What's probably most worth optimizing here is developer time and effort, for implementation and maintenance. The difference in CPU cycles (as a proportion of the total work being done) is most likely trivial. I suggest you do it whichever way you can write, test, and support most easily.
精彩评论