Imploding in PHP vs imploding in MySQL - which uses less cpu? -
which of these options more optimal?
imploding in mysql
$rsfriends = $cnn->execute('select cast(group_concat(id_friend) char) friends table_friend id_user = '.q($_session['id_user'])); $friends = $rsfriends->fields['friends']; echo $friends;
vs.
imploding in php
$rsfriends = $cnn->execute('select id_friend table_friend id_user = '.q($_session['id_user'])); while(!$rsfriends->eof) { $friends[] = $rsfriends->fields['id_friend']; $rsfriends->movenext(); } echo implode(',',$friends);
you should know correct ("most optimal") choice going factor of many variables:
- your database , application hardware.
- the size of data
- what load on database , application servers.
- indexes , other things may affect query execution plan on dataset
- how actually want use data.
- network latency between app server , database server
but can analyze program flow arrive @ answer:
imploding in php:
- execute select query returning friend ids
- return every single id application.
- build string in php read them result.
pros:
- flexibility in using raw ids if need them (meaning — if go creating string in mysql, post-process in php, expensive post-process operations negate benefit seen bypassing php in string-compilation stage)
- ids (are?) returned application numbers, not strings (
1234567890
10 bytes ascii, 4 bytes 32-bit integer)
imploding in mysql:
- execute aggregate query
- build string aggregates results
- return 1 single string application
- output string
pros:
- might use less memory on database server large datasets
- can't think of else. need big concatenated string, solution sub-optimal.
Comments
Post a Comment