A
A
Adel Khalitov2020-02-26 23:07:22
JavaScript
Adel Khalitov, 2020-02-26 23:07:22

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 раз
}

This feature will take approximately 300 days to complete, provisionally. The option does not fit for a long time

. I saw an implementation like this
let goodsObj = {};
let promises = [goods.findOne(id, goodsObj), balances.findOne(some, goodsObj) ...200 обращений в бд];
Promise.all(promises);

The speed is faster, the load on the base is good, in this case each method in the promises array simply adds a new key with some value to the goodsObj object.

There is a variant, but I tried to debug a similar project for 6 services in such an implementation, after that I wrote with a pen in my passport of a citizen of the Russian Federation that I would never implement it like that.

I am sure that there is a 3rd option, the most correct way to implement it.
The database is absolutely any monga, muscle, etc. Please give direction

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romaboy, 2020-02-27
@adelkhalitov

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

Such a construction in one_result will return the json of one record, in the second_result array, these two requests are not related to each other in any way, they are executed in one call, the base parallelizes from itself, everything is cool and great, but for developers it is too complicated and I almost never see it anywhere I saw when I wrote - I caught sidelong glances, the libraries themselves do not know how to generate this. (I’m just writing one so that I can do it, the most difficult thing remains is the dock)
In Monga, the analog of join is called lookup, goods.findOne(id) is similar to Monga

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question