I was wondering if some people would talk about design methods for object-oriented applications. I have a project that, the more complex it gets, the more queries I am executing, and it's starting to lag a bit.
Here's my scenario in a nutshell - I made an art history timelines app. you select some art periods, hit submit, and it goes to the db.. loads up all the artworks within those periods, which are all objects. then it gets the artist information and some other stuff, which are all additional objects, each one containing the capability to make a database call to load itself up. Then it cycles through al of the objects and builds the timeline, along with an image gallery. So if the timeline I'm generating at a given moment has, say, 150 events in it, there's as many as 300-500 sql calls going on in order to render the page.
My code is kind of like this-
$res = new resource(); // in this instance an artwork
$res->set_id(n);
$res->load();
$res->load_artists(); // an array of person objects associated with this artwork. i keep the query seperate from load cuz sometimes i don't need it
$res->load_pics(); // gets the image URLS. separate for same reason as above
...at which point i have a handy artwork object, which has all the info about it and i can access it and put it on the page.
... what's also cool is that artists can load up all their artworks, etc.... i really like keeping all the necessary sql code to add/updte/delete/load inside the objects
You could substitute any kind of app for what I'm doing, I'm sure the problem is common: load up a couple thousand of these babies and you're gonna be twiddling your thumbs.
What's the best way to address this? I really love using objects and would not like to stop!! (ie and revert back to many pages of procedural code). Because this app has lots more functionality than the situation listed above. OOP makes it possible to get stuff done fast!
So, the goal is a more efficient implementation that runs faster.
I see a couple of options:
1) create another couple of 'mediator tables' that store info in a way that is specifically designed for that particular query. then keep it up to date as changes are made to the main tables.
2) figure out a way to cut down on hitting the db and use some kind of cached data, like in XML documents. While this is a sexy option, I don't know if it's any faster.
3) somehow make my objects more craftily so that they don't make as many db calls, but I dunno how I'd do that!!
That's it! I'd be interested in input from anyone about this, as I spend a lot of time musing on it....
thanks
Frizzo