没有不值得去解决的问题,也没有不值得去学习的技术!

MySQL 8.0 Query Timeout Analysis: When an IN Clause Contains 10,000 Parameters

The IN condition contained 10,000 parameters (the product requirement sets the upper limit at 10,000), which caused the query to take too long and the API response to time out

作者:

在

, ,

1. The IN condition contained 10,000 parameters (the product requirement sets the upper limit at 10,000), which caused the query to take too long and the API response to time out. See Figure 1.

The IN condition contained 10,000 parameters (the product requirement sets the upper limit at 10,000), which caused the query to take too long and the API response to time out
JSON

{
  "filter": {
    "plat_order_id": [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      ...,
      10000
    ]
  },
  "page": 1,
  "per_page": 100
}

JSON

{
    "status_code": 500,
    "code": 0,
    "message": "Maximum execution time of 300 seconds exceeded",
    "trace": {
        "line": 420,
        "file": "E:\\wwwroot\\erp-backend\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
        "class": "Symfony\\Component\\ErrorHandler\\Error\\FatalError",
        "trace": [
            "#0 {main}"
        ]
    }
}

2. After reducing the number of plat_order_id parameters from 10,000 to 5, the API response no longer timed out. The table contains a total of 20 million records. See Figure 2.

After reducing the number of plat_order_id parameters from 10,000 to 5, the API response no longer timed out. The table contains a total of 20 million records.

3. The generated SQL is as follows:

SQL

select
  count(*) as aggregate
from
  `orders`
where
  `orders`.`plat_order_id` in ('95798');

4. I adjusted the SQL and decided to force the use of the index with USE INDEX, which stopped the timeout.

SQL

select
  count(*) as aggregate
from
  `orders` USE INDEX(`orders_plat_order_id_index`)
where
  `orders`.`plat_order_id` in ('95798')

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系