Re: Best Design Practice
Re: Best Design Practice
- Subject: Re: Best Design Practice
- From: wojingo <email@hidden>
- Date: Thu, 13 Apr 2006 11:53:10 +0930
Owen McKerrow wrote:
Hi All,
Below are 2 methods that do the same thing, that is return an array of
active objects from a relationship. In this case I have a Group which
has many PostiionSets, some of which are active and some of which are
not. What Im wondering is which of the 2 methods is better and or
quicker. An is this a case by case issue or is one way considered
better/quicker than the other ?
The first method just uses the groups postionSets() array and does the
search and sort in memory. the second way uses a fetch spec and gets
the DB to do the search and sort.
/**
Return an arrray of the active postion sets of this group in
sorted order.
*/
public NSMutableArray sortedPositionsSets() throws Exception
{
//Filter the positions for just the active ones
NSMutableArray qualData = new NSMutableArray(new Integer(1));
EOQualifier qual =
EOQualifier.qualifierWithQualifierFormat("active = %@",qualData);
NSArray activeSets = EOQualifier.filteredArrayWithQualifier
(positionSets(),qual);
return
(NSMutableArray)activeSets.sortedArrayUsingComparator(new
OMGenericComparator());
}
/**
Return an arrray of the active postion sets of this group in
sorted order.
*/
public NSMutableArray sortedPositionsSets() throws Exception
{
//Filter the positions for just the active ones
NSMutableArray qualData = new NSMutableArray(new Integer(1));
qualData.addObject(this);
EOQualifier qual =
EOQualifier.qualifierWithQualifierFormat("active = %@ AND group =
%@",qualData);
EOSortOrdering order1 = new EOSortOrdering
("order",EOSortOrdering.CompareAscending);
NSMutableArray sort = new NSMutableArray(order1);
EOFetchSpecification spec = new EOFetchSpecification
("PositionSet",qualData,sort);
return editingContext().objectsWithFetchSpecification(spec);
}
I have been using the first method but after talking with a college
yesterday Im wondering if the second way may be quicker.
The in-memory will stil require a trip( or several to the DB ) to get
the position sets, and then have to do the sort in memory once they get
back. But if I have set the relationships Batch Faulting size, would
these objects not already be in memory and thus we then cut down on the
trip to the DB ?
Thoughts and comments welcome.
Owen McKerrow
One thing worth considering is the memory used faulting all those
objects when your not going to actually use them. I have used the second
approach with good results to optimise methods like the ones you have above.
If you know that the set of active records will be a relatively small
number out of potentially thousands, you should fetch and sort those
that your interested in only.
Sorting/filtering at the DB(for any "large" set of data) is always going
to be preferable as the DB is optimised for that type of function.
Sorting using Java is going to be way slower and it will happen on the
box that is more likely to be under load (Maybe).
It really comes down to the number of records that will be filtered as
to which implementation is more appropriate.
An easy way to determine which would be better in your case is to try
both with a simple, println(currentTimeMillis - startTimeMillis)
wrapped around the call. :)
HTH,
regards,
- shaun
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden