Sails.js One to many speeches

I compared the sql query on sails.js with another way of using it using OVLN.

I made the following request: Get all countries from all continents and I compared both requests with wireshark.

Simple SQL Query:

"SELECT * FROM countries AS cou INNER JOIN continents AS con ON (cou.continent_id=continent.id)"

      

And then I did the same thing that defined one-many associations between countries and continents and made the following request.

Continents.find().populate("countries").exec(function(err, result)) {
  res.send(result)
}

      

So it takes about 66ms to return the result, given that I have 15ms of network latency, I can go down to 50ms by moving the node.js server.

When I do this with an sql query, it takes about 35ms, so I can go down to almost 20ms, which is fine for me.

Is there a way to get the same results using both methods? or will the sql query always be faster?

+3


source to share


1 answer


Actually, the query generated in such a population is 1. Parents' choice:

select * from continent where ...

  1. Selection of all countries on which continents were found.

(select * from country where continent_id = continent_1) union (select * from country where continent_id = continent_2) union ... union (select * from country where continent_id = continent_n)



  1. Result of regrouping (Impact of each country on its continent using a foreign key.

This implementation makes it easier to manage constraints and skipped clauses as a call:

Continents.find (). Populate ("countries"). Limit (2) Skip (1) .exec (function (uh, result)) {res.send (result)}

should only return the second and third country for each continent, and such an implementation, as you can see, generates only one query, so the DBMS will not overload.

+1


source







All Articles