# 脚本异常：脚本 [my_script] 编译失败：一分钟内动态脚本编译次数过多

- **ID:** `elasticsearch/script-compilation-too-many-dynamic`
- **领域:** elasticsearch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

集群的脚本编译速率超过了 script.max_compilations_rate 限制，通常是因为每个查询生成唯一脚本而不是使用缓存脚本。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.17.14 | active | — | — |
| 8.5.3 | active | — | — |
| 8.12.0 | active | — | — |

## 解决方案

1. ```
   使用存储脚本代替内联脚本：POST _scripts/my_script { "script": { "lang": "painless", "source": "doc['field'].value * params.factor" } } 然后在查询中引用为 "script": { "id": "my_script", "params": { "factor": 2 } }
   ```
2. ```
   临时增加编译速率限制：PUT /_cluster/settings { "transient": { "script.max_compilations_rate": "100/1m" } }
   ```

## 无效尝试

- **** — Unlimited compilations can lead to high CPU usage, memory pressure, and potential denial of service. (85% 失败率)
- **** — This only delays the problem; if scripts are not cached, the rate will still be hit eventually with high load. (70% 失败率)
