MySQL LIMIT optimization issue
I have the following MySQL query that I'm looking to make the LIMIT faster as it's running VERY slow. SQL_CALC_FOUND_ROWS equals about 114000 rows.
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
The following is the EXPLAIN results
+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+---------------开发者_开发知识库-------------------------------+
| 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 | |
+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+----------------------------------------------+
Any advice would be appreciated.
Update: Solved this issue by creating a separate table that pre-calculates the sort order making the site run about 500x to 1000x faster.
Seems like the problem is sorting. In this case you may try to create these indexes, but I cannot guarantee anything:
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
);
精彩评论