Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node MySQL execute multiple queries the fastest possible

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.

like image 294
user2278120 Avatar asked Oct 18 '22 19:10

user2278120


1 Answers

Of the four above options using 3 real queries in my app, that each take about 1-3 seconds each to execute, method 3 ended up being the fastest for anyone interested.

Method 1 and 2 were about a full 1/2 second slower, and method 4 was only super slightly slower.

Edit: I did these benchmarks by using the console.time('query') suggestion in the comments above.

like image 178
user2278120 Avatar answered Nov 02 '22 23:11

user2278120