if all this data is needed, it can be loaded out. Otherwise, load it on demand and load it when you need it.
you can do this when the amount of data is small
the subject stores what business he wants to solve. Company.User.Address [j] this way feels that it is convenient, and does not save the efficiency of the query. If it is simply to facilitate the list, unless the demand is very frequent, it can save construction. In other cases, I don't see any advantage in efficiency.
this kind of data structure is only an external interface. The backend may not always read it all from the database at once, and you can also use lazy loading method, and you can adjust
to what extent you are lazy.
Hello, I am recently writing an orm framework for java, which happens to be related to this aspect. When I designed it before, I also considered getting all the database data at once, but I encountered the following problems when I was designing:
< H1 > 1. The code is complex < / H1 >
consider the following objects:
class Province{
private long id;
private City[] cities;
}
class City{
private long id;
private Country[] country;
}
in terms of mapping relationship, one-one mapping and one-many mapping are easier to deal with, but if there are multiple mapping relationships like Province--> City--> Country and include collections, the design is very complex. I have looked at the documentation of nutzDAO, which is actually implemented by pushing the objects in the array into the queue and inserting them as a single object, but there is no doubt that if you insert a large amount of data, this performance must be very poor. When I designed the orm framework, I felt that for complex mapping relationships, not only the code was complex, but also the performance was difficult to guarantee, so I decided to abandon this one-time take-out and one-time save method.
so my final design idea is as follows:
class Province{
private long id;
}
class City{
private long id;
private long provinceId;
}
regard the foreign key as an ordinary member variable and query the foreign key as a conditional query. When saving, first save the Province variable, get the id, then insert the City variable and set their proviceId to the acquired id. Simplifying design also improves performance.
< H1 > 2. Performance < / H1 >
as your question said, in fact, most of the time, we do not need to extract all the relevant data and make a balance between performance and code complexity. In fact, when querying data, the logic of a single table query is the clearest. Programmers are not prone to mistakes when developing, and can be customized according to their own needs. Especially for enterprise projects, multiple entities are related to each other, and if they are extracted at one time, the amount of data may be large, and the programmer only needs a small part of the data, so the loss in performance outweighs the gain.
< H1 > Summary < / H1 >
to sum up, my answer is that fetching all data at once (saving all relevant entity data at once) adds code complexity and performance burden for simplicity and performance.
in addition, I recently referred to some orm frameworks in the market, and wrote a Java open source ORM framework that is still in the snapshot period (API is unstable). It is limited to community specifications. If you are interested, you can communicate with me privately.
in addition, I have recently seen several technology-related communities in China. I feel that the technical atmosphere of many communities is not very good, dressed in the skin of programmer communication and technology exchange, but almost all kinds of irrigation, communication, marriage, writing style is almost no different from the paste bar. What makes me feel some anger and shame is that some foreigners of Chinese "developers" open source projects on github (I don't know if there are any meaningless irrigation posts in the issue area of Chinese developer github projects), and comments have more than ten floors, completely Chinese style of writing (for example, some people comment on "six,six,six", "Please sit down, Mr. Chen", "double Click"! Six "), although several Chinese developers have apologized for such behavior, they still feel the atmosphere and shame when they read this behavior.
noted that segmentFault just recently saw a topic post about the increase in the number of users, and more stringent questions will be put forward in order to standardize the answers. I know it is difficult to strike a balance between the number of users and the technical atmosphere (unless the overall quality of the Chinese people is greatly improved according to the proportionality principle), so I very much hope that the segmentFault management team will try their best to maintain the technical atmosphere of the whole community. I also decided to take root in the segmentFault community and answer some questions that I can.