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.