Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:49:43+00:00 2026-05-23T16:49:43+00:00

This question is intended to be software / platform agnostic. I am just looking

  • 0

This question is intended to be software / platform agnostic. I am just looking for generic SQL code.

Consider the following (very simple for example’s sake) tables:

Table: Authors
id   | name
1    | Tyson
2    | Gordon
3    | Tony
etc

Table: Books
id   | author   | title
1    | 1        | Tyson's First Book
2    | 2        | Gordon's Book
3    | 1        | Tyson's Second Book
4    | 3        | Tony's Book
etc

Table: Stores
id   | name
1    | Books Overflow
2    | Books Exchange
etc

Table: Stores_Books
id   | store   | book
1    | 1       | 1
2    | 2       | 4
3    | 1       | 3
4    | 2       | 2

As you can see, there is a one-to-many relationship between Books and Authors, and a many-to-many relationship between Books and Stores.

Question one: What is the best query to eager load one author and their books (and where the books are sold) into an object-oriented program where each row is representative of an object instance?

Question two: What is the best query to eager load the entire object tree into an object-oriented program where each row is representative of an object instance?

Both of these situations are easy to imagine with lazy loading. In either situation you would fetch the author with one query and then as soon as you need their books (and what stores the books are sold at) you would use another query to get that information.

Is lazy loading the best way to do this or should I use a join and parse the result when creating the object tree (in an attempt to eager load the data)? In this situation what would be the optimal join / target output from the database in order to make parsing as simple as possible?

As far as I can tell, with eager loading, I would need to manage a dictionary or index of some sort of all the objects while I am parsing the data. Is this actually the case or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:49:43+00:00Added an answer on May 23, 2026 at 4:49 pm

    That’s a tough question to answer. I’ve done this before by writing a query that returns everything as a flat table and then looping through the results, creating objects or structures as the most-significant columns change. I think that works better than multiple database calls because there’s a lot of overhead involved in each call, though depending on how many smaller entities there are to each big entity that might not be best.

    The following might apply to both your questions 1 and 2.

    SELECT a.id, a.name, b.id, b.name FROM authors a LEFT JOIN books b ON a.id=b.author
    

    (pseudocode, in your program that makes the db call)

    while (%row=fetchrow) {
       if ($row{a.id} != currentauthor.id) {
          currentauthor.id=$row{a.id};
          currentauthor.name=$row{a.name};
          }
        currentbook=new book($row{b.id, b.name});
        push currentauthor.booklist, currentbook;
        }
    

    [edit] I just realized I didn’t answer the second part of your question. Depending on the size of the data for stores and what I intended doing with it, I would either

    Before looping through books/authors as above, slurp the whole stores table into a structure in my program, much like the book/author structure above but indexed by the storeid, and then do a lookup in that structure every time I read a book record and store a reference to the store table

    or, if there are many stores,

    Join the stores onto the books and have an additional nested loop to add stores objects within the part of the code that adds a book.

    Here’s a relevant Wikipedia article: http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch

    I hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is similar to this MySQL question, but intended for SQL Server: Is
This question was very helpful, however I have a list control in my report,
Ok, this question has been asked many times. Just googling with the error message:
This question is intended to be a discussion of people's personal opinions in handling
Following up on this question, I'm working on a large Delphi 7 codebase which
After following the advice in this question successfully, I added a couple additional lines
While this question is somewhat language agnostic ( agnostic as far as OOP languages
This question is intended for someone who is in game development, possibly working in
Please note that this question is intended to be a bit more on the
This question is based on a previous similar question. I have the following equation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.