Mysql CFSCRIPT中的事务性能较慢

Mysql CFSCRIPT中的事务性能较慢,mysql,coldfusion,coldfusion-9,Mysql,Coldfusion,Coldfusion 9,我最近注意到,我们的产品有一些短邮政编码的错误邮政编码位置,如AB10而不是ABD 1PT等 邮政编码数据库/表格用于在谷歌地图上生成PIN,我现在已经了解到,当我们将短邮政编码与完整邮政编码合并到表格中时,大约2200个邮政编码被错误地输入,经度和纬度的输入方式错误 显然,这是一个简单的修复,所以我决定写一个小脚本来处理不正确的值,基本上是交换它们 以下是我所拥有的: <cfscript> /** Fetch the wrong postcodes data **/ db

我最近注意到,我们的产品有一些短邮政编码的错误邮政编码位置,如AB10而不是ABD 1PT等

邮政编码数据库/表格用于在谷歌地图上生成PIN,我现在已经了解到,当我们将短邮政编码与完整邮政编码合并到表格中时,大约2200个邮政编码被错误地输入,经度和纬度的输入方式错误

显然,这是一个简单的修复,所以我决定写一个小脚本来处理不正确的值,基本上是交换它们

以下是我所拥有的:

<cfscript>

  /** Fetch the wrong postcodes data **/
  db  = "postcodes";
  sql = "
    SELECT
      postcode, longitude, latitude
    FROM
      postcodes
    WHERE
      longitude > latitude
  ";
  q    = new Query(sql = trim(sql), datasource = db);
  data = q.execute().getResult();

  if (structKeyExists(form, "execute")) {
    if (isQuery(data) && data.recordCount > 0) {
      transaction action="begin" 
      { 
        try {
          qUpdate = new Query(
            datasource = db, 
            sql = "UPDATE postcodes SET longitude = :longitude, latitude = :latitude WHERE postcode = :postcode"
          );
          for (x = 1; x <= data.recordCount; x++) {
            writeOutput("<p>" & data["postcode"][x] & "</p>");

            qUpdate.addParam(name = "longitude", value = data["latitude"][x], cfsqltype = "CF_SQL_DOUBLE");
            qUpdate.addParam(name = "latitude", value  = data["longitude"][x], cfsqltype = "CF_SQL_DOUBLE");
            qUpdate.addParam(name = "postcode", value  = data["postcode"][x], cfsqltype = "CF_SQL_VARCHAR");

            qUpdate.execute();
            qUpdate.clearParams();
          }
          transactionCommit();
        } catch (any e) {
          transactionRollback();
          writeOutput("<p>The database transaction failed, rolling back changes</p>");
          writeDump(e);
        }
      }
      writeOutput("#data.recordCount# postcodes have been updated");  
    } else {
      writeOutput("There were no incorrect postcodes found in the database");
    }
  }
</cfscript>
<cfoutput>
  <form name="update" action="" method="post">
    <input type="hidden" name="execute" value="1"/>
    <input type="submit" name="update" value="Update #val(data.recordCount)# Postcodes"/>
  </form>
</cfoutput>

<!--- <cfdump var="#data#"/> --->
脚本被包装在一个事务中,正如我计划在live server上运行它一样,但是在本地测试脚本之后,它已经持续运行了一个多小时

邮政编码数据库包含近170万条记录,只有三列,所有列都已正确索引邮政编码、经度、纬度。第一次查询返回正确的2200个结果

我已经检查了ColdFusion admin中的组件缓存设置,以查看我的本地服务器上是否缺少该设置,但它已打开

所以我的问题是,为什么要花这么长时间来执行


我们正在使用mysql和ACF 9。

为什么要在CF中使用它?只需在SQL中完成这一切,速度就会快得多。我不使用mysql,但大致是这样的:

UPDATE postcodes 
SET longitude = newlongitude,
latitude = newlatitude 
FROM (SELECT latitude AS newlongitude, longitude AS newlatitude FROM postcodes 
         WHERE longitude > latitude)

为什么要用CF呢?只需在SQL中完成这一切,速度就会快得多。我不使用mysql,但大致是这样的:

UPDATE postcodes 
SET longitude = newlongitude,
latitude = newlatitude 
FROM (SELECT latitude AS newlongitude, longitude AS newlatitude FROM postcodes 
         WHERE longitude > latitude)

执行时间如此之长的原因可能是因为您在该位上循环了2200次:

writeOutput("<p>" & data["postcode"][x] & "</p>");

qUpdate.addParam(name = "longitude", value = data["latitude"][x], cfsqltype = "CF_SQL_DOUBLE");
qUpdate.addParam(name = "latitude", value  = data["longitude"][x], cfsqltype = "CF_SQL_DOUBLE");
qUpdate.addParam(name = "postcode", value  = data["postcode"][x], cfsqltype = "CF_SQL_VARCHAR");

qUpdate.execute();
qUpdate.clearParams();

改为使用SQL解决此问题,您将不会遇到此问题。

执行此操作所需时间过长的原因可能是因为您在该位上循环了2200次:

writeOutput("<p>" & data["postcode"][x] & "</p>");

qUpdate.addParam(name = "longitude", value = data["latitude"][x], cfsqltype = "CF_SQL_DOUBLE");
qUpdate.addParam(name = "latitude", value  = data["longitude"][x], cfsqltype = "CF_SQL_DOUBLE");
qUpdate.addParam(name = "postcode", value  = data["postcode"][x], cfsqltype = "CF_SQL_VARCHAR");

qUpdate.execute();
qUpdate.clearParams();

改为使用SQL解决此问题,您就不会遇到此问题。

我知道哪里慢,我想知道为什么慢。使用预处理语句的2200多个查询应该更快使用预处理语句的2200多个查询应该更快-您同时缓冲HTML输出。那可能会让它慢下来。我知道它在哪里慢。我想知道它为什么慢。使用预处理语句的2200多个查询应该更快使用预处理语句的2200多个查询应该更快-您同时缓冲HTML输出。那可能会减慢速度。