DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Using Covering Indexes on a Multiple Document Type Bucket

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.

Nic Raboy user avatar by
Nic Raboy
·
May. 20, 16 · Opinion
Like (3)
Save
Tweet
Share
4.01K Views

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 date and type properties. so i ran the query again with the covering index but saw no change in performance.

this lead me to running an explain 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 explain provided me, i could see that it was still trying to use the #primary 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 date and not all documents had a property type that matched tweet . 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 date property exists. we were already checking that property type matched tweet but were missing the other piece.

after running the query again, it was significantly faster. when i included explain 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 .

Document Database Couchbase Server

Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Browser Sandboxing?
  • Load Balancing Pattern
  • Uplevel Your Managers With Mini-M Support Groups
  • Deploying Java Serverless Functions as AWS Lambda

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: