如何在nodejs中执行并行查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在nodejs中执行并行查询?相关的知识,希望对你有一定的参考价值。

我在nodejs中创建了一个函数,它返回对象QueryName和Query

{"QueryName":"a","Query":"SELECT something from bar"} 
{"QueryName":"b","Query":"SELECT something from bar"} 
{"QueryName":"c","Query":"SELECT something from bar"} 
{"QueryName":"d","Query":"SELECT something from bar"} 

此外,我已创建另一个函数来获取查询结果。

代码到目前为止

executeQuery(connection, query, config.dbtype, response, function (error, data) {

});

function executeQuery(connection, query, dbtype, response, callback) {
    DB_OPERATIONS.executeOperations(connection, APP_CONSTANT.findall, query, dbtype, null, null, "", function (error, data) {
        callback(error, data);
    });
};

FIND_ALL.findAllRecords(connection, query, dbtype, function(error, result) {
    callback(error, result);
});

执行每个查询的时间为5秒,总时间为20秒。

如何在nodejs中并行执行每个查询?然后,而不是20秒,最多需要5秒钟来获取所有查询数据。

答案

正如@zabusa在评论中指出的那样,你可以使用基于承诺的函数。

这里有些例子

var queries = [{"QueryName":"a","Query":"SELECT something from bar"} 
{"QueryName":"b","Query":"SELECT something from bar"} 
{"QueryName":"c","Query":"SELECT something from bar"} 
{"QueryName":"d","Query":"SELECT something from bar"}]


//Promisified executeQuery
function executeQueryAsync(connection, query, dbtype, response) {
    return new Promise(function(resolve, reject){
        DB_OPERATIONS.executeOperations(connection, APP_CONSTANT.findall, query.Query, dbtype, null, null, "", function (error, data) {
            var result = {
                QueryName: query.QueryName
            }
            if(error){
                reject(error);
                return;
            }
            result.data = data;
            resolve(result);
        });
    })
};

// Promise with callback
function executeMultipleQueries(callback){

    var promises = [];
    queries.forEach(function(query){
        promises.push(executeQueryAsync(connection, query, config.dbtype, response))
    })

    Promise.all(promises).then(callback)

}

executeMultipleQueries(function(results){
    // results[0].QueryName is a
    // results[1].QueryName is b
    // results[2].QueryName is c
    //actually there is no need to create a result object in the promise, 
    //if we only revolved the data the result array will have them in the order with pushed them in the promises array 
})


//Full Promise

function executeMultipleQueriesPromise(){

    var promises = [];
    queries.forEach(function(query){
        promises.push(executeQueryAsync(connection, query, config.dbtype, response))
    })

    return Promise.all(promises)

}

executeMultipleQueriesPromise().then(function(results){
    // results[0].QueryName is a
    // results[1].QueryName is b
    // results[2].QueryName is c
    //actually there is no need to create a result object in the promise, 
    //if we only revolved the data the result array will have them in the order with pushed them in the promises array 
})
另一答案

您可以使用Cluster更多进程。您可以在此线程Why is Node.js single threaded?中查看有关节点中多线程的更多信息。希望对你有所帮助。

以上是关于如何在nodejs中执行并行查询?的主要内容,如果未能解决你的问题,请参考以下文章

并行LINQ PLinq

Sleep() 方法后的代码片段没有被执行

如何在 Python3.7 docker 容器中执行并行 PostgreSQL 查询?

NodeJs GraphQL 片段解析器

nodeJS mysql库如何串行执行查询

NodeJs的async