3

I am running Node.js/MySQL with the following driver: https://github.com/mysqljs/mysql

The application works, but if I call the function more than 2-3 times a second the queries start executing extremely slowly, sometimes taking minutes to execute 10-15 queries.

I will be grateful even for a hint on how to debug this myself, I really don't know how to see what's happening on the back end.

Here's the code for the backend:

//rw.js

var mysql = require('mysql');
var dbconfig = require('../config/database');
var connection = mysql.createConnection(dbconfig.connection);

connection.query('USE ' + dbconfig.database);


function read(id, done) {
    var id = id;
    connection.query("SELECT * FROM users WHERE id = ?",[id], function(err, rows) {
       if (err)
       done(err);
       if (rows.length) {
           done(rows[0].progress);
        };
       });
}

function write(id, val, done) {
    var id = id;
    var val = val;
    connection.query('UPDATE users SET progress = ? WHERE id = ?', [val, id], function (error, results, fields) {
        if (error) throw error;
        done(results)
    });
};


exports.read = read;
exports.write = write;

//app.js

var rw = require('./rw.js')


app.get('/read', isLoggedIn, function(req, res) {
    rw.read(req.user.id, function(currval) {
        console.log("Current value is " + currval);
    });
});

app.get('/write', isLoggedIn, function(req, res){
    rw.read(req.user.id, function(cb) {
        var val = cb + 1;
        rw.write(req.user.id, val, function(justtesting) {
            console.log("Just testing if we get a response" + justtesting);
        });
    });
});

And the frontend:

$scope.tryread = function() {
    $http.get('/read')
}

$scope.trywrite = function() {
    $http.get('/write')
}
angularchobo
  • 183
  • 1
  • 3
  • 17
  • 1
    If you're hammering on your server as hard as you mention, you should probably refactor your node.js server side code to use pooled connections. Please give it a try and let us know if you still have the problem. https://github.com/mysqljs/mysql#pooling-connections – O. Jones Apr 02 '17 at 15:07
  • You dont need the `USE` query. You can specify the `database` in the config for `mysql.createPool` – Julian Apr 02 '17 at 15:37
  • Did you get an answer to this or figure out how to make it work? I think I might be having the same issues. – Rob Jan 20 '19 at 17:16

0 Answers0