Dot Net Solutions
George V Place,
4 Thames Avenue
Windsor
Berkshire
SL4 1QP
Great Britain
0845 402 1752
GEO: -0.606174, 51.4843
 
 
 
 

Chronological Query Pattern - Windows Azure Table Design Patterns 

Tags: Azure, Design

This post is part of a series of posts that aims to capture a number of emerging design patterns for the Windows Azure Platform. This series will begin with emerging design patterns for Windows Azure Table storage.

The Chronological Query Pattern aims to support queries that must return data in chronological or reverse chronological order.

Example

Looking at a standard example of blog data, stored as:

PartitionKey (User) RowKey Title Date Posted URL Post
Marcus (guid) Patterns for… 27/5/10
Marcus (guid) Windows Azure… 28/5/10
Marcus (guid) ScrumWall … 21/4/09 ..

Challenge

Using LINQ to return the list of blogs in chronological or, in this case, reverse chronological order the following query might be tried:

var results =
    from p in this.context.Posts
    where p.PartitionKey == "Marcus"
    orderby p.DatePosted desc
    select p;

However, the orderby statement is not supported by Windows Azure Table storage (http://msdn.microsoft.com/en-us/library/dd135725.aspx).

Solution

The entities within a partition in a table are returned in the lexicographical order of the row key. This is a feature that can be utilised to ensure that entities are returned with a particular sort order. To create a row key that enables the data to be returned in reverse chronological order, the following statement with a string representation of ticks can be used:

RowKey  =
    String.Format("{0:D19}",
        DateTime.MaxValue.Ticks -      
            DateTime.UtcNow.Ticks );

Updating the blog table and replacing the Row Key with this derived value yields:

PartitionKey (User) RowKey Title Date Posted URL Post
Marcus 2521272959999999999 Windows Azure… 28/5/10
Marcus 2521273823999999999 Patterns for… 27/5/10
Marcus 2521620287999999999 ScrumWall … 21/4/09

The original LINQ query can  be used with the order by statement omitted with the blog records returned in reverse chronological order.

Summary

Motivation:

To support queries that must return data in chronological or reverse chronological order.

Implementation:

The data within a partition in a table is returned in the lexicographical order of the row key.

Uses:

Where the chronological order of the data is important. Such as retrieving the “newest” or “oldest” items.

Reference

Windows Azure Table (Tips and Tricks Section)

Also See

Table Name Key Pattern

Hash Partitioning Pattern

Transactional Master-Item Record Pattern

Chronological Query Pattern

Starts With Query Pattern

Author: Marcus Tillett
@drmarcustillett

Tweet
Published: 28 May 2010  09:18
2  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        

Links to this post

No linkbacks added

Comments


Nrupal  commented on  28/05/2010  12:15:36 
Hi Marcus, I was at the azure user group meeting on May 27th. It was a good one. Can you please confirm the following: The limitations you highlighted apply only to the Windows Azure Table Storage not to the SQL Azure. If true. Then why not use SQL Azure for all the data storage and use Azure Table storage for files or temporary data? Your time is much appreciated. Thanks.


Marcus  commented on  28/05/2010  17:45:19 
Thanks for your question Nrupal. Windows Azure Table storage is designed for massive amounts of data providing truly infinite scalability. There are certainly reasons to choose SQL Azure, but also many reasons why Table storage is a good choice. SQL Azure is limited to 50GB for instance and cost nearly 100 times more per GB, but does provide full transactional support inside the database.



 
 
 
 

Post comment

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation