I have an API with Express/Node.js with MySQL DB. I'm using the Mysql2 npm package (no ORM) for queries. For some particular routes, I have some queries running in parallel (which are not exactly related, so no joins can be performed). I want to know whether using a stored procedure can improve my performance, especially considering the non-blocking behavior of Node.js. I'm mainly concerned with 2 parameters:
- Query time and
- Server overhead.
To rephrase, if I changed from individual queries to stored procedure, what will be the effect on my response time and server utilization?
Edit:
What I mean by queries running in parallel is, I have a function set up, so that the queries can run independently (without waiting for the response of the previous query) and once all promises resolve, my function returns the results as a Promise.resolve.
A little snippet:
const makeThreeQueries = (q1, q2, q3) => { # queries being array with 2 elements: ['query string', ['Array', 'of', 'Args']]
return new Promise((resolve) => {
const results = []
const queryHandler = (result) => {
results.push(result)
if (results.length === 3) resolve(results) # 3 in case there are 3 queries
}
db.query(...q1).then(queryHandler)
db.query(...q2).then(queryHandler) # doesn't wait for q1 to finish
db.query(...q3).then(queryHandler) # doesn't wait for q2 to finish
})
}
Of course the main code is much more robust than this, with proper error handling and validation/positional consistency in place.