Ack, not quite two queries, still N queries, but N short queries,
after two big queries.

Here are the SQL logs:

  Blog.all.each{|b| b.latest_post.something}

with the separate latest_post method:

~ (0.000791) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `posts` WHERE (`blog_id` IN ('1d1cd114-e265-11dd-
a426-001f5be7b126',
... whole page of guids snipped ...
a426-001f5be7b126', '1eb59858-e265-11dd-a426-001f5be7b126', '20a7677c-
e265-11dd-a426-001f5be7b126', '2119b674-e265-11dd-a426-001f5be7b126'))
ORDER BY `created_at` DESC LIMIT 1
 ~ (0.000799) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `posts` WHERE (`blog_id` IN ('1d1cd114-e265-11dd-
a426-001f5be7b126', ... whole page of guids snipped ...
a426-001f5be7b126', '1eb59858-e265-11dd-a426-001f5be7b126', '20a7677c-
e265-11dd-a426-001f5be7b126', '2119b674-e265-11dd-a426-001f5be7b126'))
ORDER BY `created_at` DESC LIMIT 1
 ~ (0.000779) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `req_parses` WHERE (`blog_id` IN ('1d1cd114-e265-11dd-
a426-001f5be7b126',
... whole page of guids snipped ...
a426-001f5be7b126', '1eb59858-e265-11dd-a426-001f5be7b126', '20a7677c-
e265-11dd-a426-001f5be7b126', '2119b674-e265-11dd-a426-001f5be7b126'))
ORDER BY `created_at` DESC LIMIT 1
... many more times ...

and

with the fix (has 1):

 ~ (0.001253) SELECT `id`, `created_at`, `updated_at`, `stuff` FROM
`blog` ORDER BY `created_at` DESC
 ~ (0.001086) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `posts` WHERE (`blog_id` IN ('e9f5875e-
e269-11dd-9bc6-001f5be7b126', 'ea43581c-e269-11dd-
... whole page of guids snipped ...
9bc6-001f5be7b126', 'ec340f18-e269-11dd-9bc6-001f5be7b126', 'eb9fc6e6-
e269-11dd-9bc6-001f5be7b126')) ORDER BY `created_at` DESC
 ~ (0.000418) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `posts` WHERE (`blog_id` = 'edc7f4a2-
e269-11dd-9bc6-001f5be7b126') ORDER BY `created_at` DESC LIMIT 1
 ~ (0.000279) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something`, FROM `posts` WHERE (`blog_id` = 'ede2dc54-
e269-11dd-9bc6-001f5be7b126') ORDER BY `created_at` DESC LIMIT 1
 ~ (0.000276) SELECT `id`, `created_at`, `updated_at`, `blog_id`,
`something` FROM `posts` WHERE (`blog_id` = 'ede920c8-
e269-11dd-9bc6-001f5be7b126') ORDER BY `created_at` DESC LIMIT 1
... many more of these lines ...

Certainly some sql magic could return all latest posts in a query or
two (using MAX(created_at) and GROUP_BY post_id), but this seems a
nontrivial deduction for an ORM to make.

-Gary


On Jan 14, 10:21 am, Gary <[email protected]> wrote:
> I figured out my problem for one of the cases...
>
> I had:
>
>   class Blog
>     has n, :posts
>
>     def latest_post
>       posts.first(:order=>[:created_at.desc])
>     end
>   end
>
> And when I'd do
>
>   Blog.all.each{|b| b.latest_post.something}
>
> It would generate a query for N posts for EACH blog, basically eager-
> loading the same ones every time!
>
> THE TRICK IS TO DO:
>
>   has 1, :latest_post, :class_name => Post, :order =>
> [:created_at.desc]
>
> And then it does just two queries, very nice.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to