Tolgay Toklar
Tolgay Toklar

Reputation: 4343

Node.js mysql query response delay

I am using node.js mysql driver. But when the server under high load, i guess mysql is doing queue for queries. How can I prevent this? I want to do query instantly.

How can I resolve this? My queries are laggy.

My code:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'ip',
  user     : 'db',
  password : 'pass'
});
connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }
  console.log('connected as id ' + connection.threadId);
});
connection.query('USE db');
connection.query({ sql:"select token,COUNT(token) as sayim from tokens where username=? or anon=?", timeout: 10000},[data,data],function(err,info) {
        if (info[0].sayim != 0) {
            callback(info[0].token);
        }else{
            callback("0");
        }
    });

Ps: The server is not returning any error for this. But when I do a query, server is responding after approximately 10 seconds. If server is not under the high load it is responding instantly.

Upvotes: 0

Views: 1166

Answers (1)

Ervadac
Ervadac

Reputation: 956

You could use PoolConnection instead.

var connection = mysql.createPool({
  host     : 'ip',
  user     : 'db',
  password : 'pass',
  database : 'db'
});

Another thing that comes to mind: In your example, you use asynchronous functions one after the other which should cause you some troubles as well.

Upvotes: 1

Related Questions