Node.js / MySQL查询速度极慢

时间:2017-04-02 14:01:21

标签: mysql node.js node-mysql

我使用以下驱动程序运行Node.js / MySQL:https://github.com/mysqljs/mysql

该应用程序可以运行,但是如果我每秒调用该函数超过2-3次,则查询开始执行速度非常慢,有时需要几分钟才能执行10-15次查询。

即使有关于如何自行调试的提示,我也将不胜感激,我真的不知道如何看待后端发生的事情。

以下是后端的代码:

//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);
        });
    });
});

前端:

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

$scope.trywrite = function() {
    $http.get('/write')
}

0 个答案:

没有答案