couchbase - couchbase Java JDK n1ql 需要很长时间才能获得结果,但 N1QL 从 API 或 Query bench 运行得非常快
问题描述
我有一些 N1QL sql 语句和设置正确的索引。N1QL 语句平均需要大约 10 -15 毫秒,但我有一个 Java SDK,它调用相同的 SQL,几乎需要一两秒才能执行。下面是我如何尝试的完整代码。我正在使用 Vertx api。我做了一些跟踪哪些语句需要更多时间。大部分时间(大约 90 % - 95 % )由
bucket.async().query(N1qlQuery.simple(selectStatement)) 下面
我无法弄清楚我做错了什么。
private CouchbaseCluster cluster;
private volatile Bucket bucket;
JsonObject config = context.config();
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.connectTimeout(30000) //10000ms = 10s, default is 5s
.build() ;
JsonArray seedNodeArray = config.getJsonArray("couchbase.seedNodes", config.getJsonArray("couchbase.servers"));
// convert to a List
List seedNodes = new ArrayList<>(seedNodeArray.size());
for (Object seedNode : seedNodeArray) {
seedNodes.add(seedNode);
}
// use that to bootstrap the Cluster
cluster = CouchbaseCluster.create(env ,seedNodes);
JsonObject config = vertx.getOrCreateContext().config();
try {
bucket = cluster
.authenticate(config.getString("couchbase.username"),
new String(Base64.getDecoder().decode(config.getString("couchbase.password"))))
.openBucket(config.getString("couchbase.bucket"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
startFuture.fail(e.getMessage());
}
bucket.async().query(N1qlQuery.simple(selectStatement))
.retryWhen(RetryBuilder.anyOf(BackpressureException.class)
.delay(Delay.exponential(TimeUnit.MILLISECONDS, 5000)).max(5).build())
.flatMap(AsyncN1qlQueryResult::rows).map(row -> {
return row.value().toString();
}).timeout(10, TimeUnit.SECONDS).subscribe(new Subscriber<String>() {
JsonArray jarray = new JsonArray();
JsonObject jobj = new JsonObject();
public void onCompleted() {
// Extra logger for debug
LOGGER.info("Sending final response ");
if (jarray.size() > 0) {
Response = QueryResponse.sendResponse(rc, 200, "SUCCESS", "Results Found", jarray);
Response.setHandler(resu -> {
if (resu.succeeded()) {
Response.complete();
} else {
Response.fail(resu.cause());
}
});
} else {
Response = QueryResponse.sendResponse(rc, 404, "FAILURE",
"No results found for SQL Statement :", selectStatement.toString());
Response.setHandler(resu -> {
if (resu.succeeded()) {
Response.complete();
} else {
Response.fail(resu.cause());
}
});
}
}
@Override
public void onError(Throwable exp) {
LOGGER.info("Error retriving the query docs :" + exp);
Response = QueryResponse.sendResponse(rc, 500, "FAILURE",
"Some thing went wrong exequting the request: ",
new JsonArray().add(new JsonObject().put("request", selectStatement.toString()))
.add(new JsonObject().put("response", exp)));
Response.setHandler(resu -> {
if (resu.succeeded()) {
Response.complete();
} else {
Response.fail(resu.cause());
}
});
}
@Override
public void onNext(String NextJson) {
// Add the Json objects from couchbase into the Json Array
LOGGER.info("received a doc: ");
JsonObject jobj = new JsonObject(NextJson);
jarray.add(jobj);
}
});
解决方案
推荐阅读
- jquery - 在选择另一个 select2 下拉列表时重置一个 select2 下拉列表的元素
- python - Python unicode 索引显示不同的字符
- node.js - 如何在 MongoDB 中查找文档而不是通过它的 _id 和我们自己的索引?
- forms - 表格中的组合框下拉列表
- java-8 - 令牌“1”上的语法错误,枚举 Java 中预期的标识符
- angular - Angular 6模块未正确导入
- quickbooks - 继续查询 Quickbooks 数据的最佳方式
- javascript - 用于循环条件迭代和重置变量的 Javascript
- excel - 只有第一个 IF(AND) 语句有效,其余的无效
- javascript - 如何避免 ReactJs 中的 CSS 冲突