baum icon indicating copy to clipboard operation
baum copied to clipboard

19 queries when creating the child node.

Open knvpk opened this issue 9 years ago • 14 comments

When creating a child node of root node it is creating 19 queries to mysql. i dont know it is correct or not. if it creating 19 queries what will the performance of mysql.

$root=Document::find(5);
$child=$root->children()->create([
   'name'=>'child name'
]);

This is my code. please tell me if Im doing it wrong or it really want 19 queries to execute.

knvpk avatar Mar 28 '15 03:03 knvpk

Some body clarify my doubt..

knvpk avatar Apr 05 '15 11:04 knvpk

i dont use that method before maybe try that

$root = Category::root()->first();
$node= Category::create(['name' => 'node 2 ']);
$node->makeChildOf($root);

devmark avatar Apr 06 '15 02:04 devmark

k, @devmark i will check that and inform you

knvpk avatar Apr 06 '15 02:04 knvpk

@devmark , i changed my code as you mentioned then i got this exception "Could not resolve target node"

knvpk avatar Apr 06 '15 08:04 knvpk

oop. sorry. i dont test that.

$node = new Category;
$node->name = "my name ";
$node->save();

//setup parent id
$root = Category::root();
$node->makeChildOf($root);

this code should be work. i am using that code on my project.

devmark avatar Apr 07 '15 03:04 devmark

I'm tested with that code @devmark , but now it executing 15 queries against the database

knvpk avatar Apr 07 '15 07:04 knvpk

i opened the db log, it also executes 15 queries(2 duplicated) nested tree need more time to do insertion method, bcz it need resorting, update parent etc... but it will be more faster to get data

devmark avatar Apr 07 '15 08:04 devmark

But the package like this is only creating 5 quiries when creating the child nodes. actually i dont know how to implement this nested set implementation . if you know please explain diff b/w these packages.

knvpk avatar Apr 07 '15 09:04 knvpk

AFAIK, the number of queries is directly linked to the number of data in database and the position where the node will be inserted.

ravanscafi avatar Apr 08 '15 14:04 ravanscafi

@rscafi is correct @pavankumarkatakam. When doing inserts, left and right values for the entire table needs to be updated. A lot of data is updated and if you have indexes they need rebuilding too. Nested sets have their forte in retreiving data and is slower at inserting data.

torkiljohnsen avatar Apr 17 '15 06:04 torkiljohnsen

Thank You @rscafi and @torkiljohnsen for clearing this.

knvpk avatar Apr 17 '15 12:04 knvpk

@phpspider I've been looking for something that can handle "root scopes". Similar to this -- https://github.com/lazychaser/laravel-nestedset#scoping -- but instead of using a shared lft/rgt numbering system across the entire table, the numbering would be "reset" for each root node.

That concept only solves the issue by a single factor, but sometimes that's all it takes.

trip-somers avatar Aug 29 '19 17:08 trip-somers

Apparently this package does that. Huh.

trip-somers avatar Aug 29 '19 17:08 trip-somers