feat(exports): 增强行数估算功能以支持精确统计和日志记录

- 在create函数中添加行数估算逻辑,优先使用分块统计,失败时回退到精确COUNT
- 添加日志记录以跟踪估算过程,确保在估算为0时能够进行精确统计
- 提升代码可读性,确保行数估算和日志记录逻辑清晰明了
This commit is contained in:
zhouyonggao 2025-12-17 19:58:28 +08:00
parent 646c9e8bc4
commit 83339f42a3
1 changed files with 10 additions and 0 deletions

View File

@ -305,8 +305,18 @@ func (a *ExportsAPI) create(w http.ResponseWriter, r *http.Request) {
fail(w, r, http.StatusBadRequest, fmt.Sprintf("EXPLAIN 未通过:评分=%d请优化索引或缩小查询范围", score)) fail(w, r, http.StatusBadRequest, fmt.Sprintf("EXPLAIN 未通过:评分=%d请优化索引或缩小查询范围", score))
return return
} }
// 估算行数(优先使用分块统计,失败或结果为 0 时回退到单次 COUNT
var estimate int64 var estimate int64
estimate = rrepo.EstimateFastChunked(dataDB, ds, main, p.Filters) estimate = rrepo.EstimateFastChunked(dataDB, ds, main, p.Filters)
if estimate <= 0 {
logging.JSON("WARN", map[string]interface{}{
"event": "estimate_zero_fallback",
"datasource": ds,
"main_table": main,
"filters": p.Filters,
})
estimate = rrepo.EstimateFast(dataDB, ds, main, p.Filters)
}
hdrs := make([]string, len(filtered)) hdrs := make([]string, len(filtered))
for i, tf := range filtered { for i, tf := range filtered {
if v, ok := labels[tf]; ok { if v, ok := labels[tf]; ok {