Performance issue: looping over queries with a query results set
I have code that works, but I think I should be able to run the code faster. I could try a stored procedure but there are so many variables to set. I tried with wrapping cftransation around the code, but it didn't make a noticeable difference. I need to go through the data singularly to fill my query object.
Here's an ABBREVIATED sample of the code:
<cfset tot_AllActiveListing = QueryNew(
"AnnounceNum, JP_PDLoc, JP_JS_Title, JP_JS_KWID, JP_JS, JP_Open, JP_Close, JP_CloseType, JP_CloseName, JP_PosNeed, JP_DirectHire, JP_Desc, JP_Draft, JP_Archived, JP_State, JP_AreaID, JP_AreaName, JP_AreaAlias, JP_Fac_SU, JP_Fac_Facility, JP_FAC_ID, JP_Grade1, JP_Grade2, JP_Grade3, JP_Grade4, JP_Grade5, JP_Posted, JP_TypeHire, JP_HRemail",
"VARCHAR,VARCHAR,VARCHAR,INTEGER,INTEGER,TIMESTAMP,TIMESTAMP,INTEGER,VARCHAR,INTEGER,BIT,VARCHAR,BIT,BIT,VARCHAR,INTEGER,VARCHAR,VARCHAR,VARCHAR,VARCHAR,INTEGER,VARCHAR,VARCHAR,VARCHAR,VARCHAR,VARCHAR,TIMESTAMP,INTEGER,VARCHAR")
/>
<cfquery name="getAllActiveListing" datasource="#request.at_datasource#">
SELECT j.JOB_AnnounceNum, j.JOB_PDLoc, j.fk_JS_code, j.Job_JPOpen, j.Job_JPClose, j.fk_CloseType, j.JOB_JPPosNeed, j.JOB_DirectHire, j.JOB_JPDesc, j.Job_JPDraft, j.JOB_JPArchived, j.JOB_State,
j.fk_FACID, j.Posted, j.JOB_IHSvITU, f.Fac_Area, f.Fac_ServiceUnit, f.fac_Facility, f.Fac_Addr1, f.Fac_Addr2, f.Fac_City, f.Fac_State, f.Fac_Zip
from JOB_JP j INNER JOIN #generaldb#IHSFacility f
ON j.fk_FACID = f.Fac_ID
WHERE
JOB_JPDraft = 0
and (Job_JPClose = #Now()# or Job_JPClose > #Now()# or fk_CloseType = 2 or fk_CloseType = 3)
and (JOB_JPArchived = 0 or JOB_JPArchived IS NULL)
<cfif IsDefined("qAltPostID") and qAltPostID.recordcount gt "0">
and JOB_AnnounceNum IN (<cfqueryparam list="yes" cfsqltype="CF_SQL_varchar" value="#ValueList(qAltPostID.fk_Job_AnnounceNum)#">)
<cfelseif option is "JPPostListing" and StructKeyExists(session,"IHSUID")>
and j.WhoCreated = #session.IHSUID#
</cfif>
Order by j.Job_JPOpen desc
</cfquery>
<cfloop from="1" to="#session.getAllActiveListing.recordcount#" index="i">
<cfquery name="getAllActiveListingGrade" datasource="#request.at_datasource#">
SELECT fk_Job_AnnounceNum, Grade
from Job_JP_Grade
Where Job_JP_Grade.fk_Job_AnnounceNum = '#session.getAllActiveListing.Job_AnnounceNum#'
</cfquery>
<cfif IsDefined("session.getAllActiveListing") and session.getAllActiveListing.recordcount neq "0">
<cfquery name="getAllActiveListingIHSArea" datasource="#at_datasource#">
SELECT JOBIHSArea_ID, JOBIHSArea_Name, JOBIHSArea_Alias
from JOB_IHSArea_LKUP
where JOBIHSArea_Alias = '#session.getAllActiveListing.Fac_Area#'
</cfquery>
</cfif>
<cfset session.getAllActiveListingGrade = getAllActiveListingGrade />
<cfquery name="getAllActiveListingCloseName" datasource="#at_datasource#">
SELECT JOB_CloseName
from JOB_CloseType_LKUP
where JOB_CloseType_LKUP.JOB_CloseType = #session.getAllActiveListing.fk_CloseType#
</cfquery>
<cfscript>
newRow=QueryAddRow(tot_AllActiveListing);
QuerySetCell(tot_AllActiveListing, "AnnounceNum", "#session.getAllActiveListing.Job_AnnounceNum#");
QuerySetCell(tot_AllActiveListing, "JP_PDLoc", "#session.getAllActiveListing.JOB_PDLoc#");
QuerySetCell(tot_AllActiveListing, "JP_Draft", "#session.getAllActiveListing.Job_JPDraft#");
QuerySetCell(tot_AllActiveListing, "JP_Archived", "#session.getAllActiveListing.Job_JParchived#");
QuerySetCell(tot_AllActiveListing, "JP_Posted", "#session.getAllActiveListing.Posted#");
QuerySetCell(tot_AllActiveListing, "JP_PosNeed", "#session.getAllActiveListing.JOB_JPPosNeed#");
QuerySetCell(tot_AllActiveListing, "JP_DirectHire", "#session.getAllActiveListing.JOB_DirectHire#");
</cfscript>
</cfloop>
Any ideas will be greatly appreciated. If stored procedures are the best way to handle this and will run appreciably faster, I'll try it.
Thanks.
JoyRose