Friday, November 28, 2008

Re: Finds: Contain vs. Unbind-on-the-fly

Overall, looks like 'depth' and 'recursive' are conflicting and
causing the extra queries.
Anyone had the time to take look?

When this is combined with some (somewhat) basic search/finds, it
exhausts the memory size because so much is going on.

On Nov 10, 5:32 pm, Brenton B <brenton.bar...@gmail.com> wrote:
> After digging around some more, I'm thinking this needs to be
> scrutinized.
> Unfortunately I haven't had much time lately to really pick it apart,
> but will keep updating when I find stuff.
>
> On Nov 7, 12:12 pm, "Xavier Mathews" <xavieruni...@gmail.com> wrote:
>
> > Yeah there is some extra queries and something not right with the ones that
> > already in place!
>
> > Xavier A. Mathews
> > Student/Developer/Web-Master
> > GG Client Based Tech Support Specialist
> > Hazel Crest Illinois
> > xavieruni...@gmail.com¥xavieruni...@hotmail.com¥truestar...@yahoo.com
> > "Fear of a name, only increases fear of the thing itself."
>
> > On Fri, Nov 7, 2008 at 2:04 PM, Brenton B <brenton.bar...@gmail.com> wrote:
>
> > > Wee bit of a running commentary here ...
>
> > > containable.php
>
> > > Line 348 has 'depth' being increased with each level (as it does the
> > > foreach), then subsequently assigned on line 359.
>
> > > This looks like it's set for all models and not just the ones needed,
> > > hence the possibility of extra queries.
>
> > > Confirmed on line 171 where it's recursivity is then set based on
> > > 'depth'. (possibly).
>
> > > Will continue looking ....
>
> > > On Nov 7, 11:13 am, Brenton B <brenton.bar...@gmail.com> wrote:
> > > > Re: "why is it try to select models with id=2":
>
> > > > Because thats the value of country_id and state_id in the Photographer
> > > > record that's being selected:
> > > > WHERE `Photographer`.`id` = 1
>
> > > > On Nov 6, 8:35 pm, teknoid <teknoid.cake...@gmail.com> wrote:
>
> > > > > That's very strange... why is it trying to select models with id=2?
> > > > > Doesn't seem like you pass any conditions to your find()...
> > > > > Are you sure there is nothing else going (maybe in model associations)
> > > > > or any other attached behaviors?
>
> > > > > On Nov 6, 6:34 pm, Brenton B <brenton.bar...@gmail.com> wrote:
>
> > > > > > Where there is only 1 level in the 'contain':
>
> > > > > > SELECT `Photographer`.`id`, `Photographer`.`first_name`,
> > > > > > `Photographer`.`last_name`, `Photographer`.`company_name`,
> > > > > > `Photographer`.`email`, `Photographer`.`telephone`,
> > > > > > `Photographer`.`address_1`, `Photographer`.`address_2`,
> > > > > > `Photographer`.`country_id`, `Photographer`.`state_id`,
> > > > > > `Photographer`.`city`, `Photographer`.`zip_code`, [[[... plus a
> > > bunch
> > > > > > of other fields ... ]]] `Country`.`id`, `Country`.`name`,
> > > > > > `Country`.`created`, `Country`.`created_by`, `Country`.`modified`,
> > > > > > `Country`.`modified_by`, `State`.`id`, `State`.`name`,
> > > > > > `State`.`country_id`, `State`.`created`, `State`.`created_by`,
> > > > > > `State`.`modified`, `State`.`modified_by` FROM `photographers` AS
> > > > > > `Photographer` LEFT JOIN `countries` AS `Country` ON
> > > > > > (`Photographer`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS
> > > > > > `State` ON (`Photographer`.`state_id` = `State`.`id`) WHERE
> > > > > > `Photographer`.`id` = 1 LIMIT 1
>
> > > > > > SELECT `PrintSize`.`id`, `PrintSize`.`name`, `PrintSize`.`created`,
> > > > > > `PrintSize`.`created_by`, `PrintSize`.`modified`,
> > > > > > `PrintSize`.`modified_by`, `PhotographersPrintSize`.`id`,
> > > > > > `PhotographersPrintSize`.`photographer_id`,
> > > > > > `PhotographersPrintSize`.`print_size_id`,
> > > > > > `PhotographersPrintSize`.`price`, `PhotographersPrintSize`.`comment`,
> > > > > > `PhotographersPrintSize`.`created`,
> > > > > > `PhotographersPrintSize`.`created_by`,
> > > > > > `PhotographersPrintSize`.`modified`,
> > > > > > `PhotographersPrintSize`.`modified_by` FROM `print_sizes` AS
> > > > > > `PrintSize` JOIN `photographers_print_sizes` AS
> > > > > > `PhotographersPrintSize` ON
> > > > > > (`PhotographersPrintSize`.`photographer_id` = 1 AND
> > > > > > `PhotographersPrintSize`.`print_size_id` = `PrintSize`.`id`) WHERE 1
> > > =
> > > > > > 1
>
> > > > > > Then as soon as I add in the extra level in 'contain' I get the
> > > above,
> > > > > > plus these extras:
>
> > > > > > SELECT `Country`.`id`, `Country`.`name`, `Country`.`created`,
> > > > > > `Country`.`created_by`, `Country`.`modified`, `Country`.`modified_by`
> > > > > > FROM `countries` AS `Country` WHERE `Country`.`id` = 2
>
> > > > > > SELECT `State`.`id`, `State`.`name`, `State`.`country_id`,
> > > > > > `State`.`created`, `State`.`created_by`, `State`.`modified`,
> > > > > > `State`.`modified_by` FROM `states` AS `State` WHERE `State`.`id` = 2
>
> > > > > > I'm hoping to have some time tonight to really dig through the
> > > > > > Containable behaviour code.
>
> > > > > > On Nov 6, 3:03 pm, teknoid <teknoid.cake...@gmail.com> wrote:
>
> > > > > > > What are the "extra" queries, which get generated for Country and
> > > > > > > State, when you add other models?
> > > > > > > Could you provide an example?
>
> > > > > > > On Nov 6, 5:41 pm, Brenton B <brenton.bar...@gmail.com> wrote:
>
> > > > > > > > Photographer:
> > > > > > > > Photographer belongsTo Country
> > > > > > > > Photographer belongsTo State
> > > > > > > > Photographer HABTM PrintSize: with PhotographersPrintSize
> > > (join table
> > > > > > > > has extra data)
>
> > > > > > > > PrintSize:
> > > > > > > > PrintSize HABTM Photographer: with PhotographersPrintSize
> > > (again,
> > > > > > > > join table with data)
>
> > > > > > > > PhotographersPrintSize:
> > > > > > > > PhotographersPrintSize hasMany Photographer
> > > > > > > > PhotographersPrintSize hasMany PrintSize
>
> > > > > > > > yes, all in same db.
>
> > > > > > > > Also, forgot to mention, if I take off to recursive level (as
> > > below),
> > > > > > > > it does it fine without all the extra queries.
>
> > > > > > > > $this->set('photographer',
> > > > > > > > $this->Photographer->find('first',
>
> > > array('contain' => array(
>
> > > 'Country',
>
> > > 'State',
>
> > > 'PrintSize'))));
>
> > > > > > > > On Nov 6, 2:18 pm, teknoid <teknoid.cake...@gmail.com> wrote:
>
> > > > > > > > > what's the relation between all those models?
> > > > > > > > > and are they all in the same db?
>
> > > > > > > > > On Nov 6, 5:12 pm, Brenton B <brenton.bar...@gmail.com> wrote:
>
> > > > > > > > > > Aha!!!
> > > > > > > > > > I think I've come up with the reason ...
>
> > > > > > > > > > As I was trying to produce a nice a short example I realized
> > > that when
> > > > > > > > > > there's only 1 level it doesn't produce the extra queries ...
> > > as soon
> > > > > > > > > > as there's more than 1 level it has the extra queries.
>
> > > > > > > > > > So if I have:
>
> > > > > > > > > > $this->set('photographer',
> > > > > > > > > > $this->Photographer->find('first',
>
> > > array('contain' => array(
>
> > > 'Country',
>
> > > 'State'))));
>
> > > > > > > > > > Then there's no problem, but if I have
>
> > > > > > > > > > $this->set('photographer',
> > > > > > > > > > $this->Photographer->find('first',
>
> > > array('contain' => array(
>
> > > 'Country',
>
> > > 'State',
>
> > > 'PrintSize.PhotographersPrintSize'))));
>
> > > > > > > > > > Then it'll do extra queries on Country and State, in addition
> > > to
> > > > > > > > > > PrintSize and PhotographersPrintSize.
>
> > > > > > > > > > Even if I change it to
>
> > > > > > > > > > $this->set('photographer',
> > > > > > > > > > $this->Photographer->find('first',
>
> > > array('contain' => array(
>
> > > 'Country',
>
> > > 'State',
>
> > > 'PrintSize' => array('PhotographersPrintSize')))));
>
> > > > > > > > > > I still run into the issue.
>
> > > > > > > > > > BTW:
>
> > > > > > > > > > class AppModel extends Model {
>
> > > > > > > > > > var $actsAs = array('Containable');
>
> > > > > > > > > > }
>
> > > > > > > > > > I suspect it's something to do with recursive, but will keep
> > > digging.
>
> > > > > > > > > > On Nov 6, 8:33 am, teknoid <teknoid.cake...@gmail.com>
> > > wrote:
>
> > > > > > > > > > > Can you show your use of containable?
> > > > > > > > > > > I think something might be amiss...
>
> > > > > > > > > > > On Nov 5, 6:21 pm, Brenton B <brenton.bar...@gmail.com>
> > > wrote:
>
> > > > > > > > > > > > After a bit of digging setting the 'recursive' property
> > > in the model
> > > > > > > > > > > > _partially_ solves this:
> > > > > > > > > > > > var $actsAs = array('Containable' => array('recursive' =>
> > > false));
>
> > > > > > > > > > > > However, obviously won't help where some items need a 2nd
> > > or 3rd
> > > > > > > > > > > > level.
>
> > > > > > > > > > > > So I guess it comes back to: is there any way to be
> > > model-specific for
> > > > > > > > > > > > recursion?
>
> > > > > > > > > > > > On Nov 5, 2:54 pm, Brenton B <brenton.bar...@gmail.com>
> > > wrote:
>
> > > > > > > > > > > > > That's the problem with 'contain' though ... even if I
> > > do use a hasOne
> > > > > > > > > > > > > bind, 'contain' will still do extra queries.
> > > > > > > > > > > > > In the example I mentioned, it will retrieve everything
> > > properly in 1
> > > > > > > > > > > > > big query, but then it'll go back again and find the
> > > models listed in
> > > > > > > > > > > > > 'contain'.
> > > > > > > > > > > > > On a default page of 20 records (since I'm doing
> > > pagination), where
> > > > > > > > > > > > > each record has 4 extra models to find you're looking
> > > at 80 extra
> > > > > > > > > > > > > unnecessary db hits ... multiply that by X users view
> > > the page and
> > > > > > > > > > > > > you're looking at a lot of unnecessary queries. Sure
> > > it's just
> > > > > > > > > > > > > milliseconds, but still quite concerning.
>
> > > > > > > > > > > > > On Nov 5, 2:48 pm, teknoid <teknoid.cake...@gmail.com>
> > > wrote:
>
> > > > > > > > > > > > > > JOINS are only built for hasOne and belongsTo
> > > relationships.
> > > > > > > > > > > > > > A bunch of little selects will not harm your DB (how
> > > many
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: