Sunday, 2 April 2017

Node.js / MySQL queries extremely slow

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')
}



via angularchobo

No comments:

Post a Comment