Adding an object to a very large to-many relationship fulfills the relationship fault, is too slow.

Hi all and thanks for having a look. I'm looking for some performance advice.


Long story short, we have too many rows in our tables. We are working on changing things around so that we can keep fewer rows in our database, but short of that I'd like to ask about adding an object to a very large to-many relationship.


This line of code is slow enough to cause me problems.


appliedModifier.modifier = modifier;


The relationship is: Modifier <--->> AppliedModifiers, or said another way: AppliedModifiers have one Modifier, Modifiers have many AppliedModifiers.

Problem is that the AppliedModifier table has nearly a million rows in it. Turning on core data performance logging on my trusty slowest-iPad-in-the-office I see this for one such query.

CoreData: sql: SELECT 0, t0.Z_PK FROM ZAPPLIEDMODIFIER t0 WHERE  t0.ZMODIFIER = ?
CoreData: annotation: sql connection fetch time: 0.0384s
CoreData: annotation: total fetch execution time: 0.0399s for 5699 rows.
CoreData: annotation: to-many relationship fault "appliedModifiers" for objectID 0x16ce73e0 <x-coredata://1E039A81-ECA1-42EF-9071-43AEE7C90995/Modifier/p155> fulfilled from database.  Got 5699 rows


So it looks like the relationship (and not the objects themselves) is having it's fault fulfilled by pulling all the primary keys? I also note that the log says that it takes about 4 hundredths of a second even though it is quite possible to perceive a delay just stepping over this line of code in the debugger (the real time it takes to execute this line of code is much bigger). Instruments shows a very busy CPU with most of the time spent on firing the didChangeValueForKey notification...


5.14 s   69.2% 0 s    -[OrderLine applyModifier:withAppliedGroup:]
4.97 s   66.8% 0 s     _sharedIMPL_setvfk_core
4.96 s   66.8% 0 s      _PF_ManagedObject_DidChangeValueForKeyIndex
4.96 s   66.8% 0 s       -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:]
4.96 s   66.8% 1.00 ms            NSKeyValueDidChange
4.96 s   66.7% 0 s         NSKeyValueNotifyObserver
4.96 s   66.7% 0 s          NSKVONotify
4.96 s   66.7% 0 s           -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:]
4.96 s   66.7% 1.00 ms                -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:]
4.95 s   66.7% 0 s             -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:]
4.95 s   66.7% 0 s              -[NSManagedObject(_NSInternalMethods) _includeObject:intoPropertyWithKey:andIndex:]
4.63 s   62.3% 0 s               -[_NSFaultingMutableSet containsObject:]


My question is: Other than making my tables smaller (working on it!), is there any way to speed up this assignment?


Thanks again!