【问题标题】:node - mysql query that takes a few minutes repeating itself after a few minutes before finishing the original querynode - mysql 查询,在完成原始查询之前几分钟后重复自己需要几分钟
【发布时间】:2013-09-11 15:14:17
【问题描述】:

我正在使用 Angular 和节点 js 构建应用程序。 我有一个非常大的查询(插入查询),它插入了 30,000 行之类的东西,由于某种原因需要几分钟(我想没问题)。

这是我的查询:

this.createCourts = function (req, res, next){
  console.log("entered creation courts");
      connection.query('CALL filldates("' + 
        req.body['startDate'] + '","' +
        req.body['endDate'] + '","' +
        req.body['numOfCourts'] + '","' +
        req.body['duration'] + '","' +
        req.body['sundayOpen'] + '","' +
        req.body['mondayOpen'] + '","' +
        req.body['tuesdayOpen'] + '","' +
        req.body['wednesdayOpen'] + '","' +
        req.body['thursdayOpen'] + '","' +
        req.body['fridayOpen'] + '","' +
        req.body['saturdayOpen'] + '","' +
        req.body['sundayClose'] + '","' +
        req.body['mondayClose'] + '","' +
        req.body['tuesdayClose'] + '","' +
        req.body['wednesdayClose'] + '","' +
        req.body['thursdayClose'] + '","' +
        req.body['fridayClose'] + '","' +
        req.body['saturdayClose'] +
        '");', function(err, result, fields){

        if (err){
          console.log("error is" + err);
          return res.send(500, "fail");              
        }
        else{
          console.log("finsihed");
          return res.send(200);
        }
      });
  });
};

我的用户界面将单击一个按钮,该按钮将触发如下服务:

  CourtsService.createCourts.save({}, 
    {"startDate": dateService.Date_toYMD($scope.startDate), 
    "endDate": dateService.Date_toYMD($scope.endDate), 
    "numOfCourts": $scope.numOfCourts, 
    "duration": $scope.duration, 
    "sundayOpen": $scope.sundayOpen.hour, 
    "mondayOpen": $scope.mondayOpen.hour, 
    "tuesdayOpen": $scope.tuesdayOpen.hour, 
    "wednesdayOpen": $scope.wednesdayOpen.hour, 
    "thursdayOpen": $scope.thursdayOpen.hour, 
    "fridayOpen": $scope.fridayOpen.hour, 
    "saturdayOpen": $scope.saturdayOpen.hour, 
    "sundayClose": $scope.sundayClose.hour,
    "mondayClose": $scope.mondayClose.hour,
    "tuesdayClose": $scope.tuesdayClose.hour,
    "wednesdayClose": $scope.wednesdayClose.hour,
    "thursdayClose": $scope.thursdayClose.hour,
    "fridayClose": $scope.fridayClose.hour,
    "saturdayClose": $scope.saturdayClose.hour},
     function(data){
      $scope.showSearching = false;
      $dialog.messageBox("success", btns).open();
  }, function(err){
      $scope.showSearching = false;
      $dialog.messageBox("fail", err.data, btns).open();
  });

我在实际执行查询的函数的第一行添加了这一行(它在此处的代码中) console.log("进入创世法庭");

我的问题是,因为查询运行了几分钟(可能是 10 分钟),似乎我的函数被一次又一次地调用,因为我可以在控制台中看到该行 “进入创造法庭”大约每 1 分钟一次,这当然不是我希望发生的事情。

我不知道是什么触发了 createCourts 函数一次又一次。

可能是因为 UI 进入超时状态,因此如果 1 分钟后(或接近此时间)仍未收到答案,则会再次触发服务。如果这是我如何告诉服务等待答案的原因?

【问题讨论】:

    标签: javascript mysql node.js angularjs express


    【解决方案1】:

    考虑异步 api:在服务器处理程序上立即响应 HTTP 200 和一些 id,并且还有另一个端点来检查带有 id 的插入命令是否完成。

    请注意,您的代码容易受到 sql 注入攻击。更好的方法:

    var params = req.body; // you may want to filter names explicitly here
    connection.query('CALL filldates(?,?,?,?,...)', params, function(err, result, fields){
        if (err){
          console.log("error is" + err);
          return res.send(500, "fail");              
        }
        else{
          console.log("finsihed");
          return res.send(200);
        }
    });
    

    这将在发送查询之前转义客户端上的所有危险字符,或者使用带有mysql2 的准备好的语句来发送与参数无关的查询:

    var params = req.body; // you may want to filter names explicitly here
    connection.execute('CALL filldates(?,?,?,?,...)', params, function(err, result, fields){
        if (err){
          console.log("error is" + err);
          return res.send(500, "fail");              
        }
        else{
          console.log("finsihed");
          return res.send(200);
        }
    });
    

    '几分钟'恕我直言太多了,在我的基准测试中,我的插入率约为 10k 行/秒。尝试使用生成大型插入查询文本替换惰性调用并使用控制台客户端进行测试。如果仍然需要几分钟,那么问题不在节点领域,您需要优化您的数据库。

    【讨论】:

    • 感谢您的回答。我不明白这一行是什么意思:“还有另一个端点来检查带有 id 的插入命令是否完成。”什么是“生成大插入查询文本”?再次感谢。
    • 第二个问题 - 不要执行大量的“插入”请求,而是尝试生成一个包含所有插入的大 sql 字符串,将其保存到本地文件并测量执行 cat myinserts.sql | mysql 所需的时间
    • 我只是把这个函数放在一个事务中,现在只需要 5 秒。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2013-05-08
    • 2014-12-17
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多