I am using node js 10.16.0 and the node-mssql module to connect to a DB. Everything works fine and my simple queries work fine.
If I try to stream data from a query, using the node-mssql example , the first time I execute its very slow. It doesnt show a Timeout Error, but takes about a minute or more to complete.
According to the console log, it brings the first 55 rows and then stops for a while. It looks like it takes some time between the "sets" of data, as I divide them, according to my code below . If I execute the same query a second or third time, it takes only a second to complete. The total amount of rows is about 25.000 or more
How can I make my stream queries faster, at least the first time
Here is my code
following the example, the idea is, start streaming, set 1000 rows, pause streaming, process that rows, send them back with websockets, empty all arrays, continue with streaming, until done
let skate= [];
let leather= [];
let waterproof = [];
let stream_start = new Date();
const request = new sql.Request(pool);
request.stream = true;
request
.input('id_param', sql.Int, parseInt(id))
.input('start_date_param', sql.VarChar(50), startDate)
.input('stop_date_param', sql.VarChar(50), stopDate)
.query('SELECT skate, leather , waterproof FROM shoes WHERE id = @id_param AND CAST(startTime AS date) BETWEEN @start_date_param AND @stop_date_param ');
request.on('row', row => {
rowc++; console.log(rowc);
rowsToProcess.push(row);
if (rowsToProcess.length >= 1000) {
request.pause();
processRows();
}
});
const processRows = () => {
rowsToProcess.forEach((item, index) => {
skate.push(item.skate);
leather.push(item.leather );
waterproof.push(item.waterproof);
});
measurementsData.push(
{title: 'Skate shoes', data: skate},
{title: 'Leather shoes', data: leather},
{title: 'Waterproof shoes', data: waterproof}
);
console.log('another processRows done');
//ws.send(JSON.stringify({ message: measurementsData }));
rowsToProcess = [];
skate= [];
leather= [];
waterproof = [];
measurementsData = [];
request.resume();
}
request.on('done', () => {
console.log('rowc , ', rowc);
console.log('stream start , ', stream_start);
console.log('stream done , ', new Date());
processRows();
});