问题
Please correct me if I am wrong, but is one complex query less expensive than multiple small queries -> Question? Seems the question listed has opposing views. I was always taught that the most expensive operation is the opening and closing of the connection to the db. If that is the case, what is the most efficient way to bootstrap your data on application startup?
For simplicities sake, lets say my application requires a collection of latest posts, latest categories, the logged in user, and latest tags. Now I could fetch all this data in one query, or I could fetch this data individually. Which one would you recommend? If I went with a single query, how do you usually do this in Backbone, and how would you do this over a Restful API?
Thanks so much
Tyrone
回答1:
backbone-relational (which you mention) provides a solution inside backbone without using jQuery.getJSON as suggested in the related question. Simply return everything in a response to the root query and define relations matching the JSON structure.
For example, I've just written something which returns an array of relevant profile objects in the root of the response, and then uses profile_ids elsewhere. Just be sure to define both the top-level relation and the references (using keySource and keyDestination).
回答2:
Multiple requests are executing asynchronously so total time is not a sum of all request times. As with any optimization you should check potential gain, probably you don't have to bother than with that at all.
There are quite a few possible approaches in the related question (I like Backbone because of such flexibility).
There is another one which you can achieve the goal in zero additional requests with. You can embed all needed data into page's script tags on the server side when generating the page.
来源:https://stackoverflow.com/questions/10927258/bootstrapping-data-with-backbone-using-one-query-as-opposed-to-multiple-queries