Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cfloop vs cfoutput on queries

I develop using ColdFusion and wanted to know what is the best strategy to loop over large query result set. Is there any performance difference between using cfloop and cfoutput? If not, is there any reason to prefer one over the other?

like image 200
Abbadon Avatar asked Oct 08 '12 13:10

Abbadon


4 Answers

I believe that there used to be. I think this difference has been tackled, the best bet is to do a test for each to test in you specific use case.

<cfset t = GetTickCount()/>
<cf... query="qry">
  <!--- Do something --->
</cf...>
<cfset dt = GetTickCount() - t/>
<cfdump var="#dt#"/>
<!--- 
If the differences are small you can use java.lang.System.nanoTime() instead 
--->

There are some notable differences though. cfoutput can do grouped loops, which cfloop cannot.

<cfoutput query="qry" group="col">
  <!--- Loops once for each group --->
  <cfoutput>
    <!--- Loops once for each record within the group --->
  </cfoutput>
</cfoutput>

For cfoutput you can specify the startrow and the maxrows (or the count) to paginate your result. For cfloop you have to specify the endrow index instead of the count.

Also you cannot use cfoutput for a query nested within an existing cfoutput tag, you will need to end the containing cfoutput first.

like image 153
Stuart Wakefield Avatar answered Nov 11 '22 12:11

Stuart Wakefield


One good reason to use cfloop instead of cfoutput is if you need to loop a query output within another query output cfoutput does not support nested query outputting. You can however get away with it using cfloops. So:

<cfoutput query="test1">
   #test1ID#
   <cfoutput query="test2">
      #test2ID#
   </cfoutput>
</cfoutput>

does not work, but if you replace the cfoutputs with cfloops, it will.

As of CF10, with the ability to group cfloops, that's the only remaining functional difference. They both perform the same.

like image 43
K_Cruz Avatar answered Nov 11 '22 13:11

K_Cruz


I believe it's all the same as performance, Ben Forta

And the rest is pretty much personal preference as far as how you "like" to work with your loop. Keep in mind you should always scope your variables, but inside a cfoutput loop that would be especially important since the query fields "could" be referenced without referring to their scope.

one reason you may prefer the cfloop approach would be if you needed to "escape" cfoutput during your loop for any reason. I have run into that several times, so I generally prefer cfloop.

like image 41
JamesRLamar Avatar answered Nov 11 '22 13:11

JamesRLamar


There wouldn't be a performance difference using either method, it depends on your coding style really. If you put a <cfoutput> at the top and bottom of every page then using <cfloop> will work great. If you use multiple <cfoutput> and only place where they are needed that works as well.

I personally put <cfoutput> only where they are necessary, but I wouldn't say that's more correct than placing them at the top and bottom of the page.

like image 1
Matt Busche Avatar answered Nov 11 '22 11:11

Matt Busche