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

时间:2017-12-19 12:57:54

标签: javascript node.js callback

我在nodejs中创建了一个函数,该函数返回对象 QueryName 查询

{"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秒钟来获取所有查询数据。

2 个答案:

答案 0 :(得分:1)

正如@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 
})

答案 1 :(得分:0)

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