简体   繁体   English

对 MySQL 查询使用 Promise 会导致超时

[英]Using Promises with MySQL Queries Results in Timeout

I'm wondering if I implemented Promises correctly with MySQL statements.我想知道我是否使用 MySQL 语句正确实现了 Promises。 The problem is that it will timeout waiting for the query results.问题是它会超时等待查询结果。 I find this strange because I implemented the code the same way using callback functions and it never timed out.我觉得这很奇怪,因为我使用回调函数以相同的方式实现代码并且它从未超时。 I think I'm using Promises incorrectly but can't figure out how.我想我错误地使用了 Promises,但不知道怎么做。 I confirmed that the code using Promises does work on retrieving small datasets (hundreds), but times out when retrieving thousands of records.我确认使用 Promises 的代码确实适用于检索小型数据集(数百个),但在检索数千条记录时会超时。

Using Promises使用承诺

function innermostRetrieveDependencyByResource(dep_name_version){
   var sql = "SELECT * FROM dependent WHERE name_version = '" + dep_name_version + "' LIMIT 1";
   return database.query(sql).then(result => {
      return result;
   })
}

function innerRetrieveDependencyByResource(scan_id){
   var depList = [];
   var sql = "SELECT DISTINCT dep_name_version FROM branch WHERE scanid = '" + scan_id + "'";
   return database.query(sql).then(result => {
      promiseList = [];
      for (r in result){ // iterate over branch scans
         dep_name_version = result[r];
         promiseList.push(innermostRetrieveDependencyByResource(dep_name_version))
      } 
      return Promise.all(promiseList).then(result => {
         return result;
      })
   })
}

function retrieveDependencyByResource(resource, cb){
   promiseList = []
   var sql = "SELECT (scanid) FROM scan WHERE resource = '" + resource + "'"; 
   database.query(sql).then(result => { // note: query returns a Promise
      var scanPending = result.length;
      for (s in result){
         scan_id = result[s].scanid;
         promiseList.push(innerRetrieveDependencyByResource(scan_id))
      }
      Promise.all(promiseList).then(result => {
         cb(result);
      })
   })
}

Using Callbacks:使用回调:

function retrieveDependency(projname, branch, cb){
   depList = []
  var sql = "SELECT (scanid) FROM scan WHERE project = '" + projname + "' AND branch = '" + branch + "'"; 
   connection.query(sql, function (err, result) { // note: query returns a Promise
     if (err) throw err;
     scan_id = result[0].scanid;
     var sql = "SELECT DISTINCT dep_name_version FROM branch WHERE scanid = '" + scan_id + "'";
     connection.query(sql, function (err, result) {
      if (err) throw err;
      pending = result.length; 
      for (r in result){
         dep_name_version = result[r].dep_name_version;
         var sql = "SELECT * FROM dependent WHERE name_version = '" + dep_name_version + "'";
         connection.query(sql, function(err, result){
            if (err) throw err;
            depList.push(result);
            if ( 0 === --pending ){
               cb(depList);
            }
         })
      }
    }); 
   });  
}

For proper query promises, use mysql2/promise or mysql21 .对于正确的查询承诺,请使用mysql2/promisemysql21

Also, I'd recommend loading results with a single query:另外,我建议使用单个查询加载结果:

// with mysql21

let query = `SELECT scanid, dep_name_version, dependent*
  FROM scan
  JOIN branch USING (scanid)
  JOIN dependent ON (name_version = dep_name_version)
  WHERE resource = ?`;

const retrieveDependencyByResource = async (resource) => 
  connection.query(query, [resource]);

// await results

let dependencies = await retrieveDependencyByResource(resource));

// or use then

retrieveDependencyByResource(resource)
  .then(dependencies => {
    ...
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM