미분류

The 5 Most Common Forum’s Questions

How to do “something” in N1QL?

First of all, if you are not familiar with N1QL I highly recommend you to spend a few minutes in our free N1QL training 여기,  or just play with it 여기.

Second, as it is a broad question, let’s go through some common scenarios:

 

Select the id of a document and all its attributes:

How to write a JOIN:

Let’s query which companies fly from San Francisco airport (SFO) to anywhere in the world using the travel sample:

JOIN clause looks like a standard SQL JOIN, the only difference here is the ON KEYS keyword, to read more about it check this article explaining visually N1QL JOINs. Couchbase 5.5 will also add support to ANSI JOINs

How to select items of an array:

Given documents like:

If we want to select all children who are more than 10 years old, we could use the UNNEST keyword:

 

 

Why my query is slow?

Probably your query is not hitting any indexes. You can check that by running your query with the 설명하다 keyword, as follows:

As you can see in the image above, the query is hitting the PrimaryScan which means that it is using the primary index. Creating a secondary index for it will potentially solve your problem:

Executing the same query again will output something like:

If your query is already hitting an index but still has a poor performance, you might want to add a more optimized index (like in this example). If you are not familiar with how to create an index, check out this blog post

 

How to paginate results in N1QL?

You can use LIMIT 그리고 OFFSET:

확인해 보세요 this tutorial to read more about it. Additionally, if you are using Spring Data you can add a Pageable object to the end of your method definition:

And then, in your Service you can use the PageRequest object:

 

My query has missing results/wrong results

By default, Couchbase supports read-after-writes whenever you get a document by its key, but your indexes and views are updated asynchronously via Data Change Protocol (DCP). So, if you are executing a query right after a write, it might be executed before the views/indexes had a chance to be updated.

Couchbase is all about speed, and no one has time to wait until all indexes and views are updated to send the response back to the client that a write has been executed successfully.

But there are few scenarios where strong consistency between writes and your queries are actually needed, for those cases you can specify via SDK that you actually want to wait until the index/view you are using is updated:

To read more about scan consistency, please refer to the 공식 문서.

In my personal experience, I only scenario where I do need consistency between writes and queries is during the integration tests phase, which is when you actually insert data and query it right after.

 

How to Creating/use Array Indexes.

This is an interesting topic, as array indexing might speed up significantly your performance. So, let’s say we have the following document structure:

Now, if we need to query hotel reviews, we could do something like:

So, the simplest index for the reviews array will look like the following:

And then, when we execute the query, voilà:

It is using the recently created index.

For more examples, check the 공식 문서 or read this excellent article about how to optimize array indexes.

 

이 기사 공유하기

작가

데니스 로사(Denis Rosa)는 카우치베이스(Couchbase)의 개발자 애드보케이트이며 독일 뮌헨에 거주하고 있습니다. 그는 소프트웨어 엔지니어로서 탄탄한 경험을 가지고 있으며 자바, 파이썬, 스칼라, 자바스크립트를 유창하게 구사합니다. 데니스는 검색, 빅데이터, AI, 마이크로서비스 및 개발자가 아름답고 빠르며 안정적이고 확장 가능한 애플리케이션을 만드는 데 도움이 되는 모든 것에 대해 글을 쓰는 것을 좋아합니다.

댓글 남기기

카우치베이스 카펠라를 시작할 준비가 되셨나요?

개발 시작하기

NoSQL을 탐색하고, 리소스를 찾아보고, 튜토리얼을 시작하려면 개발자 포털을 확인하세요.

카펠라 프리 사용하기

단 몇 번의 클릭으로 카우치베이스(Couchbase)를 직접 체험해 보세요. Capella DBaaS는 시작하기 가장 쉽고 빠른 방법입니다.

연락해

Couchbase 제품군에 대해 더 알고 싶으신가요? 저희가 도와드리겠습니다.