The YugaByte Database Blog

Thoughts on open source, cloud native and distributed databases

A Quick Guide to Secondary Indexes in YugaByte DB

When creating a Cassandra-compatible YCQL table in YugaByte DB, you are required to create a primary key consisting of one or more columns of the table. Primary key based retrievals are efficient because YugaByte DB automatically indexes/organizes the data by the primary key. However, there are many use-cases where you may need to retrieve data using columns that are not a part of the primary key. This is where secondary indexes help.

secondary index can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records when querying by those columns. Secondary indexes require additional writes and storage space to maintain the index data structure.

YugaByte DB provides consistent (ACID), high-performance secondary indexes built on top of distributed ACID transactions.

Example of a User Profile Table

Let us imagine the use-case of storing user profiles. To keep the example simple, let us assume that each user has a first name, a last name, an email address and a unique user id.

Given that the user id is unique across all the users, it is an ideal candidate for being the primary key of the table. Lookups to the table by the user id will be very efficient, even if there are a large number of users. This works well for the cases when a logged in user wants to view or edit their profile.

Now, assume we want to lookup the user profile given an email address. Without any indexes, this query would end up scanning through all the users to search for a user with the given email address. For a large number of users, this operation is very inefficient.

A secondary index created on the email address column would speed up the lookups done by an email address. When a secondary index is created on a table, YugaByte DB automatically maintains a separate index table which is automatically updated when the primary table is modified. In this table the data is organized by the email address.

YugaByte DB uses distributed ACID transactions under the hood in order to maintain consistency of secondary indexes. This means that from the perspective of user queries, the value of the user profile table and the secondary index table are always consistent — even when updates are happening at the same time as reads. Read more about how YugaByte DB implements distributed ACID transactions.

Note that YugaByte DB is a distributed database, which means it runs on a set of nodes. Thus, for any scenario including the above, YugaByte DB can seamlessly scale out while maintaining consistency of data.

Trying it Out

Let us try out the above scenario on YugaByte DB by performing the queries.

Remember to install YugaByte DB and connect to cqlsh in order to try out this scenario on your local machine.

First, create a YCQL keyspace called myapp by running the following command in cqlsh.

Next, create a users table with user_id as the primary key column. Each user has a firstname, a lastname and an email as additional columns.

As mentioned before, the above table is optimized to perform lookups by the primary key column (which is the user_id column). It is also very efficient at handling the writes and scaling out for millions of users.

Now, in order to efficiently look up users by their email address, we need to create a secondary index on the email column. This can be done as follows.

Note that the INCLUDE clause above causes the secondary index table to store the values for the firstname and lastname columns as well, allowing queries that need to lookup the user name by their email address to be satisfied entirely from the secondary index table. Without the INCLUDE clause, the user query would first fetch the primary key from the secondary index table, and subsequently lookup the users table by the primary key. Now that we have created the table and the secondary index, let us load some data.

To retrieve the user profile a given user id (in this example user id 1), you can run the following:

You should see the following:

Next, let us query the user profile for the email address [email protected]. This can be done as follows:

You should see the following:

Updating any of the values for a row should keep the index consistent. For example, let us update the email address for the above record by doing:

Querying for the new email address should return the row:

Querying by the older value should not have any results:

At Scale Testing

YugaByte DB source ships with a number of sample apps. One of the workloads supported is the CassandraSecondaryIndex. This workload simply inserts unique key-value pairs into a table that has a secondary index on the value column, and performs lookups by the value column.

We setup a 3-node cluster of n1-standard-16 instance types (16 vCPU, 60GB RAM, 2 x 375GB SSD per node) in Google Cloud. The cluster had a replication factor of 3 allowing it to survive node failures. Each key-value being inserted was about 64 bytes in size.

In the write phase of the test, we loaded 100 million keys with 128 writers. The entire load took about 3 hours with a steady write IOPS of over 8K write ops/sec. Below is the load command.

The graph of the write phase is shown below. The throughput of the workload was a little over 8000 writes/sec at a 12ms latency.

The read phase of the test looked up the 1 billion keys at random by querying their values. The reads were done using 256 readers for about 3 hours. The read IOPS were steady at about 80k read ops/sec. Below is the read command.

Below is a graph of the total IOPS and the latency during the read phase. The graphs show about 175K reads/sec at about 0.25ms server-side latency.

What’s Next?

A number of users have expressed interest in the secondary index feature. The current secondary index feature is being beta tested out in pre-production by a few of our users. We expect secondary indexes to be ready for production in about a month (specifically, mid-August 2018).

As for next steps, here are some improvements we are working on relating to secondary indexes:

  • Currently, the secondary indexes need to be created before loading data. We will soon allow secondary indexes to be created on tables with pre-existing data.
  • YugaByte DB today only supports equality-based indexes. Equality-based indexes are ideal when queries try to lookup data by an exact value (as opposed to lookups for values less than or greater than a particular value). We are working on indexes that will allow comparison and range queries.

Interested? You can follow YugaByte DB on GitHub!

Karthik Ranganathan

Founder & CTO