I
I
Ilya78rus2016-11-15 20:02:07
MySQL
Ilya78rus, 2016-11-15 20:02:07

How to simplify and optimize mysql query in node.js?

Hello.
There are requests to the database with a check for relevance and replenishment of the balance. It is necessary to optimize the query, possibly reducing the processing time. Tk sometimes the request is not even processed. The application is highly loaded and it is necessary to process the request quickly (because there can be more than 1000 such requests to replenish the balance at the same time). I can’t do it myself, so I ask you for help, please help me out

$app.mysql.getConnection(function(err, connection) {
            if(err) {
              self.error('[Balance2] '+ err);
              return;
            }
            connection.query("SELECT * FROM `trade_log` WHERE `tradeId` = ? and `status` = 'send'", offer.tradeofferid, function(err, rows, fields) { //Тут проверка имеется ли такая запись в бд
              if(err) {
                self.error('[Balance3] '+ err);
                return;
              }
              if(typeof rows[0] == "undefined") {
                return;
              }

              connection.query('UPDATE `trade_log` SET status = "success" WHERE tradeId = ?', offer.tradeofferid, function(err, results) { //Если имеется, то ставим статус success
                if(err) {
                  self.error(err);
                  return;
                }

                if(results.affectedRows == 0) {
                  self.debug("NOT UPDATE TRADE OFFER #" + offer.tradeofferid);
                  return;
                }

                connection.query("SELECT * FROM `trade_log` WHERE `tradeId` = ? and `status` = 'success'", offer.tradeofferid, function(err, rows, fields) { //Получаем оставшиеся данные записи
                  if(err) {
                    self.error(err);
                    return;
                  }

                  var sum = rows[0].sum;
                  connection.query('UPDATE `users` SET `balance` = `balance` + ? WHERE steam_id = ?', [sum, offer.steamid_other], function(err, results) { //Пополняем баланс
                    if(err) {
                      self.error(err);
                      return;
                    }

                    if(results.affectedRows == 0) {
                      self.debug("NOT UPDATE USER BALANCE steamid=" + offer.steamid_other + ", sum=" + sum);
                      return;
                    }

                    self.debug("TRADE OFFER #" + offer.tradeofferid + " ОТ: " + offer.steamid_other + " БЫЛ ПОДТВЕРЖДЕН");	  
      $app.io.emit('deposit', {
        steam_id: offer.steamid_other,
        sum: sum,
      });
                  });
                });
              });
            });

            connection.release();
          });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-11-15
@Ilya78rus

UPDATE `trade_log` AS `tl`
  JOIN `users` AS `u` ON `tl`.`tradeId` = ? AND `tl`.`status` = 'send' 
    AND `u`.`steam_id` = `tl`.`steamid_other`
  SET `tl`.`status` = 'success', `u`.`balance` = `u`.`balance` + `tl`.`sum`

PS Corrected a little. I didn't look at where the money comes from. And I haven’t looked yet that I need to send data, which means that I still have to make a request for a selection
SELECT `steamid_other`, `sum` 
  FROM `trade_log` 
  WHERE `tradeId` = ? AND `status` = 'success'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question