Answer the question
In order to leave comments, you need to log in
How to speed up the work of a method in js or how to make a mass call to the database?
The question is very simple. There is a method that performs a simple task. For example.
There is a product that is stored in the good table, there are balances of goods stored in the balances table, there are 200 more necessary parameters that relate to the product.
Implementation example
async some(id) {
let good = await goods.findOne(id);
let balance = await balances.findOne(some);
.... итп так 200 раз
}
let goodsObj = {};
let promises = [goods.findOne(id, goodsObj), balances.findOne(some, goodsObj) ...200 обращений в бд];
Promise.all(promises);
Answer the question
In order to leave comments, you need to log in
The Promise.all method not only waits, but also returns the results, so the object is not needed:
If there are really 200 of them, then the object will certainly be better) cases, this is certainly a very unusual task.
Further it refers to postgres, I don’t know Mongo:
In order not to optimize either the database or the queries themselves, you can use a connection pool.
One connection to the base (one socket) can simultaneously wait for only one response, that is, exactly 0 will be useful from promises.
Many requests can be sent to the base at the same time through one connection:
const [a, b] = await Promise.all([query1, query2])
SELECT 1; SELECT 2; SELECT 3
Bases understand the semicolon, they can parallelize, but that's the trouble! Most likely the js library does not know how. Okay, I'm not really sure that this will be executed in parallel, but the databases can definitely respond to such a connected request and it would work faster than 3 separate ones.
Postgres can also turn a string into json and you can write something like
SELECT
(SELECT row_to_json(table.*) FROM table LIMIT 1) AS one_result,
(SELECT json_agg(row_to_json(table.*)) FROM table LIMIT 1) AS second_result
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question