mysql快速查询节点,节点MySQL尽可能快地执行多个查询

本文探讨了四种不同的MySQL查询方法,包括使用单个连接和连接池,以及同步和异步执行。方法1和2使用单个连接,而方法3和4利用异步并行查询。选择最佳方法取决于数据库服务器的处理能力和与应用服务器间的网络带宽。当数据库处理速度快于网络时,方法1和2更优;如果网络带宽更大,方法3和4理论上更快。然而,异步方法可能因依赖关系导致数据不一致。
摘要由CSDN通过智能技术生成

Which is the fastest method gets the query to MYSQL, and then comes back to output:

console.log('queries finished', results)"

Is there an even better method? Please explain your answer!

Thanks!

Method 1:

var connection = mysql.createConnection({multipleStatements: true});

connection.query('SELECT ?; SELECT ?', [1, 2], function(err, results) {

if (err) throw err;

console.log('queries done', results);

});

Method 2:

const Db = mysql.createPool({

connectionLimit: 7,

dateStrings: true,

multipleStatements: true

});

Db.getConnection(function(err, connection) {

if(err) console.log(err);

connection.query(`

SELECT "1" AS "first";

SELECT "2" AS "second";`, function(err, results) {

connection.release();

if(err) console.log(err);

console.log('queries done', results);

}

);

});

Method 3:

const Db = mysql.createPool({

connectionLimit: 7,

dateStrings: true,

multipleStatements: true

});

Db.getConnection(function(err, connection) {

async.parallel([

function(callback) {

connection.query(`

SELECT "1" AS "first"`, function(err, done) {

callback(err, done);

}

);

},

function(callback) {

connection.query(`

SELECT "2" AS "second"`, function(err, done) {

callback(err, done);

}

);

}

], function(err, results) {

connection.release();

if(err) console.log(err);

console.log('queries finished', results);

});

});

Method 4:

const Db = mysql.createPool({

connectionLimit: 7,

dateStrings: true,

multipleStatements: true

});

async.parallel([

function(callback) {

Db.getConnection(function(err, connection) {

connection.query(`

SELECT "1" AS "first"`, function(err, done) {

connection.release();

callback(err, done);

}

);

});

},

function(callback) {

Db.getConnection(function(err, connection) {

connection.query(`

SELECT "2" AS "second"`, function(err, done) {

connection.release();

callback(err, done);

}

);

});

}

], function(err, results) {

if(err) console.log(err);

console.log('queries finished', results);

});

And I didn't post it, but method 3 and 4 could also be done a without connection pool as well. There's also promises over using the npm module async, what is the fastest and why!? Thanks.

解决方案

Method 1 and 2 are similar except that Pool creates a connection if all connections in the pool are used.

To determine which is faster, you need to know the computing power vs network bandwidth between your application server and your database server.

Here's why:

In method 1 and 2, you are using a single connection to execute multiple queries. If the processing power of the database machine is faster than then network bandwidth to send/receive multiple queries, then method 1 and 2 is more efficient. If the processing power of the database machine is slower than the bandwidth(e.g. both application / mysql server resides on the same machine), then method 3 and 4 will theoretically be faster.

Another factor is whether the statements depend on one another. Because step 1 and 2 essentially runs the statements synchronously, the entire set of operation is an atomic / consistent operation. For method 3 and 4, because they run asynchronously although they are triggered in parallel, there can be instances where a later statement complete earlier than an earlier statement. If that happens and there's dependency, method 3 and 4 will corrupt your data.

TL;DR

Fast + Synchronous (insert/update, then select results) =

Method 1, 2 (Use pooling to reduce the need to open new connections)

Fast + Asynchronous(mass inserts) = Method 3, 4

Slow connection between application/database server = Method 1,2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值