Anybody knows why select count(*) is always executed everytime CGridView is used?
I have 2 tables…
table 1: order_details
columns: order_id, product, subtotal
table 2: order
columns: id, order_type, total, user_id
code on order_details model:
public function relations()
{
return array(
'orders' => array(self::BELONGS_TO,'orders','order_id'),
);
}
code on orders model:
public function relations()
{
return array(
'order_details' => array(self::HAS_ONE,'order_details','order_id'),
);
}
i get the correct sql statement:
SELECT `od`.`order_id` AS `t0_c1`, `od`.`sld` AS `t0_c2`,
`od`.`subtotal` AS `t0_c5`, `od`.`id` AS `t0_c0`, `o`.`order_type` AS
`t1_c1`, `o`.`total` AS `t1_c11`, `o`.`user_id` AS `t1_c3`, `o`.`time` AS
`t1_c10`, `o`.`status` AS `t1_c4`, `o`.`rcn_country` AS `t1_c20`, `o`.`id`
AS `t1_c0` FROM `order_details` `od` INNER JOIN `orders` `o` ON
(`od`.`order_id`=`o`.`id`) LIMIT 20
but before that, i also see this sql query, which is very slow to execute:
SELECT COUNT(DISTINCT `od`.`id`) FROM `order_details` `od`
INNER JOIN `orders` `o` ON (`od`.`order_id`=`o`.`id`)
What is the use of the select count() query? Or any ideas to optimize?