首页 > 解决方案 > 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);

                        }

                    });

标签: couchbasevert.xn1ql

解决方案


推荐阅读