Poor performance of greenDAO @toMany relation

294 views Asked by At

I'm using greenDAO as a local DB in my App to store a parent-children relation (simplified example).

@Entity
class ParentDB {

    @Id
    @Unique
    @NotNull
    private String id;

    @ToMany(referencedJoinProperty = "parentId")
    private List<ChildDB> children;
    ...
}

@Entity
class ChildDB {

    @Id
    @Unique
    @NotNull
    private String id;

    private String parentId;
    ...
}

There are about 4000+ ParentDB objects. And at least as many ChildDB objects. Now in order to map my business objects (BOs) from the DB objects, I have to iterate through all parents and get the children for each one.

List<Parent> parentList = new ArrayList<>();

for(ParentDB parentDB : ParentDBDao.loadAll()){
    Parent parent = new Parent();

    List<ChildDB> childrenDB = parentDB.getChildren()); // <- PROBLEM

    ... map each child to a BO ...

    parent.setChildren(children);
    parentList.add(parent);
}

This operation takes about 16-30 seconds! When removing the problematic line, it takes about 400 ms to load all the Parents (which still is quiet slow afaik)

Internally the generated getChildren() method performs an additional query similar to this:

List<ChildDB> childrenNew = ChildDBDao._queryParentDB_Children(id);

My guess is that 4000 of these queries in the loop cause the poor performance.

As a workaround I am currently loading ALL children and doing the association to their respective parents manually in code. This takes about 800ms, which is acceptable but defeats the purpose of an ORM DB such as greenDAO. This is especially true when the ChilDB contains another Child2DB and BO mapping cannot be abstracted away easily but has to be done manually.

Am I following the wrong approach or is there a way to remove the bottleneck?

0

There are 0 answers