MySQL 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
以下是解释结果
+----+-------------+-------+--------+-------------------------------------------------------------------------------------------------+-----------------+---------+-------------------------+------+----------------------------------------------+
| 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 倍,从而解决了此问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来问题出在排序上。在这种情况下,您可以尝试创建这些索引,但我不能保证任何事情:
Seems like the problem is sorting. In this case you may try to create these indexes, but I cannot guarantee anything: