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.

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.

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