Re: can I use fetch spec to filter an array?
Re: can I use fetch spec to filter an array?
- Subject: Re: can I use fetch spec to filter an array?
- From: Denis Stanton <email@hidden>
- Date: Thu, 1 Jan 2004 13:30:33 +1300
Hi Chuck
Lots to think about here. I'm going to consider all this off-line for a
while.
Some comments:
On Thursday, January 1, 2004, at 12:04 PM, Chuck Hill wrote:
Hi Denis,
At 10:19 AM 01/01/2004 +1300, Denis Stanton wrote:
OK, so my biggest error was to apply @sum 11 times for each agent,....
I don't know if that was your BIGGEST mistake (see below)
OK, I knew I was setting myself up for that one :-)
It made no measurable difference. The timing of consecutive
executions
varies so much without changing the code that I cannot get an accurate
comparison.
OK, so that tells us the bottleneck lies elsewhere.
I put in log messages to count the milliseconds at several point in
each loop. The log messages themselves distorted the timings, but what
I did observe was:
On the first attempt, loading takes much longer than calculating,
particularly when there are many booking per agent (some agents have
only one booking, some have 200)
On the second attempt the two phases take similar times.
This is consistent with the different speeds of DB and memory access.
There is an apparently random delay cropping up every now and again,
making the loop take much longer for some agents. It is not
consistently associated with particular agents - the next attempt will
be different. My guess is that this is garbage collection cutting in
and taking up some cycles.
You've overlooked a crucial part of my previous message:
"...I'd fetch all the bookings in the time frame with a fetch spec set
to
prefetch all the agents. This should give you all the data that you
need
in two fetches (one for bookings, one for agents).
Does this mean that I could fix the slow response time on the first
execution by simply reading all bookings (within date range) into an
array before beginning the other processing, even if that array is not
otherwise used in my program? A single fetch is more efficient that
the multiple fetches that may be requested when my code seeks to follow
the relationships. Would EO figure out that all the bookings were now
available in memory and this save the time it was spending on trips to
the database? A quick test suggests that this does not help.
I expect that your performance problems stem from one or more of the
following:
1. Too many trips to the database
2. Fetches that can't be optimized with an index
3. Excessive garbage collection caused by memory starvation.
Let's start with the first one. The only objects in the report are
Agent
and Booking. We should be able to get all of the data needed with two
fetches. I'm assuming that there are few agents excluded from the
report
but presumable a lot of Bookings (from past years) will be excluded.
Close enough. There are probably a few agents without bookings this
year, but if the db covers say four years of increasing business then
this year's bookings would be something between 25% and 50 % of the
total.
// Fetch one: all the agents
NSArray allAgents = EOUtilties.objectsForEntity(ec, "Agent");
// Fetch only the bookings that we will report on:
NSArray bookingQualifiers = new NSMutableArray();
bookingQualifiers.addObject(
new EOKeyValueQualfier("date",
EOQualifier.QualifierOperatorGreaterThanOrEqualTo,
startDate));
bookingQualifiers.addObject(
new EOKeyValueQualfier("date",
EOQualifier.QualifierOperatorLessThanOrEqualTo,
endDate));
bookingQualifiers.addObject(
new EOKeyValueQualfier("status",
EOQualifier.QualifierOperatorLessThanOrEqualTo,
Booking.STATUS_COMPLETE));
// etc.
EOFetchSpecification fetchSpec = new EOFetchSpecification(
"Booking",
new EOAndQualifier(bookingQualifiers),
NSArray.EmptyArray);
// Fetch Two: All appropriate bookings in period
NSArray bookings = ec.objectsWithFetchSpecification(fetchSpec);
That's approximately what I have, but with rather more typing than I
am accustomed to. Does the code above offer any advantage over the
code below? Maybe you are just trying to give the most general case,
or is there a penalty on using qualifierWithQualifierFormat?
NSMutableArray args = new NSMutableArray();
args.addObject(firstDate);
args.addObject(lastDate);
qualifier = EOQualifier.qualifierWithQualifierFormat("startDate
>= %@ AND startDate <= %@", args);
fetchSpec = new EOFetchSpecification("Booking", qualifier,
null);
editingContext = session.defaultEditingContext();
NSArray bookings = new
NSArray(editingContext.objectsWithFetchSpecification(fetchSpec));
Notice carefully that I am *not* using agent.bookings().
agent.bookings() looks so easy! I now see it as a potential trap
If only some of
the bookings are on this report, there is no need to fetch them from
the
database, nor to consume large amounts of memory by turning them into
objects. You may, in fact, discover that you do not want bookings()
as a
class property of Agent as each time it is referenced it will pull in
(at
least a reference) to each and every booking ever associated with an
Agent.
Now you've got me thinking. I have many relationships in my model
because that seemed the logical way to organise the data and still have
easy access to it. I wonder how much overhead I am adding if EO is
pulling in references for relationships that are rarely followed.
The next thing to check is how the database is locating the data. It
is a
good idea to add an index on date to the table for Booking.
Already did that.
Because when you click the link to get off of this page WO does not
know
which component it is for. It must reevaluate each repetition and each
conditional so that it can ask the child of each if it is responsible
for
that action.
This seems like a very bad thing. I click the submit button in a
little query form at the top of the page and WO has to re-expand the
whole rest of the page before it can figure out that the submit button
was clicked? This page has no links from inside the repetition. There
seems to be no reason to re-expand the page.
Denis
Denis Stanton
email@hidden
Home: +64 9 533 0391
mobile: +64 21 1433622
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.