Skip to content

Commit bd5151b

Browse files
committed
结果集容量预估:最低开启阈值默认设置为 2^16 - 1 = 65535,ArrayList 最多扩容 5 次
1 parent 7a53f3b commit bd5151b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
158158
return rs;
159159
}
160160

161+
public static int MIN_OPTIMIZE_CAPACITY = 2^16 - 1;
162+
161163
/**执行SQL
162164
* @param config
163165
* @return
@@ -330,7 +332,7 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
330332
}
331333
else { // 预估容量
332334
capacity = config.getCount() <= 0 ? AbstractParser.MAX_QUERY_COUNT : config.getCount();
333-
if (capacity > 100) {
335+
if (capacity > MIN_OPTIMIZE_CAPACITY) {
334336
// 有 WHERE 条件,条件越多过滤数据越多,暂时不考虑 @combine:"a | (b & !c)" 里面 | OR 和 ! NOT 条件,太复杂也不是很必要
335337
Map<String, List<String>> combine = config.getCombineMap();
336338

@@ -354,14 +356,15 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
354356
Map<String, Object> having = config.getHaving();
355357
int havingCount = having == null ? 0 : having.size();
356358

357-
capacity /= Math.pow(1.5, Math.log10(capacity)
359+
double cap = capacity / Math.pow(1.5, Math.log10(capacity)
358360
+ andCondCount
359361
+ ((orCondCount <= 0 ? 0 : 2.0d/orCondCount) // 1: 2.3, 2: 1.5, 3: 1.3, 4: 1.23, 5: 1.18
360362
+ (notCondCount/5.0d) // 1: 1.08, 2: 1.18, 3: 1.28, 4: 1.38, 1.50
361363
+ (groupCount <= 0 ? 0 : 10.0d/groupCount)) // 1: 57.7, 7.6, 3: 3.9, 4: 2.8, 5: 2.3
362364
+ havingCount
363365
);
364-
capacity += 1; // 避免正好比需要容量少一点点导致多一次扩容,大量数据 System.arrayCopy
366+
cap = Math.max(MIN_OPTIMIZE_CAPACITY, Math.max(cap, capacity/Math.pow(1.5, 5))); // 1/(1.5^5) = 0.13
367+
capacity = (int) (cap + 1); // 避免正好比需要容量少一点点导致多一次扩容,大量数据 System.arrayCopy
365368
}
366369
}
367370
}

0 commit comments

Comments
 (0)