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.
console.time('query')
andconsole.timeEnd('query')
. I would recommend to run each method at least 10 times to get an average. It should give you the answer you are looking for. – Murder