Using Covering Indexes on a Multiple Document Type Bucket
We're going to take a look at what Nic Raboy did to speed up his queries, and some of the things he had to consider during the process.
Join the DZone community and get the full member experience.Join For Free
i was recently working on a project that made use of n1ql for querying couchbase server data. this was an internal java application that i was hosting on a low budget amazon ec2 instance. my problem here is that my queries were running incredibly slow. the reason for this was that i only had a primary index that was very generic.
first off, it is probably a good idea to share what version of couchbase server i am using. i am using couchbase server 4.1 on my low powered machine. the bucket i'm working with has around 100,000 documents in it of varying types. not that it matters for this article, but the application that accesses this data was built in java using the couchbase java sdk.
with my setup out of the way, let me share one of the queries i was running:
select millis_to_utc(date, '2006-01-12') as tweetdate, count(*) as count from `default` where type='tweet' group by millis_to_utc(date, '2006-01-12') order by tweetdate asc
this query would return the total number of tweets that i had saved for any particular date. time information was not important for me. note that at the start i only had a single index, being my primary index. the above query would take quite some time to run.
this is where i started to re-evaluate my strategy.
i decided to take advantage of covering indexes that were made available in couchbase 4.1. this is when we make an index that covers all properties that will be used within a query. my index creation statement for the previous query looked like this:
create index twitter_by_date on `default` (date, type) where type = 'tweet' using gsi;
yes, i am doing aggregations, but at the end of the day, i'm only querying based on the
properties. so i ran the query again with the covering index but saw no change in performance.
this lead me to running an
on the query itself to troubleshoot what was going on.
explain select millis_to_utc(date, '2006-01-12') as tweetdate, count(*) as count from `default` where type='tweet' group by millis_to_utc(date, '2006-01-12') order by tweetdate asc
when i saw the query breakdown that
provided me, i could see that it was still trying to use the
index that i had originally created. this is even after i validated that the covering index now existed in couchbase.
then i remembered that the twitter documents weren't the only types of data that existed in my bucket. in other words, not all documents had a property called
and not all documents had a property
. i now had to revise the query that i wanted to run to check for these scenarios.
select millis_to_utc(date, '2006-01-12') as tweetdate, count(*) as count from `default` where type='tweet' and date is not missing group by millis_to_utc(date, '2006-01-12') order by tweetdate asc
in the above query, notice in particular how i added
and date is not missing
. i'm first checking that the
property exists. we were already checking that property
but were missing the other piece.
after running the query again, it was significantly faster. when i included
i could immediately see that the query was now using the covering index rather than the primary index.
to learn more about covering indexes, visit the couchbase developer portal .
Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.