Skip to content

N+1 problem with polymorphic #27

Description

@simplepersonru

I use polymorphic feature and there is a known problem N+1

Small presentation

#pragma db object polymorphic
struct Base
{
#pragma db id auto
int64_t rowid = 0;
};

#pragma db object
struct Person : Base
{
int age;
};

#pragma db object
struct Employee : Person
{
bool onVacation;
};

//... 
void foo(odb::database& conn)
{
// have 100k Person objects in DB.
conn.query<Person>(); // fast (the best case)
conn.query<Base>(); // slow (the worst case)

// have 50k Person and 50k Employee
conn.query<Person>(); // slow (the middle case). 50k Person fast, another 50k Employee slow
conn.query<Employee>(); // fast (the best case)
conn.query<Base>(); // slow (the worst case)
}

Description

I saw this commit f7a8c80, it is clear that this direction is developing. The first question is - are there plans to expand the logic to polymorhic?

The intended implementation is such that N+1 can be reduced to M+1, where M is the number of different tables that need to be loaded. That is, as part of the first query, we collected all PK + typeid, then for each individual typeid, a group query is made

-- ...
WHERE PK IN(5, 6, 7, ...) 
-- ...

for each unique typeid, based on the information that was received from the first request.

The second question is, assuming that this patch is prepared by the community, are you interested in the development of polymorphic, or is it more of an experimental semi-living thing with very limited use that you don't want to touch? In other words, is it possible to expect feedback on such work?

The third question is, assuming that such a development can be sponsored, how much would you rate such a refinement?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions