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:

  1. your database , application hardware.
  2. the size of data
  3. what load on database , application servers.
  4. indexes , other things may affect query execution plan on dataset
  5. how actually want use data.
  6. network latency between app server , database server

but can analyze program flow arrive @ answer:

imploding in php:

  1. execute select query returning friend ids
  2. return every single id application.
  3. 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:

  1. execute aggregate query
  2. build string aggregates results
  3. return 1 single string application
  4. 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

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -