dbTalk Databases Forums  

In your opinion, what method is the most efficient for looping thedata...

comp.databases.mysql comp.databases.mysql


Discuss In your opinion, what method is the most efficient for looping thedata... in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SM
 
Posts: n/a

Default In your opinion, what method is the most efficient for looping thedata... - 03-13-2010 , 12:59 AM






i'm always trying to optimize my code even if the one in place works
ok...

I've read, a long time ago, that to make it more efficient when
looping the data from MySQL, the fastest you open the connection and
close the connection to the database, the better it is.

So, i was wondering, what is better: Open the database, put all data
in an array, close the database and use the array to process data OR
open the database, process the data and close the database.

Any ideas what method is better? Maybe both are equally more
efficient...

----------------

//open database and execute query

if($num_results == 0) {

} else {

$rows = array();

while($row = $result->fetch_assoc()) {
array_push($rows, $row);
}

//free query, close db
$result->free(); $db->close();
}

//process data
foreach($rows as $value){
//process data after the db connection is close and the query
results are free
}



OR



//open database and execute query

if($num_results == 0) {

} else {

while($row = $result->fetch_assoc()) {
//process data while the db connection is open and the results
are not free
}
}

//free query, close db
$result->free(); $db->close();

Reply With Quote
  #2  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: In your opinion, what method is the most efficient for loopingthe data... - 03-13-2010 , 07:06 AM






SM wrote:
Quote:
i'm always trying to optimize my code even if the one in place works
ok...

I've read, a long time ago, that to make it more efficient when
looping the data from MySQL, the fastest you open the connection and
close the connection to the database, the better it is.

So, i was wondering, what is better: Open the database, put all data
in an array, close the database and use the array to process data OR
open the database, process the data and close the database.

Any ideas what method is better? Maybe both are equally more
efficient...

Insufficient information, and every case will be different. For
instance, it can depend on the size of the dataset returned - handling
100 bytes of data will be very different from handling 100 MB of data.

But it looks like you are falling into the premature optimization trap.
Code your program for to make it easy to understand and maintain. If
you find you have a performance problem, find the cause of the problem
and fix that.

You will do much better in the long run.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.