Azure Table Storage good practices - Intra-partition secondary index pattern

Searching for records in Table Storage is super fast when using Partition and Row Keys. For most sce

Searching for records in Table Storage is super fast when using Partition and Row Keys. For most scenarios such setup will be sufficient - you either don't need additional properties to filter with or you're interested in large portions of data, which will be processed in your code. However, sometimes you'd like to make it possible to perform quick filtering using custom column. This is where intra-partition secondary index pattern helps.

The problem

The reason why queries using PKs and RKs are so fast lies behind the fact, that Table Storage automatically indexes entities using those columns. This is how it was designed and developed - in reality most scenarios are covered with this setup. On the other hand, we'd like to enable ourselves to create a table, which will keep superb performance and still allow querying other columns(like FirstName, City or Street in Employee table). Of course it's possible to perform partition scans and filter proper values in your code, yet additional overhead could be something, you cannot accept. We have to design a table in such way we'll somehow incorporate additional filters into internal design of Table Storage.

The solution

The solution here(as most solutions I present) is pretty simple. If we know, that indexes are created using PKs and RKs, we have to add additional values to them. This will allow us to take advantage of the indexing feature and let avoid additional overhead during filtering values. Let's consider following example:

/
PK	| RK	| FirstName	| LastName
employee	1	John	Doe
employee	2	Jane Doe

If we'd like to filter retrieved records using LastName = `Doe` then it'd force us to do it on our side, possible fetching more records than we need and lowering performance of our application. Now let's redesign it a little bit:

/
PK	| RK	| FirstName	| LastName
employee	1	John	Doe
employee	2	Jane Doe
employee	lastname_Doe	John	Doe
employee	lastname_Doe	Jane Doe

Now we can perform following filtering on this table:

/
$filter=(PartitionKey eq 'employee') and (RowKey eq 'lastname_Doe')

Retrieving only those records we need. 

Summary

As you can see small changes in Table Storage design can result in significant performance improvements. Some consideration here should be focused on possible duplication of data, which has to be handled on your side. If performance is important for you, this is a small price for the overall "level-up" in areas you care the most.

Comments (2) -

The single partition case is simple. Usually, you want to have more than 1 Partition Key (scalability). In this case, the secondary index you proposed requires some smart ordering of inserts or usage other exotic structures like RAMP transactions .

Kamil Mrzygłód 9/1/2017 9:01:49 AM

Hi Scooletz, thanks for this comment! For sure this scenario is not optimal for each and every case in situation, where you're under high load. I guess it'd be more suitable when dequeuing items from a queue or similar scenarios.

Add comment