Querying with N1QL

    +
    Parallel data management for complex queries over many records, using a familiar N1QL-like syntax.

    The N1QL Query Language provides a familiar, SQL-like experience for querying documents stored in Couchbase. You can read up on the language in our reference guide, but you probably just want to dive into a practical example.

    Below, we fill in some of the gaps between reference and rolling-up-your-sleeves practicality, with discussion of a few areas of the Query service where more background knowledge will help you to better program your application.

    Indexes

    The Couchbase query service makes use of indexes in order to do its work. Indexes replicate subsets of documents from data nodes over to index nodes, allowing specific data (for example, specific document properties) to be retrieved quickly, and to distribute load away from data nodes in MDS topologies.

    In order to make a bucket queryable, it must have at least one index defined.

    You can define a primary index on a bucket. When a primary index is defined you can issue non-covered (see below) queries on the bucket as well. This includes using the META function in the queries.

    CREATE PRIMARY INDEX ON `users`

    You can also define indexes over given document fields and then use those fields in the query:

    CREATE INDEX ix_name ON `users`(name);
    CREATE INDEX ix_email ON `users`(email);

    This would allow you to query the users bucket regarding a document’s name or email properties, thus:

    SELECT name, email FROM `users` WHERE name="Monty Python" OR email="monty@python.org";

    Indexes help improve the performance of a query. When an index includes the actual values of all the fields specified in the query, the index covers the query, and eliminates the need to fetch the actual values from the Data Service. An index, in this case, is called a covering index, and the query is called a covered query. For more information, see Covering Indexes.

    You can also create and define indexes in the SDK using:

    mgr := cluster.QueryIndexes()
    err = mgr.CreatePrimaryIndex(bucketName, nil)
    err = mgr.CreateIndex(bucketName, "index_name", []string{"name"}, nil)
    err = mgr.CreateIndex(bucketName, "index_email", []string{"email"}, nil)

    Index Building

    Creating indexes on buckets with many existing documents can take a long time. You can build indexes in the background, creating deferred indexes. The deferred indexes can be built together, rather than having to re-scan the entire bucket for each index.

    CREATE PRIMARY INDEX ON `users` WITH {"defer_build": true};
    CREATE INDEX ix_name ON `users`(name) WITH {"defer_build": true};
    CREATE INDEX ix_email ON `users`(email) WITH {"defer_build": true};
    BUILD INDEX ON `users`(`#primary`, `ix_name`, `ix_email`);

    The indexes are not built until the BUILD INDEX statement is executed. At this point, the server scans all of the documents in the users bucket, and indexes it for all of the applicable indexes (in this case, those that have a name or email field).

    Building deferred indexes can also be done via the SDK:

    mgr := cluster.QueryIndexes()
    err = mgr.CreatePrimaryIndex(bucketName, &gocb.CreatePrimaryQueryIndexOptions{Deferred: true})
    err = mgr.CreateIndex(bucketName, "index_name", []string{"name"},
        &gocb.CreateQueryIndexOptions{Deferred: true},
    )
    err = mgr.CreateIndex(bucketName, "index_email", []string{"email"},
        &gocb.CreateQueryIndexOptions{Deferred: true},
    )
    indexesToBuild, err := mgr.BuildDeferredIndexes(bucketName, nil)
    err = mgr.WatchIndexes(bucketName, []string{"ix_name", "ix_email", "#primary"}, time.Duration(2*time.Second), nil)

    Index Consistency

    Because indexes are by design outside the data service, they are eventually consistent with respect to changes to documents and, depending on how you issue the query, may at times not contain the most up-to-date information. This may especially be the case when deployed in a write-heavy environment: changes may take some time to propagate over to the index nodes.

    The asynchronous updating nature of Global Secondary Indexes (GSIs) means that they can be very quick to query and do not require the additional overhead of index recaclculations at the time documents are modified. N1QL queries are forwarded to the relevant indexes, and the queries are done based on indexed information, rather than the documents as they exist in the data service.

    With default query options, the query service will rely on the current index state: the most up-to-date document versions are not retrieved, and only the indexed versions are queried. This provides the best performance. Only updates occurring with a small time frame may not yet have been indexed. For cases where consistency is more important than performance, the scan_consistency property of a query may be set to REQUEST_PLUS. ensuring that indexes are synchronized with the data service before querying.

    The following options are available:

    • not_bounded: Executes the query immediately, without requiring any consistency for the query. If index-maintenance is running behind, out-of-date results may be returned.

    • at_plus: Executes the query, requiring indexes first to be updated to the timestamp of the last update. If index-maintenance is running behind, the query waits for it to catch up.

    • request_plus: Executes the query, requiring the indexes first to be updated to the timestamp of the current query-request. If index-maintenance is running behind, the query waits for it to catch up.

    For N1QL, the default consistency is not_bounded.

    Consider the following snippet:

    random := rand.Intn(10000000)
    user := struct{
        Name   string `json:"name"`
        Email  string `json:"email"`
        Random int    `json:"random"`
    }{Name: "Brass Doorknob", Email:"brass.doorknob@juno.com", Random:random}
    
    _, err := collection.Upsert(fmt.Sprintf("user:%d", random), user, nil)
    
    results, err := cluster.Query("SELECT name, email, random, META(default).id FROM default WHERE $1 IN name'",
        &gocb.QueryOptions{
            PositionalParameters: []interface{}{"Brass"},
        },
    )

    The above query may not return the newly inserted document because it has not yet been indexed. The query is issued immediately after document creation, and in this case the query engine may process the query before the index has been updated.

    If the above code is modified to use RequestPlus, query processing will wait until all updates have been processed and recalculated into the index from the point in time the query was received:

    results, err := cluster.Query("SELECT name, email, random, META(default).id FROM default WHERE $1 IN name'",
        &gocb.QueryOptions{
            PositionalParameters: []interface{}{"Brass"},
            ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
        },
    )

    This gives the application developer more control over the balance between performance (latency) and consistency, and allows optimization on a case-by-case basis.