最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 6浏览 0评论

如何在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 
})

如何在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 
})

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论