1

I am building an app with angular and node js. I have a very big query (insert query) that inserts something like 30,000 rows which for some reason takes a few minutes (i suppose its ok).

this is my query:

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);
        }
      });
  });
};

my ui will click a button which will trigger a service like this:

  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();
  });

I added this row on the first line of the function that will actually do the query(its in the code up here) console.log("entered creation courts");

My problem is that because the query runs for a few minutes (may be 10 minutes), it seems as if my function is being called again and again because I can see in my console the line "entered creation courts" approximately every 1 minute and this is ofcourse not something i wish to be happening.

I don't know what triggers the createCourts function again and again.

maybe it is because the UI goes into time out and thus triggers the service again if an answer hasn't been received after 1 minutes (or something close to that). and if this is the reason how do I tell the service to just wait for an answer?

4

1 回答 1

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 行/秒。尝试使用生成大型插入查询文本替换惰性调用并使用控制台客户端进行测试。如果仍然需要几分钟,那么问题不在节点区域,您需要优化数据库。

于 2013-09-08T23:59:26.123 回答