Mysql查询优化,将CASE Exists(SELECT ...)移动到左连接中

时间:2011-03-30 18:10:34

标签: mysql query-optimization

更新 这是CASE语句使用的两个表的ddl

CREATE TABLE product_option (
merchant_id                 smallint unsigned NOT NULL,
product_option_id           smallint unsigned NOT NULL AUTO_INCREMENT,
product_option_name         varchar(255) NOT NULL DEFAULT '',
product_id                  int unsigned NOT NULL DEFAULT 0,    /* Option may be associated with a product  */
package_id                  int unsigned NOT NULL DEFAULT 0,    /* or all products with this package    */
PRIMARY KEY pk_product_option (merchant_id,product_option_id),

) TYPE=MyISAM PACK_KEYS=1 COMMENT='Options';

CREATE TABLE package_2_product (
merchant_id     smallint unsigned NOT NULL,
package_id      int unsigned NOT NULL,
product_id      int unsigned NOT NULL,

PRIMARY KEY pk_package_2_product (merchant_id,package_id,product_id)

) TYPE=MyISAM PACK_KEYS=1 COMMENT='Link product to package';

我有一个查询...

SELECT SQL_CALC_FOUND_ROWS 
  p.*,
  CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg, 
  CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
  CASE WHEN EXISTS (
    SELECT product_option_id 
      FROM product_option
     WHERE merchant_id = 116 AND product_id = p.product_id
     UNION
    SELECT product_id FROM package_2_product
     WHERE merchant_id = 116 AND product_id = p.product_id
  ) THEN 1 ELSE 0 END AS options_exist,
  i.thumbnail,i.thumbnail_width,i.thumbnail_height,
  i.title AS thumbnail_title, i.alt AS thumbnail_alt
FROM 
  product p 
  INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                       AND p.product_id=c2p.product_id 
                                       AND c2p.category_id = 84
  LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                       AND p.product_id = i.product_id
                                       AND i.is_default = 1 
WHERE 
  p.merchant_id = 116
  AND FIND_IN_SET('live',p.param) > 0
  AND FIND_IN_SET('wholesale-only',p.param) = 0
ORDER BY
  p.rank, p.product_name
LIMIT 0, 50;

相关部分是案例,当时存在...... 导致查询需要多秒才能运行。如果没有子选择,它将在十分之一秒内完成。

我想知道是否有办法移动这种类型的情况,子选择内部连接以获取相同的数据,而不是为返回的每一行运行子选择。

通过调整Coalesce线和LEFT JOIN(SELECT ...)来尝试下面的查询p1

我收到了这个错误......

错误代码:1054 'on clause'

中的未知列'p1.product_id'

这是查询...

SELECT SQL_CALC_FOUND_ROWS 
  p.*,
  CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg,  
  CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
  COALESCE(p1.product_option_id, p2.product_id) IS NOT NULL AS options_exist,
  i.thumbnail,i.thumbnail_width,i.thumbnail_height,
  i.title AS thumbnail_title, i.alt AS thumbnail_alt
FROM 
  product p 
  INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                       AND p.product_id=c2p.product_id 
                                       AND c2p.category_id = 84
  LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                       AND p.product_id = i.product_id
                                       AND i.is_default = 1 
  LEFT JOIN (
   SELECT product_option_id
     FROM product_option
    WHERE merchant_id = 116
    GROUP BY product_id) p1 on p1.product_id = p.product_id
   LEFT JOIN (
    SELECT product_id
    FROM package_2_product
   WHERE merchant_id = 116
    GROUP BY product_id) p2 on p1.product_id is null and p2.product_id = p.product_id 
WHERE 
  p.merchant_id = 116
  AND FIND_IN_SET('live',p.param) > 0
  AND FIND_IN_SET('wholesale-only',p.param) = 0
ORDER BY
  p.rank, p.product_name

2 个答案:

答案 0 :(得分:2)

SELECT SQL_CALC_FOUND_ROWS 
  p.*,
  CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg, 
  CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
  COALESCE(p1.product_id, p2.product_id) IS NOT NULL AS options_exist,
  i.thumbnail,i.thumbnail_width,i.thumbnail_height,
  i.title AS thumbnail_title, i.alt AS thumbnail_alt
FROM 
  product p 
  INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                       AND p.product_id=c2p.product_id 
                                       AND c2p.category_id = 84
  LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                       AND p.product_id = i.product_id
                                       AND i.is_default = 1 
  LEFT JOIN (
    SELECT product_id
      FROM product_option
     WHERE merchant_id = 116
     GROUP BY product_id) p1 on p1.product_id = p.product_id
  LEFT JOIN (
    SELECT product_id
      FROM package_2_product
     WHERE merchant_id = 116
     GROUP BY product_id) p2 on p1.product_id is null and p2.product_id = p.product_id

WHERE 
  p.merchant_id = 116
  AND FIND_IN_SET('live',p.param) > 0
  AND FIND_IN_SET('wholesale-only',p.param) = 0
ORDER BY
  p.rank, p.product_name

注意:

  • on p1.product_id is null被添加到第二个左连接中,以防止在p1已经有结果时执行它。
  • group by子句阻止LEFT JOIN通过笛卡尔积扩展结果集

<小时/> 此DDL将创建一个包含足够字段的表,以显示查询正常运行。

create table product_option(merchant_id int, product_id int);
create table package_2_product(merchant_id int, product_id int);
create table category_2_product(merchant_id int, product_id int, category_id int);
create table product_image(merchant_id int, product_id int, is_default int,
    thumbnail int, thumbnail_width int, thumbnail_height int, title int, alt int);
create table product(merchant_id int, product_id int, in_stock_msg int,
    out_stock_msg int, param int, rank int, product_name int);

答案 1 :(得分:0)

试试这个:

SELECT SQL_CALC_FOUND_ROWS p.*, 
                           CASE p.in_stock_msg 
                             WHEN '' THEN 'In stock' 
                             ELSE p.in_stock_msg 
                           END     AS in_stock_msg, 
                           CASE p.out_stock_msg 
                             WHEN '' THEN '' 
                             ELSE p.out_stock_msg 
                           END     AS out_stock_msg, 
                           CASE 
                             WHEN Isnull(po.product_option_id) 
                                  AND Isnull(p2p.product_id) THEN 0 
                             ELSE 1 
                           END     AS options_exist, 
                           i.thumbnail, 
                           i.thumbnail_width, 
                           i.thumbnail_height, 
                           i.title AS thumbnail_title, 
                           i.alt   AS thumbnail_alt 
FROM   product p 
       INNER JOIN category_2_product c2p 
         ON p.merchant_id = c2p.merchant_id 
            AND p.product_id = c2p.product_id 
            AND c2p.category_id = 84 
       LEFT JOIN product_image i 
         ON p.merchant_id = i.merchant_id 
            AND p.product_id = i.product_id 
            AND i.is_default = 1 
       LEFT JOIN product_option po 
         ON po.merchant_id = 116 
            AND po.product_id = p.product_id 
       LEFT JOIN package_2_product p2p 
         ON p2p.merchant_id = 116 
            AND p2p.product_id = p.product_id 
WHERE  p.merchant_id = 116 
       AND Find_in_set('live', p.param) > 0 
       AND Find_in_set('wholesale-only', p.param) = 0 
GROUP  BY p.prduct_id 
ORDER  BY p.rank, 
          p.product_name 
LIMIT  0, 50; 

你需要两个表的索引

  • (MERCHANT_ID,PRODUCT_ID)