Skip to content

Commit 0fb4256

Browse files
committed
优化 AbstractSQLExecutor 关于耗时统计的代码
1 parent 5b7d54a commit 0fb4256

File tree

1 file changed

+17
-51
lines changed

1 file changed

+17
-51
lines changed

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

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,9 @@
4545
public abstract class AbstractSQLExecutor implements SQLExecutor {
4646
private static final String TAG = "AbstractSQLExecutor";
4747

48-
49-
private int generatedSQLCount;
50-
private int cachedSQLCount;
51-
private int executedSQLCount;
52-
public AbstractSQLExecutor() {
53-
generatedSQLCount = 0;
54-
cachedSQLCount = 0;
55-
executedSQLCount = 0;
56-
}
48+
private int generatedSQLCount = 0;
49+
private int cachedSQLCount = 0;
50+
private int executedSQLCount = 0;
5751

5852
@Override
5953
public int getGeneratedSQLCount() {
@@ -68,26 +62,11 @@ public int getExecutedSQLCount() {
6862
return executedSQLCount;
6963
}
7064

71-
// 只要不是并发执行且执行完立刻获取,就不会是错的,否则需要一并返回,可以 JSONObject.put("@EXECUTED_SQL_TIME:START|DURATION|END", )
72-
private long executedSQLStartTime;
73-
private long executedSQLEndTime;
74-
private long executedSQLDuration;
75-
private long sqlResultDuration;
76-
77-
public long getExecutedSQLStartTime() {
78-
return executedSQLStartTime;
79-
}
80-
public long getExecutedSQLEndTime() {
81-
return executedSQLEndTime;
82-
}
65+
private long executedSQLDuration = 0;
66+
private long sqlResultDuration = 0;
8367
@Override
8468
public long getExecutedSQLDuration() {
85-
if (executedSQLDuration <= 0) {
86-
long startTime = getExecutedSQLStartTime();
87-
long endTime = getExecutedSQLEndTime();
88-
executedSQLDuration = startTime <= 0 || endTime <= 0 ? 0 : endTime - startTime; // FIXME 有时莫名其妙地算出来是负数
89-
}
90-
return executedSQLDuration < 0 ? 0 : executedSQLDuration;
69+
return executedSQLDuration;
9170
}
9271

9372
@Override
@@ -96,15 +75,15 @@ public long getSqlResultDuration() {
9675
}
9776

9877
/**
99-
* 缓存map
78+
* 缓存 Map
10079
*/
10180
protected Map<String, List<JSONObject>> cacheMap = new HashMap<>();
10281

10382

10483
/**保存缓存
10584
* @param sql
10685
* @param list
107-
* @param isStatic
86+
* @param type
10887
*/
10988
@Override
11089
public void putCache(String sql, List<JSONObject> list, int type) {
@@ -116,7 +95,7 @@ public void putCache(String sql, List<JSONObject> list, int type) {
11695
}
11796
/**移除缓存
11897
* @param sql
119-
* @param isStatic
98+
* @param type
12099
*/
121100
@Override
122101
public void removeCache(String sql, int type) {
@@ -126,7 +105,10 @@ public void removeCache(String sql, int type) {
126105
}
127106
cacheMap.remove(sql);
128107
}
129-
108+
/**获取缓存
109+
* @param sql
110+
* @param type
111+
*/
130112
@Override
131113
public List<JSONObject> getCache(String sql, int type) {
132114
return cacheMap.get(sql);
@@ -154,24 +136,18 @@ public JSONObject getCacheItem(String sql, int position, int type) {
154136

155137
@Override
156138
public ResultSet executeQuery(@NotNull Statement statement, String sql) throws Exception {
157-
// executedSQLStartTime = System.currentTimeMillis();
158139
ResultSet rs = statement.executeQuery(sql);
159-
// executedSQLEndTime = System.currentTimeMillis();
160140
return rs;
161141
}
162142
@Override
163143
public int executeUpdate(@NotNull Statement statement, String sql) throws Exception {
164-
// executedSQLStartTime = System.currentTimeMillis();
165144
int c = statement.executeUpdate(sql);
166-
// executedSQLEndTime = System.currentTimeMillis();
167145
return c;
168146
}
169147
@Override
170148
public ResultSet execute(@NotNull Statement statement, String sql) throws Exception {
171-
// executedSQLStartTime = System.currentTimeMillis();
172149
statement.execute(sql);
173150
ResultSet rs = statement.getResultSet();
174-
// executedSQLEndTime = System.currentTimeMillis();
175151
return rs;
176152
}
177153

@@ -182,10 +158,7 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
182158
*/
183159
@Override
184160
public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws Exception {
185-
// executedSQLDuration = 0;
186-
executedSQLStartTime = System.currentTimeMillis();
187-
executedSQLEndTime = executedSQLStartTime;
188-
// sqlResultDuration = 0;
161+
long executedSQLStartTime = System.currentTimeMillis();
189162

190163
boolean isPrepared = config.isPrepared();
191164

@@ -231,8 +204,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
231204
rs = execute(statement, sql);
232205
int updateCount = statement.getUpdateCount();
233206
if (isExplain == false) {
234-
executedSQLEndTime = System.currentTimeMillis();
235-
executedSQLDuration += executedSQLEndTime - executedSQLStartTime;
207+
executedSQLDuration += System.currentTimeMillis() - executedSQLStartTime;
236208
}
237209

238210
result = new JSONObject(true);
@@ -251,8 +223,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
251223
}
252224
int updateCount = executeUpdate(config);
253225
if (isExplain == false) {
254-
executedSQLEndTime = System.currentTimeMillis();
255-
executedSQLDuration += executedSQLEndTime - executedSQLStartTime;
226+
executedSQLDuration += System.currentTimeMillis() - executedSQLStartTime;
256227
}
257228

258229
if (updateCount <= 0) {
@@ -294,8 +265,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
294265
}
295266
rs = executeQuery(config); //FIXME SQL Server 是一次返回两个结果集,包括查询结果和执行计划,需要 moreResults
296267
if (isExplain == false) {
297-
executedSQLEndTime = System.currentTimeMillis();
298-
executedSQLDuration += executedSQLEndTime - executedSQLStartTime;
268+
executedSQLDuration += System.currentTimeMillis() - executedSQLStartTime;
299269
}
300270
break;
301271

@@ -1131,9 +1101,7 @@ public void close() {
11311101
@Override
11321102
public ResultSet executeQuery(@NotNull SQLConfig config) throws Exception {
11331103
PreparedStatement stt = getStatement(config);
1134-
// 不准,getStatement 有时比 execute sql 更耗时 executedSQLStartTime = System.currentTimeMillis();
11351104
ResultSet rs = stt.executeQuery(); //PreparedStatement 不用传 SQL
1136-
// executedSQLEndTime = System.currentTimeMillis();
11371105
// if (config.isExplain() && (config.isSQLServer() || config.isOracle())) {
11381106
// FIXME 返回的是 boolean 值 rs = stt.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
11391107
// }
@@ -1144,9 +1112,7 @@ public ResultSet executeQuery(@NotNull SQLConfig config) throws Exception {
11441112
@Override
11451113
public int executeUpdate(@NotNull SQLConfig config) throws Exception {
11461114
PreparedStatement stt = getStatement(config);
1147-
// 不准,getStatement 有时比 execute sql 更耗时 executedSQLStartTime = System.currentTimeMillis();
11481115
int count = stt.executeUpdate(); // PreparedStatement 不用传 SQL
1149-
// executedSQLEndTime = System.currentTimeMillis();
11501116

11511117
if (count <= 0 && config.isHive()) {
11521118
count = 1;

0 commit comments

Comments
 (0)