简体   繁体   中英

Node JS - Sequelize Too many connections - CPU usage 100%

I am using a third party websocket connection. Websocket sends data with array length of 100+. So I have to check every record into database table to do some calculations (required).

May be in this loop it is causing memory and high CPU usage.

My db Config.

const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD, {
  dialect: 'mysql',
  host: DB_HOST,
  port: DB_PORT,
  logging: false,
  dialectOptions: {
//  useUTC: true
  },
  timezone: '+00:00', 
  pool: {
    max: 500,
    min: 0,
    idle: 10000,
    // @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
    acquire: 100*1000,
  }
}); 

One Loop code:

for(const theOdd of records) {
  
  await odd_records.count() // First query
  await matches.findOne() //Second query
  .
  .
  .
 //End with 5-6 queries for each row.
}

How this can be optimized?

Any help?

Let's say, You are querying 6 times for each record (assume there are 100 records). So, you are querying 600 times.

I assume there exist one unique id for each record.

  1. Do the constant value query out of the loop that returns the same result all the time.
    await odd_records.count() looks constant value to me.
  2. collect all the record's id, and fetch all the specific data for each record in one query.
  3. Further, use Promise.all for queries if possible.

It will reduce the number of queries to 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM