0

当我添加从 JOINED 表派生的 ORDER BY 时,执行查询的时间从 0.008 秒上升到 0.50 秒,尽管 order by 字段上有一个索引。以下是查询:

SELECT a.productid 
FROM   products a 
   JOIN products_lng b FORCE INDEX (primary)
     ON a.productid = b.productid 
        AND b.code = 'US' 
   JOIN pricing c 
     ON c.productid = a.productid 
   JOIN thumbnails d 
     ON d.productid = a.productid 
   JOIN bigthumbnails e 
     ON e.productid = a.productid 
   JOIN products_categories f 
     ON f.productid = a.productid 
WHERE  a.forsale = 'Y' 
GROUP  BY b.productid 
ORDER  BY b.product 

解释计划: 在此处输入图像描述

b 具有以下索引:

在此处输入图像描述

CREATE TABLE `products_lng` (
 `code` varchar(2) CHARACTER SET latin1 NOT NULL DEFAULT '',
 `productid` int(11) NOT NULL DEFAULT '0',
 `product` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '',
 `descr` varchar(512) CHARACTER SET latin1 NOT NULL,
 `full_descr` varchar(1024) CHARACTER SET latin1 NOT NULL,
 `processed` varchar(1) CHARACTER SET latin1 NOT NULL,
 PRIMARY KEY (`code`,`productid`,`product`),
 KEY `ad` (`code`,`productid`,`product`,`descr`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
4

1 回答 1

0

最后,ORDER BY 和 GROUP BY 的组合似乎是查询执行时间比正常情况更长的原因。然而,通过将查询放在子查询中并因此将 ORDER BY 和 GROUP BY 分开,我设法将执行时间大幅限制为 0.0015 秒,而不是原来的 0.5 秒。我仍然必须根据我的要求调整查询,但以下是它的外观。基本上它归结为在子查询中定义 WHERE 参数和周围查询您希望选择的值。

    SELECT z.*,d.*,e.*
FROM products_lng z
JOIN thumbnails d ON d.productid = z.productid
JOIN bigthumbnails e ON e.productid = z.productid
WHERE EXISTS
    ( SELECT b.productid
     FROM products b
     JOIN pricing c ON c.productid = b.productid
     JOIN products_lng i USE INDEX (PRIMARY) ON i.productid = b.productid
     AND i.code = 'US'
     JOIN products_categories f ON f.productid = b.productid
     WHERE b.productid = z.productid
     GROUP BY b.productid )
ORDER BY z.product
于 2012-12-21T04:12:57.630 回答