MySQL LIMIT 优化问题

发布于 2024-11-27 00:07:56 字数 2335 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2024-12-04 00:07:56

看来问题出在排序上。在这种情况下,您可以尝试创建这些索引,但我不能保证任何事情:

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
);

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
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文