4

Why does same query take around 300 ms more than actual execution time when using Mysql native driver for Nodejs , even with or without using "create pool" options?

Please refer highlighted section in below attached screenshot

Mysql workbench execution

Also for native driver execution time, please see below attached screenshot:

enter image description here

Codebase for node.js Mysql Native driver

db.js

var mysql = require('mysql');
var connectionpool = mysql.createPool({
    connectionLimit: 100, //important
    host: 'localhost',
    user: config.development.username,
    password: config.development.password,
    database: config.development.database,
    multipleStatements: true,
});

    exports.getConnection = function(callback) {
        connectionpool.getConnection(callback);
    };

emp.js

var connections = require('../config/db');
     var pre_query = new Date().getTime();
        var sqlstatements = "select  * from employees where recordstate<>'raw'and  DATE(createdAt) " + "between ('1982-03-24') and ('2017-04-23')    order by employeesID desc limit 20 offset 0;" + "select COUNT(*) as count   from employees where recordstate<>'raw'and  " + "DATE(createdAt) between ('1982-03-24') and ('2017-04-23')    order by employeesID desc";
        connections.getConnection(function(err, c) {
            console.log(sqlstatements)
            c.query(sqlstatements, function(err, projects) {


                console.log(err);
                if (!err) {
                    c.release();

                    var red = new Object();
                    red.rows = JSON.parse(JSON.stringify(projects[0]));
                    red.count = JSON.parse(JSON.stringify(projects[1]))[0].count;
                    var post_query = new Date().getTime();
                    // calculate the duration in seconds
                    var duration = (post_query - pre_query) / 1000;
                    console.log(duration)
                        // console.log(red);
                    res.json(red);

                }
            })
        })
Rizwan Patel
  • 538
  • 2
  • 9
  • 27

1 Answers1

1

Your measurement in JS includes connection setup and all the processing of the results. The times reported in MySQL Workbench (and the MySQL terminal client) are only what the server reports (running a query and result transmission). Alone the connection setup probably takes most of the 300ms. Try moving the pre_query init to the line right before running the actual query. And end the time measurement directly after that (before the console.log(err) call. This delivers a result comparable to that reported by other client tools.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • 1
    Thanks for clarification, 1. time setting up the TCP connection 2. handshaking with the MySQL sever 3. performing the authentication and setting setup. what would be tentative ratio of query execution in mysql database and nodejs mysql native driver including aforementioned factors ? ratio example :- 1ms in mysql database then 2 ms in nodejs object Since the overall architecture of native driver should be to close the gap of execution time between the two critical pieces – Rizwan Patel Apr 23 '17 at 09:36
  • also query run slower after some idle time and then n number of times a lot faster , what would be recommendation in that scenario. Many thanks – Rizwan Patel Apr 23 '17 at 09:39
  • The connection setup time depends on many factors (server load, connection speed, configuration etc.). Hard to give any fixed ratio for that, but keep in mind that client tools usually connect to a server and hold that connection for the entire lifetime of the app (or at least until the server session is closed). All follow queries run then against this held connection. Regarding slower/faster query execution time: there is a query cache in the server that keeps executed queries for a certain amount of time and can so answer quicker. Could be that is the cause for what you see. – Mike Lischke Apr 23 '17 at 13:04
  • 1
    Thanks for further intricate explanation , but let take below factors into acccount. A case scenario wherein the single user load , db and app on same machine and config is pretty standard with not external options like failover or any intricate user access management of RDBMS in question .still the lag persist for first time or after idle connection which is explained in second part and about second part the query cache, I had an educated guess , just wanted to confirm from MVP! Thanks for your time and clarification ! – Rizwan Patel Apr 23 '17 at 14:49
  • in a nutshell, how to optimize query performance for first time and after idle time ? my machine config . https://cloud.githubusercontent.com/assets/1698139/25309975/bd65659c-27f7-11e7-94f9-2b174af166d6.png – Rizwan Patel Apr 23 '17 at 14:57