Thursday, March 26, 2009

Using CFTHREAD to loop through a query

My first experience with Cfthread tag, I had to loop through a query with loads of records. To make the looping faster, I used 10 cfthreads, below is how to do it.

<cfquery name="get_data" datasource="whatever">
select xyz from table
</cfquery>

First create 10 threads

<cfloop index="intGet" from="0" to="9" step="1">
<cfthread action="run" name="objGet#intGet#">

do some calculations to get the startrow and endrow for each thread to loop through

<cfset startRow = int((#intGet# * #get_users.Recordcount# / 10) + 1)>
<cfset endRow = int(((#intGet# + 1) * #get_users.Recordcount# / 10))>

Loop through the query

<cfloop query="get_data" startrow="#startRow#" endrow="#endRow#">
-------- Your calculations / stuff goes here -------------
</cfloop>

</cfthread>
</cfloop>

Lastly JOIN the threads

<cfloop index="intGet" from="0" to="9" step="1">
<cfthread action="join" name="objGet#intGet#" />
</cfloop>

you would be able to save a lot of time, also you can use "thread" scopes to assign values and then do the calculations.

1 comments:

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Blog Archive