DZone
Database Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > 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 · Database Zone · Opinion
Like (3)
Save
Tweet
3.82K 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

  • Cypress: The Future of Test Automation! Advantages and Disadvantages
  • CSS Position: Relative vs Position Absolute
  • 9 Extraordinary Terraform Best Practices That Will Change Your Infra World
  • Functional vs. Non-Functional Requirements: The Full Guide, Definitions, and Technical Examples

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo