Create indexed fields such as data storage that are frequently referenced by query criteria. This pattern can improve query performance by allowing applications to locate data more quickly to retrieve data from the datastore.
Background and issues
Many data stores data that is organized into collections of entities by using primary keys. Applications can use this key to find and retrieve data. Figure 1 shows an example of a data store that holds the customer's information. The primary key is the customer ID.
Figure 1- customer information organized by primary key (Customer ID)
While a primary key is a valuable query that takes data based on the value of that keyword, the application may not be able to use the primary key to retrieve data based on other fields. In a customer, for example, an application cannot use the Customer ID primary key to retrieve a customer, if it specifies a value that references some other property, such as when the customer is located in the town standard query data completely. To execute a query, such as this may require a request to obtain and examine each customer's record, this may be a slow process.
Many relational database management systems support two-level indexing. A two-time index is a single data structure held by one or more non-primary (secondary) key fields, which represents the data that each index value is stored in. Items in a second index are usually sorted by the value of the second key of the method, making the data quick to find. These metrics are typically maintained automatically by the database management system.
Because you need to support your application to execute different queries, you can create any number of level two metrics. For example, in a relational database where the guest ID is the primary key of the table of the client, it may also be beneficial to supplement the auxiliary index in town if the application frequently finds the customer in its small town of residence.
However, although level two indicators are a common feature of relational systems, most NoSQL data stores using cloud applications do not provide the same functionality.
Solution Solutions
What if the data store does not support?? Secondary indexes, you can manually emulate them by creating your own index table. The index table organizes the data by the specified key. Three strategies are typically used to build an index table, depending on the number of two indexes required and the nature of the queries that the application executes:
• Duplicate the data in each index table, but instead organize it by a different key (completely denormalized). Figure 2 shows the organization of the index table, including the city and last name of the same customer information:
Figure 2-Index Table performs customer data for level two metrics. The data is copied to each index table.
This strategy may be appropriate if the number of data that is queried by using each key is relatively static when compared. This approach is useful if the data is more dynamic, and maintaining the processing overhead of each index table can become too large. In addition, if the amount of data is very large, the amount of space required to store duplicate data will be significant.
• Create A table of indexes organized by different keys and refer to the original data by using a primary key instead of repeating it, as shown in Figure 3, the original data is called a fact table:
Figure 3-Index Table performs customer data for level two metrics. The data is referenced by each index table.
This technology can save space and reduce the overhead of maintaining duplicate data. The disadvantage is that an application has to perform two lookup operations by using a second key to find data (find the primary key in the index table of the data, and then find the data in the fact table by using the primary key).
• Create a partial index table of different keys organized by repeated frequently retrieved fields . Reference raw data to access fields that are less frequently accessed. This structure is shown in Figure 4.
Figure 4-Index Table performs customer data for level two metrics. Frequently accessed data is duplicated in each index table.
Using this technique, you can strike a balance between the first two methods. You can quickly retrieve data by using a single lookup, a common query, while the spatial and maintenance overhead is not as large as copying the entire dataset.
If your application frequently queries data by specifying a combination of values (for example, "Find live in Redmond and all customers with Smith's surname "), you can implement keys in the index table of items as a cascading city attribute and last name attribute, as shown in Figure 5 in the key by town sort, Then by name for those records that have the same value as the town.
Figure 5-Index table based on composite primary key
The index table can speed up the data query operation of the Shard, and where the fragment key hash is particularly useful. Figure 6 shows an example where the Shard key is a hash of the customer ID. An index table can organize data by non-hash values (city and name) and provide the hash shard key as the lookup data. This saves the application from repeating the hash key (which can be an expensive operation) if it needs to retrieve the data falling within a range, or it needs to read the data so that the key is not hashed. For example, a query such as "Find life in Redmond all customers " can be resolved by locating a match in the Index table (which is all stored in a contiguous block), and using the key that is stored in the fragment of the index table as soon as the referenced customer data resolves.
Figure 6-Fast Lookup shard data is available in the Index table
Issues and considerations
When deciding how to implement this pattern, consider the following points:
• The overhead of maintaining a secondary index can be significant. You must analyze and understand the queries that your application uses. Only create index tables that they are most likely to be used frequently. Don't speculate. Create indexed tables to support applications that do not execute queries, or an application that only executes very accidentally.
• The cost of storing the data that is replicated in the index table and the working conditions required to maintain multiple copies of the data are significantly overhead.
• Perform an index table as a standardized structure that references the applications that the raw data might require to perform two lookup operations to find data. The first action searches the index table to retrieve the primary key, and the second uses the master key to fetch the data.
• If the system contains a large number of index tables in very large datasets, it can be difficult to maintain consistency between the index table and the original data. It is possible to design an application around the final conformance model. For example, to insert, update, or delete data, an application can send a message to a queue and have an independent task perform operations and maintain an indexed table that references the data that is not synchronized. For more information on achieving eventual consistency, see data consistency Primer.
Note:
Microsoft Azure Storage Table supports transactional updates to the data saved in the same partition for changes (referred to as entity Group transactions). If you can store a fact table and data from one or more index tables in the same partition, you can use this feature to help ensure consistency.
• The index table can be partitioned or fragmented on its own.
when to use this mode
Use this pattern to improve query performance when applications often need to use a primary (or sub-Library) key other than a key to retrieve data.
This mode may not be appropriate when:
• The data is not stable. The index table may become obsolete at a very fast rate, invalidate it, or keep the index table larger than the cost of any savings made with it.
• The field chosen as the level two key in the index table is very non-discriminating and can only have a small collection of values (for example, gender).
• Data values are selected as a two level key in an index table the balance of the field is highly skewed. For example, if 90% of the records contain the same value in one of the fields, then creating and maintaining an index table in which to find data based on the field can impose a greater overhead than through the data scan order. However, if the query is very frequent against a value that is located on the remaining 10%, the index can be useful. You must understand the doubts that your application is executing, and how often they are executed.
Example
Azure Storage tables provide a highly extensible key/value data store for applications running in the cloud. The application stores and retrieves data values by specifying a key. A data value can contain multiple fields, but the structure of a data item is an opaque table store, which only processes one data item as a byte array.
Azure Storage tables also support sharding. The Shard key consists of two components, a partition key, and a row key. Data items that have the same partition key are stored in the same partition (fragmentation), and the items are stored in a sub-library in the order of the sort keys. Table storage is optimized for executing queries within the range of row key values that are used to fetch contiguous ranges in the data descent partition. If you are building a cloud app that stores information about tables in azure, you should organize your data in consideration of this feature.
For example, consider an application that stores information about a movie. Applications often query movies by genre (action films, documentaries, History, comedy, drama, etc.). You can create an azure table partition for each type, 7 by using the type as the partition key, and specifying the name of the movie as the row key.
Figure 7-Movie data stored in Azure table, divided by genre and sorted by movie name
This approach is less effective if the app also needs to be starred by actors to query the movie. In this case, you can create a separate Azure table as an index table. The partition key is the actor and the line key is the name of the movie. The data for each actor will be stored in a separate partition. If a movie star has more than one actor, the same movie will appear in multiple partitions.
You can repeat the movie data for the values that are saved in each partition by using the first method described in the solution section above. However, it is likely that each film will be repeated (for each actor once) several times, so it may be more efficient to partially denormalized the data to support the most common queries (such as the names of other actors) and implement an application consisting of the necessary to find the complete information in the genre partition, partition key to retrieve any remaining details. This method is described in the third item in the Solution section. Figure 8 depicts this approach.
Figure 8-actor partition as image data for index table
This article is translated from msdn:http://msdn.microsoft.com/en-us/library/dn589791.aspx
Cloud computing Design Pattern (12)--Index Table mode