I am facing issues while fetching data from couchbase. The SQL is running within milliseconds but the data retrieval is taking time. The size of the data is 30446 KB.
So ideally it is taking 11.81 seconds to get the data which is huge for the size of the data that is being retrieved. Is there any settings/port that needs to be configured so that it takes less time in transfer of data.
@Repository
public interface ProductRepo extends ReactiveCouchbaseRepository<Product, String>{
}
@Autowired
ProductRepo productRepo;
@GetMapping("/getProductsObservable")
public Observable<Product> getProductsObservable(@RequestParam int limit) {
return productRepo.getCouchbaseOperations().findByN1QL(N1qlQuery.simple("select META(pmc).id as _ID, META(pmc).cas as _CAS, pmc.* from pmc USE KEYS (select raw meta().id from pmc where meta().id like \"product_%\" order by meta().id LIMIT " + limit + ") order by meta().id"), Product.class);
}
Non-Reactive Java Code:
@GetMapping("/getRecords")
@ResponseBody
public String getDataByKey(@RequestParam String keyPrefix, @RequestParam(required=false) Integer limit, @RequestParam(required=false) Integer skip) {
if(skip==null) {
skip=0;
}
if(limit==null) {
limit=10;
}
StringBuffer stringBuffer = new StringBuffer();
StringBuffer query = new StringBuffer();
try {
N1qlParams n1qlParams = N1qlParams.build().pretty(false).disableMetrics(false);
query.append("select * from pmc USE KEYS ");
query.append("(select raw meta().id from pmc where meta().id like '").append(keyPrefix).append("%'").append(" order by meta().id LIMIT ").append(limit).append(" OFFSET " ).append(skip).append(") order by META().id");
Instant startTime = Instant.now();
stringBuffer.append("Query: Start time: " + startTime).append("\n<br/>\n");
System.out.println("Query: " + query.toString());
N1qlQueryResult result = template.getCouchbaseBucket().query(N1qlQuery.simple(query.toString(), n1qlParams));
Instant endTime = Instant.now();
stringBuffer.append("Query: End time: " + endTime).append("\n<br/>\n");
stringBuffer.append("Query: Total Time Taken: " + (endTime.toEpochMilli() - startTime.toEpochMilli())).append("\n<br/>\n");
stringBuffer.append("N1qlMetrics: " + result.info()).append("\n<br/>\n");
startTime = Instant.now();
stringBuffer.append("Print Data: Start time: " + startTime).append("\n<br/>\n");
stringBuffer.append("Count: " + result.allRows().size()).append("\n<br/>\n");
for (N1qlQueryRow row : result) {
stringBuffer.append(row.value().toString().substring(0, 60)).append(".....").append("\n<br/>\n");
}
endTime = Instant.now();
stringBuffer.append("Print Data: End time: " + endTime).append("\n<br/>\n");
stringBuffer.append("Print Data: Total Time Taken: " + (endTime.toEpochMilli() - startTime.toEpochMilli())).append("\n<br/>\n");
System.out.println(stringBuffer.toString());
return stringBuffer.toString();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
Python code:
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
from couchbase.n1ql import N1QLQuery
from datetime import datetime
cluster = Cluster('couchbase://localhost')
authenticator = PasswordAuthenticator('pmc', '*******')
cluster.authenticate(authenticator)
cb = cluster.open_bucket('pmc')
print "Query: start time: " + str(datetime.now())
row_iter = cb.n1ql_query(N1QLQuery('select pmc.* from pmc USE KEYS (select raw meta().id from pmc where meta().id like "product_%" order by meta().id LIMIT 1000 OFFSET 100) order by meta().id'))
print "Query: end time: " + str(datetime.now())
print "Print Data: Start time: " + str(datetime.now())
for row in row_iter:
print 'Data: {0}'.format(row['product_attribute1'])
print "Print Data: End time: " + str(datetime.now())
@himanshu.mps thanks for sharing - and what is the time difference you are observing? as in the python code and the java code per query. and how long does it take when executed in the UI?