Search code examples
node.jsnode-mysql

How to use node-mysql without loads all the rows into the memory?


I'm using NodeJS. I want to do something to 1,000,000 rows without loading all the rows into the memory. (for-each)

Before, when I used ASP Classic I did:

do while not rec.eof
   //do something
   rec.movenext
loop

In node-mysql I didn't found anything that similar to cursor. only

connection.query('select * from bigdata',function(err,rows))

The problem is, that I don't want to loads all the rows at one time.

I found an answer, and put it below. I keep the question to help others who will have the same question


Solution

  • Thanks to Ziggy Jonshon, I found something similar to cursor in node-mysql, using stream.

    I can get the query result by stream, and pause it automatically:

    See Issue #1370 on node-mysql:

    const mysql = require('mysql');
    const stream = require('stream');
    
    connection.query('SELECT * FROM `bigdata`')
      .on('error', function(err) {
        // Do something about error in the query
      })
      .stream()
      .pipe(new stream.Transform({
        objectMode: true,
        transform: function (row, encoding, callback) {
          // Do something with the row of data
    
          callback();
        }
      }))
      .on('finish', function() {
        connection.end();
      });
    

    This way I don't need to load all the data into the memory of the node process.