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. The Guts 'n' Glory of Database Internals: Seeing the Forest for the Trees

The Guts 'n' Glory of Database Internals: Seeing the Forest for the Trees

Here, we branch off into indexes and how they can both help and complicate B+Trees to allow for quicker, cheaper data searches.

Oren Eini user avatar by
Oren Eini
·
Jun. 17, 16 · Tutorial
Like (3)
Save
Tweet
Share
2.64K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous post, I talked about B+Trees and how they work. Unfortunately, just having a B+Tree isn't enough. A B+Tree allows you to do queries (including range queries) over a single dimension. In other words, in the case of our users' data, we can easily use the B+Tree to find a particular entry by the user's id.

image

However, if we wanted to find a user by email, or by name, a B+Tree of the users' ids isn't very helpful. That means that if we wanted to do a query based on the user's email, we would need to do a table scan, run over all the records in the database and match each of them to the email we are looking for. That has a pretty significant cost to it, and it is generally something that you want to avoid at all costs.

This is where we introduce another B+Tree, this one using the users' email as the key, and whose value is the users' id. It looks something like this:

  • bar@funyun.org –>  users/12
  • foo@example.com –> users/1

So finding a user by their email is going to involve:

  1. Searching for the relevant email in the emails B+Tree.
  2. Once found, searching for the user's id in the documents B+Tree.

In other words, we have to do two B+Tree lookups (at a cost of O(logN) each) to do this lookup.

Congratulations, you just used an index. You have probably heard of indexes, and how they can dramatically improve performance (and kill it). At the most basic level, an index is just another B+Tree in the system that has some of the records' data indexed. The email B+Tree is what happens when you index an email column, for example.

And it is also the reason why indexes are expensive. Consider what happens when you insert new users into such a system. The database is going to update the two B+Trees. One for the actual data, and the other for the email that has the key of the data. And while we can arrange for the user's id to be sequential, it is very unlikely that the emails will be sequential, so it is likely to be a bit more fragmented. That said, the index isn't going to be as big as the actual data, so more entries are going to fit per page. In particular, while we can fit five entries per leaf page in the data B+Tree, the email index B+Tree is going to be around 70 entries per leaf page. So that should help keep it shallower than the data B+Tree (thus cheaper).

Note that in database terms, both B+Trees are actually indexes. The data B+Tree is a clustered index on the user id, and the email B+Tree is a non-clustered index on the email. What is the difference between the two? A non-clustered index contains as its value the key to find the relevant record. A clustered index contains the actual record data. In other words, after looking up a non-clustered index, we need to go to the clustered index to find the actual data, while if we are using a clustered index, we are done with just one lookup.

This is also the reason that you often have to do a balancing act, you have to chose what fields you'll index, because the more fields you have indexes, the faster your queries can be (O(logN) vs. O(N)), but on the other hand, that means that you need to update more B+Trees.

Finally, I'll invite you think what happens if we have an index (B+Tree) on a date field. For example, LastLogin field, and we wanted say "Gimme all the users who haven't logged in for the past 30 days." How would you do that, and what would the cost be, using the system I outlined in this post?

Database Forest (application)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Should You Know About Graph Database’s Scalability?
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Why Every Fintech Company Needs DevOps
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize

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: