MySQL LIMIT优化问题

时间:2011-07-28 19:24:12

标签: mysql database optimization limit

我有以下MySQL查询,我正在寻求使LIMIT更快,因为它运行非常慢。 SQL_CALC_FOUND_ROWS等于大约114000行。

SELECT SQL_CALC_FOUND_ROWS PStD.ProductID FROM ProductStoreDef PStD
    JOIN ProductSummary PS ON PStD.ProductID = PS.ProductID 
    JOIN MasterVendor MV ON MV.VendorID = PStD.MasterVendorID 
WHERE 
    PStD.SKUStatus = 'A' AND 
    MV.isActive = 1 AND 
    PStD.MasterCategoryID = 66 AND 
    PStD.CustomerPrice > 0
ORDER BY PStD.VendorName, PS.VendorPartNumber 
LIMIT 100000,50

以下是EXPLAIN结果

+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                                                                                   | key             | key_len | ref                     | rows | Extra                                        |
+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | MV    | ALL    | PRIMARY,isActive,VendorID                                                                       | NULL            | NULL    | NULL                    | 2126 | Using where; Using temporary; Using filesort | 
|  1 | SIMPLE      | PStD  | ref    | PRIMARY,MasterVendorID,MasterCategoryID,SKUStatus,CustomerPrice,MasterVendCatID,ProdStoreStatus | MasterVendCatID | 8       | ecomm.MV.VendorID,const |   94 | Using where                                  | 
|  1 | SIMPLE      | PS    | eq_ref | PRIMARY                                                                                         | PRIMARY         | 4       | ecomm.PStD.ProductID    |    1 |                                              | 
+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+----------------------------------------------+

任何建议都将受到赞赏。

更新:通过创建一个单独的表来解决此问题,该表预先计算排序顺序,使网站运行速度提高约500倍至1000倍。

1 个答案:

答案 0 :(得分:1)

似乎问题是排序。在这种情况下,您可能会尝试创建这些索引,但我无法保证:

ALTER TABLE `ProductStoreDef` ADD INDEX `ProductStoreDef_CIndex` (
    `ProductID` ASC, `MasterVendorID` ASC, `MasterCategoryID` ASC, 
    `SKUStatus` ASC, `CustomerPrice` ASC, `VendorName` ASC
);    
ALTER TABLE `ProductSummary` ADD INDEX `ProductSummary_CIndex` (
    `ProductID` ASC, `VendorPartNumber` ASC
);    
ALTER TABLE `MasterVendor` ADD INDEX `MasterVendor_CIndex` (
    `VendorID` ASC, `isActive` ASC
);