Question

I have a Many to Many relationship Between what are called

Objects and ObjectGroups.

An ObjectGroup is just a 'Group' or Objects and an Object can belong to many Object Groups.

For example

ObjectGroupA
-------------
ObjectA
ObjectB
ObjectC

ObjectGroupB   
-------------
ObjectA
ObjectB
ObjectC
ObjectD

These ObjectGroups belong to a device

I have put the Objects and ObjectGroups into the Database via multicreate but now I need to build up the Many-to-Many relationships ( I wasn't able to work out how to do this via multicreate. I did read the documentation). like below;

my $device = $device_rs->create(
 {
   devicename      => $deviceName,
   objects         => \@objects,
   object_groups   => \@objectgroups
 }
);

Then for the relationships I build up a hash of arrays where the ObjectGroup(Parent) is the key and the array contains the Names of the objects. Then for each object group I move through the list of child objects and add them via add_to_$rel.

foreach my $parent (keys %childobjects) { 
  my $parent_objectgroup = $device->object_groups->find({objectgroupname => $parent}); 
    foreach my $child (@{$childobjects{$parent}}) { 
      my $child_object   = $device->objects->find({objectname => $child}); 
        if ($child_object) { 
          #add the child object from the array to the object-group 
          $parent_objectgroup->add_to_childobjects($child_object); 
        } 
     } 
} 

This works, but is VERY slow as I have a lot of relationships to build up. Can anybody suggest a faster way to do this with DBIC?

Was it helpful?

Solution

Okay so unless I am totally lost on this, there is one thing that seems to spring to mind.

You seem to be populating bulk data into these tables as some kind of initial load or conversion or something. And it would seem that from your last routine you already have some concept of which devices have which objects and which group and so on by the matching that you are doing.

With that in mind, it seems the logical thing to do is just populate each table with the relational keys you already know about. Loading up each table is going to be a lot faster than looping through and trying to work out the relations that you should already know in your data.

The operations you are trying to use to bulk load are intended for more atomic purposes where you are going to add new relations in a many to many context. These will work well programatically, as you add an additional device or object group etc.

But as you should already know the associations of this bulk data, just load it into the tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top