laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

400+ queries to fetch all descendants of root

Open hekin1 opened this issue 4 years ago • 6 comments

I have a large tree of nodes containing several root nodes with many children in each. When trying to fetch all the children of one of the root nodes with a simple query:

$children = Group::descendantsAndSelf($root_id);

then the result is 400+ queries against the database. image

Is this by design?

I'm using laravel-nestedset version ^5.0

hekin1 avatar Apr 03 '20 11:04 hekin1

That doesn't sound right. A single query should be enough to fetch an entire tree or subtree.

judgej avatar Apr 16 '20 23:04 judgej

Debugged this a bit further. The 400 queries comes from the function getNodeData in Kalnoy\Nestedset\QueryBuilder.php :

$data = $query->first([ $this->model->getLftName(),
                                $this->model->getRgtName() ]);

hekin1 avatar Apr 30 '20 07:04 hekin1

Any solution for this?

tiagofcp avatar Jun 29 '20 09:06 tiagofcp

@tiagofcp It doesnt seem like there's any interest in fixing this bug, my workaround was to query the left and right properties of the root group, and put them in a native query. Looks really ugly but gets the job done in 1 query.

hekin1 avatar Oct 09 '20 15:10 hekin1