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.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `MasterVendor` ADD INDEX `mastervendor_idx_isactive` (`isActive`);
ALTER TABLE `ProductStoreDef` ADD INDEX `productstoredef_idx_skustatus_mastercate_customerpr` (`SKUStatus`,`MasterCategoryID`,`CustomerPrice`);
ALTER TABLE `ProductSummary` ADD INDEX `productsummary_idx_productid` (`ProductID`);
SELECT
SQL_CALC_FOUND_ROWS pstd_productid
FROM
(SELECT
PStD.ProductID AS pstd_productid,
PStD.MasterVendorID AS PStD_MasterVendorID
FROM
ProductStoreDef PStD
JOIN
ProductSummary PS
ON PStD.ProductID = PS.ProductID
WHERE
PStD.SKUStatus = 'A'
AND PStD.MasterCategoryID = 66
AND PStD.CustomerPrice > 0
ORDER BY
PStD.VendorName,
PS.VendorPartNumber LIMIT 50) PStD
JOIN
MasterVendor MV
ON MV.VendorID = PStD.PStD_MasterVendorID
WHERE
1 = 1
AND MV.isActive = 1
AND 1 = 1
AND 1 = 1 LIMIT 50