Query between multiple Inserts

Asked

Viewed 35 times

0

I have an api that is every second (varying the time according to the response of the webservice) sending data to be inserted into the nodejs(v12.11.0) of several units that call the url http://localhost:3000/api/inserir

const energy = require('../service')
const energia = server => {
    server.get('api/inserir', async (req, res, next)=>{
            const {
                     f1_tensao, f1_corrente , f1_potencia_ativa, medidor
                  } = req.params
            const obj = {
                    f1_tensao, f1_corrente , f1_potencia_ativa, medidor
            }
           const inserir = await energy.energy().insert2( obj )
            try {
                res.send( inserir );
            } catch (error) {
                res.send( error )
            }
            next()
        })

    server.get( 'api/medidor/:ip', async (req, res, next) =>{
            const { ip } = req.params
            try {
                res.send( await energy.energy().valores( ip ) )
            } catch (error) {
                res.send( error )
            }
            next()
        })
}

module.exports = energia

When I try to query data by calling my url: localhost:3000/api/medidor/1, they take too long to be returned

Is it due to the many Inserts that are made almost at the same time?

Because I already tried consuming the api to do the query without the Inserts and the data returns almost instantly.

How can I solve without having to create another api so to perform queries?

This is my npm configuration:

"devDependencies": {
    "dotenv": "^6.2.0",
    "eslint": "^5.14.1",
    "mysql": "^2.16.0",
    "nodemon": "^1.18.10",
    "pm2": "^3.3.1",
    "restify": "^8.0.0",
    "restify-cors-middleware": "^1.1.1",
    "socket.io": "^2.2.0"
  }
  • Dear, have you tried applying some type of filter, for example, date range to see if the query improves? I do not believe that it is necessary to create another api, because it is a lot of data, maybe some process is delaying the answer. What happens within the service?

  • In the service it is subdivided with the functions , updates, Inserts, selects, the filter is already applied, because I look for the sent parameter.. I think I’ll have to use Microservices

1 answer

1


How the database connection is made?

If by chance it opens every time the command is executed there is a big loss of performance, if that is it looks more over connections pools that will drastically reduce the execution time of commands in the database.

Here’s a link that better explains how the connection pool works:

http://cangaceirojavascript.com.br/lidando-com-conexoes-banco-plataforma-node/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.