I am learning Node.
Using Express and Node-Mysql, I am successfully able to query my mysql database and return the result to the client as JSON.
However, it is far too slow. For a simple query with a LIMIT of 100 it is taking about 6 seconds to get the response.
According to my logger, the DB query only takes about 8 ms to run - so I do not think that node-mysql is the problem.
But the only other thing I am doing is passing the resultset to the response object to be converted into json.
Here is my code, roughly:
Route:
app.get( "/exercises", function( req, res ){
exercises.get( req.query, function( result ){
res.json( result );
});
});
Model:
module.exports.get = function( params, cb ){
var sql = "SELECT * FROM exercises LIMIT 100";
db.do( sql, function( result ){
var response = {};
response.data = result[ 0 ];
response.meta = result[ 1 ][ 0 ];
cb( response );
});
};
DB:
module.exports.do = function( sql, vals, cb ){
var selectStart = +new Date();
pool.acquire(function( err, db ){
db.query( sql, vals, function( err, result ) {
cb( result );
console.log( "Query Execuction: %d ms", +new Date() - selectStart );
pool.release( db );
});
});
};
Does anyone know what I could be doing wrong / why it is taking so long to send the resultset to the client?
Thanks (in advance) for your help.
I'm not familiar with pool, and unfortunately I can't comment yet. One thing I noticed is that you are calling db.do with two arguments, while it takes three. This should raise 'undefined is not a function' when you try to callback.