To Q1: Are the Home objects really so huge? In the other case, any optimization here will hardly have a reasonable effect.
To Q2: You can use newQuery with join here to spare one database request (but I am not sure here), but your code will be a bit less readable.
But IMHO the most serious problem might be you have not a database table for the home-lot pairs. Using such table, you might be able to retrieve all the pairs with joined objects using only one database request (via getCollectionGraph).
Hi pavsyk, many thanks for the reply!
As for Q1 - they are a reasonable size but not *huge*, so I guess it wouldn’t be a big problem. Just wondered if there can be a more efficient way to do it.
Q2 - thanks, I will try that out and see if there is a performance difference.
As for your suggestion regarding Home-Lot pairs, this is something I had wondered about. Homes and Lots are in no way related, but I need to list results containing one Home and One lot per result. It would be a Many-to-Many situation, with roughly 100 Home designs and potentially thousands of lots. The resulting intermediary table could have up to 500,000 rows!
I don’t know a lot about how MySql works, but would this likely be slower than just making the two queries?
Hi lucas,
IMHO, you definitely have a relationhsip between homes and lots now, although not in a table, but in your $shortlist array. Or is it a set of random homes against a random set of lots?
Your approach in the code above leads to (IMHO) 3 database queries per each home-lot pair. The performance will depend on how many pairs you have and how many concurrent users will use it. E.g. if you will query the database with 5-10 pairs once a minute, it is waste of time to try optimize it. But in case of a couple of tens or hundreds pairs being queried each couple of seconds - you should definitely think about another approach. If it is not a typical many-to-many relationship, you can for example query the database once for a collection of homes and once for a collection of lots (possibly including the estates). And to pair them afterwards.
It’s more like random pairings of homes and lots, as selected and shortlisted by visitors as they browse the site. Almost any combination is possible (there are some size restrictions but not important here). So you’re right, there is a relationship... but it’s decided by the visitor and not something I can control.
Certainly for now the code doesn’t need optimising for performance - this is more out of my own interest and from a best-practice point of view. The lot/estate pairs can be brought down to a single query as they are related. Is that an inner join? I’ll give that a try for now.
Really appreciate your thoughts!