0

When I add an ORDER BY derived from a JOINED table, the time to execute the query goes up from 0.008 seconds to 0.50 seconds, this although there's an index on the order by field. The following is the query:

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 

Explain plan: enter image description here

b has the following indexes:

enter image description here

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

In the end it appeared to be that the combination of ORDER BY and GROUP BY was the reason that the executement of the query took longer than normal. By placing the query however in a sub query and therefore separating the ORDER BY and GROUP BY I managed to drastically limit the execution time to 0.0015 sec instead of the original 0.5 sec. I still have to adjust the query to my requirements but the following is how it should look like. Basically it comes down to that in the subquery you define the WHERE parameters and the surrounding query which values you wish to select.

    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 に答える