Nodejs mysql query blocking loop

I had this problem causing blocking issues in a full blown application, I managed to boil it down to this code:

"use strict";
var mysql = require('mysql');
var blocked = require('blocked');

blocked(function(ms) {
  console.warn('event loop blocked for', ms, 'ms');
});

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : '****',
  password : '****'
});

connection.connect(function(err) {
  console.time('query');
  connection.query('SELECT * FROM db.table', function(err, rows, fields) {
    console.log('rows:', rows.length);
    console.timeEnd('query');
    connection.end();
  });
});

      

I am getting this output:

event loop blocked for 445 ms
event loop blocked for 399 ms
event loop blocked for 496 ms
event loop blocked for 388 ms
event loop blocked for 356 ms
event loop blocked for 345 ms
event loop blocked for 354 ms
event loop blocked for 346 ms
event loop blocked for 333 ms
event loop blocked for 344 ms
rows: 115194
query: 4856ms

      

This, of course, has some pretty harsh implications for my server, as incoming requests have to wait these times, and since every incoming connection fires a request that causes a lot of blocking, this means concurrency increases response times. Also, I really don't understand the reason for this as I thought the IO in node.js should be non-blocking. By the way, you can also see that my table is not that big.

I would really appreciate any help, and even if you don't have a solution, I would appreciate an explanation.

thank

+3


source to share





All Articles