카우치베이스 서버

Feature Enhancements to N1QL in the Couchbase Server 5.0 March Developer Build

With the recent March 2017 Developer build of Couchbase there were many bug fixes, but also feature enhancements to core technologies like N1QL.  For example, now indexes can be created on various meta information such as the document cas and expiration values.  Another example includes a simplified syntax when creating array indexes.

We’re going to take a look at some of these enhancements and how you might use them in your own application.

Simplified Array Indexing in Couchbase 5.0

With Couchbase Server 4.5 came the array indexing. Take the following sample document for example:

New Array Index Syntax

Before Couchbase 5.0, to index the array elements found in social-media you had to write an index that looked something like the following:

In the above example, the FOR operator was necessary for array indexing.  In Couchbase Server 5.0 there is a much more simplified syntax.  The same index can be created via the following:

To make sure your index works, you can execute the following N1QL query:

When looking through the results of the EXPLAIN you should see that it is using the ism index that was previously created.

Now just because the simplified syntax exists doesn’t mean you can’t use the previous syntax when array indexing.  The following would be a perfect example of why the previous syntax would still be valid:

Notice that a WHEN operator was used when creating the ism_website index above.

More information on array indexing can be found 여기, along with other useful documentation on creating indexes in Couchbase.

Relaxed Variable Match Requirement for Array Indexes

In 4.x releases, the array indexing required usage of exactly same variable names in the SELECT query that were used in the CREATE INDEX statement. Find more details 여기.

For example, referring to the previous queries seen above, note that the variable 미디어 that is used to iterate through the array social-media is very important. Array indexing in 4.x mandates the exact variable name 미디어 to be used in the previous SELECT query.

Couchbase 5.0 release relaxes this requirement, and following query would work perfectly fine:

Note that, the index ism uses variable name 미디어, but the query above uses m. Still, the above query can use the index ism successfully.

A Relaxed Whole Array Index Key Requirement for Array Indexes

Also recall, in 4.x releases, the covered array indexing requires the whole array attribute as a mandatory index-key in the array index definition.  For example, take the following query again:

The covered array index corresponding to the above query would be:

Note that, the second index key social-media is mandatory, for example to cover following query:

In Couchbase 5.0, this same query will be covered by index ism at the very start of this article.

It is important to note that, this feature brings lot of power to Array Indexes. Because, now each entry in the array index consumes less storage space and memory. It enables following benefits:

  • Usage of covered array indexes for larger arrays
  • Brings more efficiency and performance to queries using covered array indexes

Indexing Document Meta Information

Let’s take a turn here and discuss the new meta information that can be indexed.  Previously indexes could be created on the meta().id property, but now both the meta().cas 그리고 meta().expiration properties are supported.

So how do we create an index that uses the meta properties as keys?  It isn’t any different from what you already know.  Take the following covering indexes for example:

Now if I wanted to use the idx_cas 그리고 idx_exp indexes in a N1QL query, I could do something like the following:

0

So why would being able to index these properties?  Well, what if you wanted to do a query for all documents that had expired today or all documents that were modified today?

For more information on the CAS and expiration properties, visit 여기.  For more information on indexing or using N1QL with Couchbase, check out the 카우치베이스 개발자 포털.

이 기사 공유하기

작가

닉 라보이(Nic Raboy)는 모던 웹 및 모바일 개발 기술의 옹호자입니다. 그는 자바, 자바스크립트, 골랑(Golang)을 비롯해 앵귤러, 네이티브스크립트, 아파치 코르도바 같은 다양한 프레임워크 경험을 가지고 있습니다. 닉은 웹 및 모바일 개발을 더 쉽게 이해할 수 있도록 돕는 자신의 개발 경험에 대해 글을 씁니다.

1개의 응답

  1. […] Feature Enhancements to N1QL in the Couchbase Server 5.0 March Developer Build […]

댓글 남기기

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

개발 시작하기

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

카펠라 프리 사용하기

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

연락해

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