Skip to main content
kvanhout
Participant
December 3, 2014
Question

Memory leak/overload when looping by index over a large query and updating each DB record

  • December 3, 2014
  • 3 replies
  • 509 views

I am importing a CSV file into a temporary table and then running a select query that joins my actual database table with the temporary table, looking for any changes in the data. If changes exist, the select query is looped from 1 to #recordCount# and an update is applied to each record via cfquery. It runs very quickly (much more quickly than looping the query itself), but my memory spikes and overloads after about 1500 updates are completed. I need to be able to do upwards of 20000 at a time, without killing my system. I have tried manually setting the runtime garbage collection to trigger after X number of loops, but that doesn't seem to help. I am running CF8. See below for loop example:

<cfloop from="1" to="#updatedRecordsQuery.recordCount#" index="a">

<cftry>

                <cfquery datasource="#db#" name="doUpdate">

                    UPDATE

                        CI

                    SET

                        firstname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#updatedRecordsQuery.firstname#" />,

                        lastname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#updatedRecordsQuery.lastname#" />,

                        etc, for about 15 various fields

                    FROM

                        client_info CI

                    WHERE

                        CI.client_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#updatedRecordsQuery.client_id#" />

                </cfquery>

          

                <cfcatch type="database">

                    <cfset local.updateErrorList = listappend(local.updateErrorList,updatedRecordsQuery.client_id) />

                    <cfset local.error = true />

                </cfcatch>

           </cftry>

</cfloop>

This topic has been closed for replies.

3 replies

December 10, 2014

Hi,

Maybe this is a dumb question but how are you detecting updated rows in the temp table.

Is there a "update date" field that indicates a change or are you scanning/comparing multiple fields.

If the answer is the second option maybe you can add a date stamp to the application that creates the csv file and add a date variable to your temp table query WHERE statement.

Upen1
Inspiring
December 8, 2014

I would suggest to use select update instead of looping over query object and update each row one-by-one.

Procedure:

- Insert your CSV data into temp table.

- Use a select update SQL query to update the changed data instead of looping over a select query.

Example:

UPDATE
  
Table
SET
  
Table.col1 = other_table.col1,
  
Table.col2 = other_table.col2
FROM
  
Table
INNER JOIN
  other_table
ON
  
Table.id = other_table.id

NOTE: You can put all your scripts in a Procedure.