Hi,
I was looking at my SQL debug logs and seeing a lot of redundant
unnecessary queries which I don't think should be there at all. I am
using the nightly build of 1.2 from couple of days ago but I was
seeing this with 1.2 for quite a while.
So I created a very basic scenario and this is still reproducing, just
two DB tables and two cake models:
class Category extends AppModel
{
var $name = 'Category';
}
class Product extends AppModel
{
var $name = 'Product';
var $belongsTo = array('Category');
var $recursive = 2;
}
In my products controller, I want to get all products:
$products = $this->Product->findAll();
And this is what I see in the SQL log:
1 DESC `products` 3 3 5
2 DESC `categories` 2 2 5
3 SELECT `Product`.`id`, `Product`.`name`, `Product`.`category_id`,
`Category`.`id`, `Category`.` name` FROM `products` AS `Product` LEFT
JOIN `categories` AS `Category` ON (`Product`.`category_id` =
`Category`.`id`) WHERE 1 = 1 12 12 1
4 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 1 1 1 1
5 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 1 1 1 0
6 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 1 1 1 0
7 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 1 1 1 0
8 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 0
9 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 1
10 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 0
11 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 3
12 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 1
13 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 3 1 1 1
14 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 2 1 1 1
15 SELECT `Category`.`id`, `Category`.` name` FROM `categories` AS
`Category` WHERE `Category`.`id` = 2 1 1 0
What are all the SELECT queries from categories table doing there?
They are already included in the main query with the LEFT JOIN and why
is there a separate query for each product row even if the same
category was already fetched?
This is just a sample DB, in my real scenario with many more
associations, a findAll() query generates over 1000 redundant queries
which takes some time to execute.
Can anyone shed some light on this?
Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---