1. Field
This application relates generally to data management and, more specifically, to systems and processes for storing and retrieving data using indexes.
2. Related Art
Data management systems are often used to store, search, and retrieve large amounts of data. The data may be stored as entries in a “table” containing a set of numbered rows, where each row includes one or more columns of data values of various types. A data structure called an “index” may be used to organize the data entries of the table by mapping each value contained in the set of rows with the row(s) in which that value appears. While useful for producing fast search results, indexes must be managed as data is added, removed, or altered (collectively called “updates” to the index). In applications where the volume and frequency of data updates are very large, managing conventional indexes becomes a costly task that may require expensive, high-speed hardware to operate within reasonable time constraints.
To illustrate,
Some conventional data management systems may implement index 103 using a B-tree index. In a typical B-tree index, the keys may be stored in nodes of the tree and may be arranged in ascending numerical and/or alphabetical order from left to right. Further, child nodes of a particular node may also be arranged in ascending numerical and/or alphabetical order based on the values of their keys and the values of the keys stored in the parent node separating the adjacent child nodes. Using this type of data structure to implement index 103, nodes containing a desired value can be searched relatively quickly by starting at the root node and navigating down the branches of the tree.
In cases where the index is larger than available memory for storing the index, the index is typically stored primarily on hard-disk storage devices due to their large capacity and low-cost. In these instances, portions of the index may be cached in available memory to improve performance. When portions of the index stored on the hard-disk storage are needed (e.g., when a key is to be added or deleted from the index), a disk caching mechanism may read in a disk page to memory. However, when there is insufficient space in memory to store the page, one or more pages stored in memory may be written to hard-disk storage (e.g., in the case of updates) and removed from memory to free up space for the incoming page. Unfortunately, if the removed page is later needed, it must be loaded back into memory, and some other page in memory must be written to hard-disk storage (e.g., in the case of updates) and removed from memory.
A typical process 200 for updating an index, such as index 103, is illustrated in
In the illustrated example of
Disk reads and writes are slow compared to memory accesses, and random-access reads and writes are slow compared to streaming reads and writes. Consequently, when used to index data containing a large number of entries and when that data is updated in large batches, the repeated navigation of index 103 using process 200 to maintain the index may result in very slow updating of data.
In addition, B-trees and other index structures designed to be modified in place, typically have large amounts of unused space in them to leave room for new entries. This unused space typically has to be read along with the used space, resulting in slower performance. Additional disk space may also be required to store the index.
Thus, improved management of indexes capable of supporting large updates on large indexes is desired.
Processes for indexing data stored in a plurality of rows in a data table are disclosed. In some examples, the process may include storing, in a memory, an update to a row of a data table, wherein the update to the row of the data table is stored as an entry comprising a key and a row identifier associated with the row; sorting a plurality of entries stored in the memory in response to a first threshold condition, wherein the plurality of entries comprises the entry; storing the sorted plurality of entries as a first sub-index; and updating a second sub-index based on a set of first sub-indexes in response to a second threshold condition, wherein the set of first sub-indexes comprises the first sub-index.
In some examples, after updating the second sub-index based on the set of first sub-indexes, the second sub-index may include: a plurality of keys contained in the data table; and one or more lists of row identifiers associated with plurality of keys, wherein the one or more lists of row identifiers comprise row identifiers corresponding to the rows of the data table that include the plurality of keys.
In some examples, the first threshold condition may include insufficient space in the memory to store the update. In other examples, the second threshold condition may include a threshold number of sub-indexes in the set of first sub-indexes, a threshold length of time, or a threshold amount of storage occupied by the set of first sub-indexes.
In some examples, the set of first sub-indexes and the second sub-index may be stored on a hard-disk drive. In other examples, a size of each sub-index of the set of first sub-indexes may be equal to or less than a size of a portion of the memory allocated to store updates to the data table.
In some examples, the update to the row of the data table may include an addition of the row to the data table or a deletion of the row from the data table.
In some examples, the set of first sub-indexes may include a first set of entries that correspond to updates for adding rows to the data table and a second set of entries that correspond to updates for deleting rows from the data table. In other examples, updating the second sub-index may include: adding entries of the first set of entries to the second sub-index; and removing entries of the second set of entries from the second sub-index.
In some examples, the process may further include deleting the set of first sub-indexes after updating the second sub-index. In other examples, the process may further include searching the set of first sub-indexes and the second sub-index for a search key.
Systems and computer-readable storage medium for indexing data are also disclosed.
The following description is presented to enable a person of ordinary skill in the art to make and use the various embodiments. Descriptions of specific devices, techniques, and applications are provided only as examples. Various modifications to the examples described herein will be readily apparent to those of ordinary skill in the art, and the general principles defined herein may be applied to other examples and applications without departing from the spirit and scope of the various embodiments. Thus, the various embodiments are not intended to be limited to the examples described herein and shown, but are to be accorded the scope consistent with the claims.
Various embodiments are described below relating to managing data using a composite index formed from a major sub-index and zero or more minor sub-indexes. Updates to the data may be cached in memory. When the cache memory becomes full, the contents of the cache may be sorted and stored as entries in a minor sub-index in a hard-disk drive with a single streaming disk write. In response to a threshold condition, the major sub-index may be updated using streaming disk accesses based on the entries in the minor sub-indexes. Once the major sub-index is updated to include all of the updates from the minor sub-indexes, the minor sub-indexes may be deleted.
Updates to data table 301 may be cached in memory before being sorted and stored in hard-disk storage as a minor sub-index 305. Minor sub-index 305 may store updates to the data table 301 in the form of entries. In the example shown in
In some examples, as will be discussed in greater detail below, each minor sub-index may be designated as an add or delete sub-index indicating whether the entries contained in that minor sub-index correspond to additions or deletions being made to the data table. The designation may be represented by a single bit, a number, a character, a string of numbers and/or characters, or the like. For example, a “0” bit may indicate that the minor sub-index is designated as an add minor sub-index while a “1” bit may indicate that the minor sub-index is designated as a delete minor sub-index. As the cache used to store updates to data table 301 becomes filled with entries corresponding to updates made to data table 301, the contents of the cache may be sorted and stored as a new minor sub-index in order to accommodate the incoming updates to data table 301. The composite index may include any number of minor sub-indexes, but a threshold condition may be established to limit the maximum number of minor sub-indexes. The threshold condition may include a maximum number of minor sub-indexes, maximum amount of storage space occupied by the minor sub-indexes, a threshold length of time, or the like. As discussed in greater detail below, once the threshold condition is met, the minor sub-indexes may be collapsed into the major sub-index 303.
In some examples, the size of each minor sub-index may be equal to or less than the size of the memory available to the system for caching entries corresponding to updates made to the data table. For example, the size of each minor sub-index may be equal to or less than the size of the random access memory (RAM) allocated to serve as the cache for index entries because the entire cache may be output as a single minor sub-index. In these examples, the table updates currently being loaded may be stored in memory in the index-entry cache, while the minor sub-indexes and the major sub-index may be stored on a hard-disk storage medium. As the cache currently being written becomes full, the cache contents may be sorted and stored in hard-disk storage as a new minor sub-index. The cached entries may be deleted from memory, allowing a new cache of entries to be generated in memory.
As mentioned above, the composite index may further include one or more major sub-indexes 303 for associating keys with one or more rows of data table 301 containing those keys. As shown in
To illustrate the operation of the composite index,
At block 403, it may be determined if there is sufficient space in memory to store the entry that was received at block 401. For instance, continuing with the example provided above, it may be determined whether or not there is sufficient space in memory to store a mapping of the key “qpr” to Row ID 45. If there is sufficient space in memory, the process may proceed to block 405.
At block 405, the entry received at block 401 may be cached in memory by writing the entry to memory. For instance, continuing with the example provided above,
Once an entry is received and the memory is determined to contain insufficient space to store the new entry at block 403, the process may instead proceed to block 407. For example, as shown in
After sorting at block 407, process 400 may proceed to block 409 where the sorted cached data may be written from memory to hard-disk storage as a minor sub-index. The cached data may subsequently be deleted from memory. Once the minor sub-index is written to hard-disk, it may be determined whether or not a threshold condition for the minor sub-indexes has been reached at block 411. In some examples, the threshold condition may include a threshold number of minor sub-indexes that can be created, a threshold amount of storage being occupied by the minor sub-indexes, a threshold length of time since the first sub-index was created, or the like. For example, referring to
Process 400 may be repeated to index updates made to table 501 using blocks 401, 403, 405, 407, 409, and 411, as discussed above, until a threshold condition occurs for the minor sub-indexes. For example, as shown in
At block 413, the major sub-index may be updated based on the minor sub-indexes.
To illustrate the operation of block 1103,
However, any entries contained in a delete minor sub-index, such as minor sub-index 509, may cause the unique identifiers associated with keys in the delete minor sub-index to be removed from the list of Row IDs for those keys in the temporary sub-index 511. For example, as shown in
Referring back to
Referring back to
Using processes 400 and 1100 described above, a composite index may be managed by caching updates (e.g., additions and deletions) to a data table in memory. The cached updates may be sorted and stored as minor sub-indexes on hard-disk storage once the memory becomes full. Since all writing to sub-indexes are done in batches, there is no need to reserve unused space for new entries within the sub-index, and sub-indexes can be implemented using any of a variety of well-known data structures optimized for compact storage, streaming writes, and fast lookups. Consequently, the total time spent reading and writing disk pages is reduced.
After searching the sub-indexes at block 1403, the process may proceed to block 1405. At block 1405, the search results produced at block 1403 may be merged. Merging may include generating a list of Row IDs based on the search results produced at block 1403 and their associated add/delete designations. For purposes of the merging performed at block 1405, the search results from the major sub-index may be considered as additions. The list may be generated by adding Row IDs contained in search results from add sub-indexes and removing Row IDs contained in search results from delete sub-indexes. The search result (if any) from the major sub-index may be processed first, followed by search results (if any) from the minor sub-indexes based on the order in which they were generated (with the earlier created sub-indexes processed first). For instance, continuing with the example provided above, the first search result from the major sub-index may include Row IDs 15 and 22. Thus, Row IDs 15 and 22 may be added to the merged list, which may now contain Row IDs 15 and 22. The second search result from minor sub-index 507 may include Row ID 49 designated as an addition. Thus, Row ID 49 may be added to the merged list, which may now contain Row IDs 15, 22, and 49. The third search result from minor sub-index 509 may include Row IDs 15 and 22 designated as deletions. Thus, Row IDs 15 and 22 may be removed from the merged list, which may now contain Row ID 49.
Once the search results are merged at block 1405, the merged list of search results may be returned at block 1407. Continuing with the example above, the merged list of search results contains Row ID 49, which may be returned to the user to identify the occurrences of the key “abc” in data table 501.
In some examples, a non-transitory computer-readable storage medium can be used to store (e.g., tangibly embody) one or more computer programs for performing any one of the above-described processes by means of a computer. The computer program may be written, for example, in a general purpose programming language (e.g., Pascal, C, C++) or some specialized application-specific language. The non-transitory computer-readable medium may include storage 1503, memory 1505, embedded memory within processor 1501, an external storage device (not shown), or the like.
Although only certain exemplary embodiments have been described in detail above, those skilled in the art will readily appreciate that many modifications are possible in the exemplary embodiments without materially departing from the novel teachings and advantages of this disclosure. For example, aspects of embodiments disclosed above can be combined in other combinations to form additional embodiments. Accordingly, all such modifications are intended to be included within the scope of this disclosure.