我需要从产品表中获取所有记录,并且只匹配其他表中的记录。 我在产品表中有多达2000条记录的mysql表。没有group_concate和group by的连接查询运行得更快。 e.x以下查询需要0.6486秒
SELECT * FROM `oc_product`
LEFT JOIN `oc_product_to_category` ON `oc_product`.`product_id` = `oc_product_to_category`.`product_id`
LEFT JOIN `oc_product_description` ON `oc_product`.`product_id` = `oc_product_description`.`product_id`
LEFT JOIN `oc_category_description` ON `oc_product_to_category`.`category_id` = `oc_category_description`.`category_id`
LEFT JOIN `oc_product_image` ON `oc_product_to_category`.`product_id` = `oc_product_image`.`product_id`
LEFT JOIN `oc_seo_data` ON `oc_product`.`product_id` = `oc_seo_data`.`id`
LEFT JOIN `oc_url_alias` ON `oc_seo_data`.`url_alias_id` = `oc_url_alias`.`url_alias_id`
当我使用GROUP BY和CONCAT_GROUP时,需要18.4205秒
SELECT `oc_product_description`.`name` as `Product_Name`,`oc_product`.`price` as `PRICE`,`oc_url_alias`.`keyword` as `Product_Url`,`oc_product_image`.`image` as `Image Path`,GROUP_CONCAT(distinct `oc_category_description`.`name`) as `Category_name`,`oc_product`.`model` as `Product_Code` FROM `oc_product` LEFT JOIN `oc_product_to_category` ON `oc_product`.`product_id` = `oc_product_to_category`.`product_id`
LEFT JOIN `oc_product_description` ON `oc_product`.`product_id` = `oc_product_description`.`product_id`
LEFT JOIN `oc_category_description` ON `oc_product_to_category`.`category_id` = `oc_category_description`.`category_id`
LEFT JOIN `oc_product_image` ON `oc_product_to_category`.`product_id` = `oc_product_image`.`product_id`
LEFT JOIN `oc_seo_data` ON `oc_product`.`product_id` = `oc_seo_data`.`id`
left JOIN `oc_url_alias` on `oc_seo_data`.`url_alias_id` = `oc_url_alias` .`url_alias_id`
GROUP BY `oc_product`.`product_id`