Re: Efficient ways to retrieve data from models with high association depth

2009-07-31 Thread AD7six



On Jul 31, 12:15 am, Roel roel@gmail.com wrote:
 Ah I like your idea of encapsulating the two finds in a model method.
 That seems much better than putting the logic in the controller.
 Thanks.

 Could you please elaborate on the Linkable (and OneQuery) behavior?
 Anything with regard to optimizing database performance for related
 models seems relevant to me.

Given that A belongsTo|hasOne B belongsTo|hasOne C belongsTo|hasOne D
belongsTo|hasOne E etc.

you'll get n queries with cake, and 1 query using Linkable/OneQuery.
They're used in a similar fashion to containable.

Cheers,

AD



 -Roel

 On 30 jul, 21:40, AD7six andydawso...@gmail.com wrote:

  On Jul 30, 9:00 pm, Roel roel@gmail.com wrote:

   I've tried Containable but it doesn't help lower the number of queries
   made. In fact in some circumstances Containable creates unnecessary
   queries (https://trac.cakephp.org/ticket/5864).

   To be clear, I have no trouble getting my data. It's just, from a
   perspective of someone who handwritten his MySQL queries, CakePHP
   could be much more efficient with its database calls.

   At points I'm a bit torn between writing an app that prioritizes
   efficient database queries or Cake's logic. To illustrate:

   Option 1:

   $data = $this-Article-read();

   queries made (aside from DESCRIBE): 1 + [num_comments]

   Option 2:

   $this-Article-recursive = -1;

   $articleData = $this-Article-read();

   $postData = $this-Article-Comment-find('all', array('conditions' =
   array('Comment.article_id' = $this-Article-id)));

   queries made (aside from DESCRIBE): 2)

   Option 1 is nice and clean, as you would expect from Cake, but it can
   potentially mean a lot of database quering. Option 2 on the other hand
   doesn't look as slick. It puts some logic in my controller I don't
   want there. The related data is split over 2 variables. But the number
   of queries is fixed.

   I'm just curious if this issue also bothers other developers and what
   direction do they choose.

  I'd go with the latter - 2 explicit and simple queries (although I
  think your 1 + number of comments is dependent on the version of cake
  you're testing with - should be 2 queries anyway).
  You could create a model method to encapsulate your two finds (and any
  other logic therin). and even re arrange the resultant array data such
  that it's the same as calling find with containable.

  The Linkable (or my own OneQuery) behavior is worth using - although
  in the use case you're presented it's not relevant.

  AD
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-31 Thread AD7six



On Jul 31, 9:58 am, AD7six andydawso...@gmail.com wrote:
 On Jul 31, 12:15 am, Roel roel@gmail.com wrote:

  Ah I like your idea of encapsulating the two finds in a model method.
  That seems much better than putting the logic in the controller.
  Thanks.

  Could you please elaborate on the Linkable (and OneQuery) behavior?
  Anything with regard to optimizing database performance for related
  models seems relevant to me.

 Given that A belongsTo|hasOne B belongsTo|hasOne C belongsTo|hasOne D
 belongsTo|hasOne E etc.

 you'll get n queries with cake, and 1 query using Linkable/OneQuery.
 They're used in a similar fashion to containable.

 Cheers,

 AD

The article by Mark descibes how you can do it by hand
http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations

Cheers,

AD
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-31 Thread Thiago Elias
Hi guys.

Sometimes, to made the joins using only one query, I've used the joins
inside find method, but I saw Mark's article using bindModel and
unbindModel. Now my question is: What's the difference using bind or
unbindModel, instead the joins array inside the find method ?!



Regards.
Thiago Elias.

2009/7/31 AD7six andydawso...@gmail.com




 On Jul 31, 9:58 am, AD7six andydawso...@gmail.com wrote:
  On Jul 31, 12:15 am, Roel roel@gmail.com wrote:
 
   Ah I like your idea of encapsulating the two finds in a model method.
   That seems much better than putting the logic in the controller.
   Thanks.
 
   Could you please elaborate on the Linkable (and OneQuery) behavior?
   Anything with regard to optimizing database performance for related
   models seems relevant to me.
 
  Given that A belongsTo|hasOne B belongsTo|hasOne C belongsTo|hasOne D
  belongsTo|hasOne E etc.
 
  you'll get n queries with cake, and 1 query using Linkable/OneQuery.
  They're used in a similar fashion to containable.
 
  Cheers,
 
  AD

 The article by Mark descibes how you can do it by hand
 http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations

 Cheers,

 AD
 


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-30 Thread JamesF

have you tried using Containable behavior?

On Jul 30, 12:16 pm, Roel roel@gmail.com wrote:
 Hi,

 I'm using CakePHP for quite some time now and I really love it, but I
 hadn't come around to make use of the complex association
 possibilities for models. So I started to play around with them for a
 personal project I just started. Although the advantages Cake offers
 are numerous, I stumbled across an issue I'm not happy about. It's not
 a bug but an effciency issue.

 Let's say we have three models: Article, Comment, and User. The
 relationships are as follows: Article hasMany Comment, Comment hasOne
 User. Now when the data for Article is retrieved, a nice JOIN query is
 used to fetch the Comment data together with the Article data, but a
 seperate query is made for each User instead of using a nice JOIN
 statement.

 Now I know this is a known issue (https://trac.cakephp.org/ticket/
 2931) and I understand why the resolution is set to 'wont fix', but I
 wondered how other people worked around this or maybe didn't even
 bother. Truth is, in this specific case of my personal project it
 doesn't even matter that much. It doesn't need to serve thousands
 users. But it bothers me that Cake, which first enabled me to
 elegantly develop applications fast, now kinda shows it's dirty side.

 So what are your thoughts about retrieving data efficiently with Cake?
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-30 Thread Roel

I've tried Containable but it doesn't help lower the number of queries
made. In fact in some circumstances Containable creates unnecessary
queries (https://trac.cakephp.org/ticket/5864).

To be clear, I have no trouble getting my data. It's just, from a
perspective of someone who handwritten his MySQL queries, CakePHP
could be much more efficient with its database calls.

At points I'm a bit torn between writing an app that prioritizes
efficient database queries or Cake's logic. To illustrate:

Option 1:

$data = $this-Article-read();

queries made (aside from DESCRIBE): 1 + [num_comments]


Option 2:

$this-Article-recursive = -1;

$articleData = $this-Article-read();

$postData = $this-Article-Comment-find('all', array('conditions' =
array('Comment.article_id' = $this-Article-id)));

queries made (aside from DESCRIBE): 2)

Option 1 is nice and clean, as you would expect from Cake, but it can
potentially mean a lot of database quering. Option 2 on the other hand
doesn't look as slick. It puts some logic in my controller I don't
want there. The related data is split over 2 variables. But the number
of queries is fixed.

I'm just curious if this issue also bothers other developers and what
direction do they choose.

-Roel

On 30 jul, 19:14, JamesF usaexportexpe...@gmail.com wrote:
 have you tried using Containable behavior?

 On Jul 30, 12:16 pm, Roel roel@gmail.com wrote:



  Hi,

  I'm using CakePHP for quite some time now and I really love it, but I
  hadn't come around to make use of the complex association
  possibilities for models. So I started to play around with them for a
  personal project I just started. Although the advantages Cake offers
  are numerous, I stumbled across an issue I'm not happy about. It's not
  a bug but an effciency issue.

  Let's say we have three models: Article, Comment, and User. The
  relationships are as follows: Article hasMany Comment, Comment hasOne
  User. Now when the data for Article is retrieved, a nice JOIN query is
  used to fetch the Comment data together with the Article data, but a
  seperate query is made for each User instead of using a nice JOIN
  statement.

  Now I know this is a known issue (https://trac.cakephp.org/ticket/
  2931) and I understand why the resolution is set to 'wont fix', but I
  wondered how other people worked around this or maybe didn't even
  bother. Truth is, in this specific case of my personal project it
  doesn't even matter that much. It doesn't need to serve thousands
  users. But it bothers me that Cake, which first enabled me to
  elegantly develop applications fast, now kinda shows it's dirty side.

  So what are your thoughts about retrieving data efficiently with Cake?
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-30 Thread AD7six



On Jul 30, 9:00 pm, Roel roel@gmail.com wrote:
 I've tried Containable but it doesn't help lower the number of queries
 made. In fact in some circumstances Containable creates unnecessary
 queries (https://trac.cakephp.org/ticket/5864).

 To be clear, I have no trouble getting my data. It's just, from a
 perspective of someone who handwritten his MySQL queries, CakePHP
 could be much more efficient with its database calls.

 At points I'm a bit torn between writing an app that prioritizes
 efficient database queries or Cake's logic. To illustrate:

 Option 1:

 $data = $this-Article-read();

 queries made (aside from DESCRIBE): 1 + [num_comments]

 Option 2:

 $this-Article-recursive = -1;

 $articleData = $this-Article-read();

 $postData = $this-Article-Comment-find('all', array('conditions' =
 array('Comment.article_id' = $this-Article-id)));

 queries made (aside from DESCRIBE): 2)

 Option 1 is nice and clean, as you would expect from Cake, but it can
 potentially mean a lot of database quering. Option 2 on the other hand
 doesn't look as slick. It puts some logic in my controller I don't
 want there. The related data is split over 2 variables. But the number
 of queries is fixed.

 I'm just curious if this issue also bothers other developers and what
 direction do they choose.

I'd go with the latter - 2 explicit and simple queries (although I
think your 1 + number of comments is dependent on the version of cake
you're testing with - should be 2 queries anyway).
You could create a model method to encapsulate your two finds (and any
other logic therin). and even re arrange the resultant array data such
that it's the same as calling find with containable.

The Linkable (or my own OneQuery) behavior is worth using - although
in the use case you're presented it's not relevant.

AD
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Efficient ways to retrieve data from models with high association depth

2009-07-30 Thread Roel

Ah I like your idea of encapsulating the two finds in a model method.
That seems much better than putting the logic in the controller.
Thanks.

Could you please elaborate on the Linkable (and OneQuery) behavior?
Anything with regard to optimizing database performance for related
models seems relevant to me.

-Roel

On 30 jul, 21:40, AD7six andydawso...@gmail.com wrote:
 On Jul 30, 9:00 pm, Roel roel@gmail.com wrote:





  I've tried Containable but it doesn't help lower the number of queries
  made. In fact in some circumstances Containable creates unnecessary
  queries (https://trac.cakephp.org/ticket/5864).

  To be clear, I have no trouble getting my data. It's just, from a
  perspective of someone who handwritten his MySQL queries, CakePHP
  could be much more efficient with its database calls.

  At points I'm a bit torn between writing an app that prioritizes
  efficient database queries or Cake's logic. To illustrate:

  Option 1:

  $data = $this-Article-read();

  queries made (aside from DESCRIBE): 1 + [num_comments]

  Option 2:

  $this-Article-recursive = -1;

  $articleData = $this-Article-read();

  $postData = $this-Article-Comment-find('all', array('conditions' =
  array('Comment.article_id' = $this-Article-id)));

  queries made (aside from DESCRIBE): 2)

  Option 1 is nice and clean, as you would expect from Cake, but it can
  potentially mean a lot of database quering. Option 2 on the other hand
  doesn't look as slick. It puts some logic in my controller I don't
  want there. The related data is split over 2 variables. But the number
  of queries is fixed.

  I'm just curious if this issue also bothers other developers and what
  direction do they choose.

 I'd go with the latter - 2 explicit and simple queries (although I
 think your 1 + number of comments is dependent on the version of cake
 you're testing with - should be 2 queries anyway).
 You could create a model method to encapsulate your two finds (and any
 other logic therin). and even re arrange the resultant array data such
 that it's the same as calling find with containable.

 The Linkable (or my own OneQuery) behavior is worth using - although
 in the use case you're presented it's not relevant.

 AD
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---