# Patent

## Title
Dynamic allocation of memory for memory intensive operators

## Abstract

A method, article of manufacture, and apparatus for processing queries, comprising performing a query with a first memory intensive allocation, receiving an indication that a memory intensive operator spilled to a first storage device, adjusting a resource queue memory allocation based on the indication, computing a second memory intensive allocation based on the adjusted resource queue memory allocation, and storing the second memory intensive allocation in a second storage device.

## Background
CROSS REFERENCE TO RELATED APPLICATIONS
This application is related to co-pending U.S. patent application Ser. No. 13/051,980 for RESOURCE QUEUES and filed concurrently herewith on Mar. 18, 2010, which is incorporated herein by reference for all purposes, and to co-pending U.S. patent application Ser. No. 13/051,986 for MEMORY QUOTA and filed concurrently herewith on Mar. 18, 2010, which is incorporated herein by reference for all purposes.
FIELD OF THE INVENTION
This invention relates generally to databases, and more particularly to systems and methods for allocating memory in databases.
BACKGROUND OF THE INVENTION
Databases are typically used to organize, store, and retrieve data. Recent advances in technology have allowed databases to store vast amounts of data. However, querying vast amounts of data presents many challenges.
Due to the large amount of data stored, a database query may potentially take significant system resources, such as processing power and memory. One common error when performing a query on a large database is an “Out of memory” error. This error occurs when the system has run out of physical memory to perform an operation or operations.
Conventional methods to guard against an “Out of memory” error include adding more physical memory, or reducing the amount of queries processed concurrently. However, conventional methods are either expensive, not scalable, or both. There is a need, therefore, for an improved method, article of manufacture, and apparatus for allocating memory in databases.



BRIEF DESCRIPTION OF THE DRAWINGS
The present invention will be readily understood by the following detailed description in conjunction with the accompanying drawings, wherein like reference numerals designate like structural elements, and in which:
FIG. 1 is a diagram of a database in accordance with some embodiments.
FIG. 2 is a diagram of a query tree in accordance with some embodiments.
FIG. 3 is a graph of a method to compute memory intensive allocations in accordance with some embodiments.
FIG. 4 is a flowchart of a method to process queries in accordance with some embodiments.
FIG. 5 is a flowchart of a method to process queries in accordance with some embodiments.
FIG. 6 is a flowchart of a method to process queries in accordance with some embodiments.



DETAILED DESCRIPTION
A detailed description of one or more embodiments of the invention is provided below along with accompanying figures that illustrate the principles of the invention. While the invention is described in conjunction with such embodiment(s), it should be understood that the invention is not limited to any one embodiment. On the contrary, the scope of the invention is limited only by the claims and the invention encompasses numerous alternatives, modifications, and equivalents. For the purpose of example, numerous specific details are set forth in the following description in order to provide a thorough understanding of the present invention. These details are provided for the purpose of example, and the present invention may be practiced according to the claims without some or all of these specific details. For the purpose of clarity, technical material that is known in the technical fields related to the invention has not been described in detail so that the present invention is not unnecessarily obscured.
It should be appreciated that the present invention can be implemented in numerous ways, including as a process, an apparatus, a system, a device, a method, or a computer readable medium such as a computer readable storage medium or a computer network wherein computer program instructions are sent over optical or electronic communication links. Applications may take the form of software executing on a general purpose computer or be hardwired or hard coded in hardware. In this specification, these implementations, or any other form that the invention may take, may be referred to as techniques. In general, the order of the steps of disclosed processes may be altered within the scope of the invention.
An embodiment of the invention will be described with reference to a data storage system in the form of a storage system configured to store files, but it should be understood that the principles of the invention are not limited to this configuration. Rather, they are applicable to any system capable of storing and handling various types of objects, in analog, digital, or other form. Although terms such as document, file, object, etc. may be used by way of example, the principles of the invention are not limited to any particular form of representing and storing data or other information; rather, they are equally applicable to any object capable of representing information.
Databases typically handle many queries simultaneously. For example, an airline database may handle queries from thousands of users simultaneously via the internet. Handling too many queries concurrently may lead to out of memory errors. If too many users submit queries concurrently, and the database processes those queries simultaneously, the database will run out of memory and the queries will not be completed. Another cause may be a malicious user generating queries with an intent to overload the database, or prevent others from using the database. Conventional databases do not differentiate between queries. In conventional databases, a query is just as valid as another. There is no precedence give to one query over another query, or more system resources given to one query than another query.
The enhanced techniques described herein differentiate queries by placing queries in a resource queue. In some embodiments, a resource queue may include a total memory allocation and a cap on concurrent queries, among others. Queries coming from different types of users may be placed in different resource queues. To illustrate, suppose a company has a single database servicing three departments: Legal, Engineering, and Finance. It may be preferable to create three resource queues: One for legal, one for engineering, and one for finance. Depending on policy, the three resource queues may have dramatically different characteristics. If engineering had top priority, legal middle priority, and finance low priority, the engineering resource queue may have a total memory allocation of 1 GB, the legal resource queue may have 500 MB, and the finance resource queue may have 100 MB. Further, the engineering resource queue may be allowed five concurrent queries, while the legal and finance resource queries may be allowed two queries each. In this way, queries from engineering have a higher preference. Engineering queries are allocated more system resources so they are completed faster, and they are given more concurrent queries so more of them can be completed concurrently.
Other applications of the resource query may include placing queries from “platinum customers” into a platinum customer resource queue, which may have more system resources than a “gold customers” resource queue.
The memory allocation of a resource queue may be user-defined. For example, a system administrator may decide that the engineering resource queue is allocated 1 GB of memory. However, there may be times that the system administrator underestimates, or otherwise allocates an insufficient amount of memory for a resource queue. In such cases, the query may spill over to disk after it has used up all of its allocated memory.
FIG. 1 illustrates an example of resource queues in accordance with some embodiments. Database 10 includes Data 100, Resource Queue A 102, Resource Queue B 104, and Resource Queue C 106. As illustrated in FIG. 1, Resource Queue A has three slots, Resource Queue B has two slots, and Resource Queue C has three slots. Since Resource Queue A has three slots, up to three queries may be run concurrently on Resource Queue A. Similarly, since Resource Queue B has two slots, up to two queries may be run concurrently.
FIG. 5 illustrates a method to process queries in accordance with some embodiments. In step 500, a query is received. In step 502, a classification for the query is determined. In step 504, the query is assigned a resource queue based on the determined query classification. In step 506, the query is placed in the assigned resource queue.
The actual amount of concurrent queries may be limited by factors other than available slots, such as the amount of available memory. For example, suppose Resource Queue A is allotted 300 MB of memory total. In some embodiments, this may translate to 100 MB of memory per slot in Resource Queue A (300 MB/3 slots=100 MB/slot). However, if the first query takes more than 100 MB of memory, and the second query is allocated 100 MB, the third query would be allocated less than 100 MB of memory. If allowing a query to execute with less than 100 MB allocated may be unsafe (e.g. high probability of returning an out of memory error), the system or administrator may decide not to run a third concurrent query until one of the first two queries finishes.
Further, though FIG. 1 illustrates Resource Queue A and C having the same amount of slots, the amount of memory allocated to each query within each queue may be different. For example, suppose Resource Queue A was allocated a total amount of 300 MB, each of its queries may be allocated 100 MB. However, Resource Queue C may have been a more important resource queue (e.g. “platinum” level customers' queues may be placed in this resource queue), and was allocated a total of 900 MB. In this case, each of Resource Queue C's queries may be allocated 300 MB. In some embodiments, it may be preferable to assign different slots different amounts of memory (e.g. first slot 40% of the total, second slot 30% of the total, and third slot 30% of the total).
In some embodiments, a database optimizer may translate a query into an execution plan that includes a tree of operators. There may be several ways to execute a query, and the database optimizer may choose one of many execution plans based on policy or a cost model. A user, such as a system administrator, may inspect and influence an execution plan through several mechanisms. For example, the user may change certain parameters, such as the cost of scanning a table sequentially vs using an index for random access of the table.
A typical query tree may include many operators. Such operators include sort, hash join, filter, etc. Operators may be classified as memory intensive (MI) or non-memory intensive (NMI). Sequential Scan is a NMI operator. Sort, on the other hand, is a MI operator. This is especially true if the sort operator has to sort a large amount of data. In some embodiments, it may be beneficial to allocate different amounts of memory to different operators.
For example, in some embodiments, NMI operators may be assigned a pre-set memory allocation, while MI operators may be assigned a dynamic memory allocation. Since NMI operators require little memory to complete, their allocation may be set by a system administrator or some other user having sufficient authority. For example, in some embodiments, a system administrator may assign 1 MB to each NMI operator. It should be noted that a system administrator may also determine which operators are classified as NMI, and which operators are classified as MI.
FIG. 4 illustrates a method to process queries in accordance with some embodiments. In step 400, a query tree is analyzed. In step 402, at least one operator is determined based on the query tree analysis. In step 404, a memory allocation is assigned for each of the at least one operator. In step 406, the assignment is stored in a storage device. In some embodiments, the assignment may be stored in a storage device, such as a physical hard disk, memory, a virtual hard disk, etc.
With the NMI operators' memory allocation set, it is now possible to determine how much memory the MI operators should be allocated. As discussed above, a resource queue may be allocated a specific amount of memory, and have a cap on the number of concurrent queries. Using the example above, if the engineering resource queue was allocated 1 GB of memory, and had a cap of five concurrent queries, then each query may be allocated roughly 200 MB each. This is assuming that each of the five concurrent engineering queries is given equal amounts of memory. It should be noted that the queries need not be given equal amounts of memory.
For ease of illustration, suppose 200 MB must be shared among all the operators in an engineering query. Suppose the query contained fifty NMI operators and three MI operators. If the system administrator allocated 1 MB per NMI operator, that would leave 150 MB for the three MI operators. Thus, each of the three MI operators may be allocated 50 MB. This is assuming that each of the three MI operators is given equal amounts of memory. It should be noted that the MI operators need not be given equal amounts of memory. For example, SORT may be given more memory over a hash-join.
In some embodiments, the following algorithm may be used to determine how much memory a MI operator should receive:

MI=(X−NMI*k2)/k1, where:

MI=amount of memory allocated for each MI operator,
X=total amount of memory allocated for the query,
NMI=amount of memory allocated for each NMI operator,
k2=number of NMI operators in the query, and
k1=number of MI operators in the query.
Since X and NMI may be pre-set by a system administrator, and k2 and k1 are readily ascertainable by inspecting the execution plan in some embodiments, MI allocation may be calculated.
FIG. 2 illustrates a query tree in accordance with some embodiments. Query Tree 20 includes NMI operators 200, 204 and 208, and MI operators 202 and 206. In FIG. 2, k2=3, and k1=2. Using the above equation, if a system administrator set NMI to 10 MB and X to 230 MB, MI can be calculated as (230−10*3)/2=200/2=100 MB. In some embodiments, the memory allocation information may be associated with each operator (e.g. NMI operator 200 may be associated with 10 MB, MI operator 202 may be associated with 100 MB, etc.). This memory allocation information may be stored as part of the execution plan in some embodiments.
In some cases, the MI calculated may be insufficient. For example, suppose the MI calculated was 50 MB, but the query ended up running out of memory and spilling to disk. In such cases, it may be preferable to raise X in order to accommodate the query and prevent it from spilling to disk. This may be accomplished by the system administrator, or the database itself can automatically adjust X for subsequent similar queries if a disk-spill is detected.
The above equation may be rearranged to solve for X. That is, in some embodiments:

X=MI*k1+NMI*k2, where:

X=total amount of memory allocated for the query,
MI=amount of memory allocated for each MI operator,
NMI=amount of memory allocated for each NMI operator,
k2=number of NMI operators in the query, and
k1=number of MI operators in the query.
In some embodiments, X may be computed analytically using a linear equation. However, in some embodiments, X may not be a linear function of MI. Rather, X may be a nonlinear function that may be proportionate to MI*k1+NMI*k2. In some embodiments, it may be preferable to employ a binary search algorithm to find the optimal value of X. For example, the binary search algorithm may begin with a low, medium and high value X and compute MI's using the algorithm. Based on the 3MIs and the target, the process is repeated with a smaller range of X. Thus, a desirable X may be calculated, and the resource query may be adjusted accordingly. For example, if X is calculated to be 100 MB larger than the original value, X may be increased for subsequent similar queries.
FIG. 3 illustrates using the binary search algorithm in accordance with embodiments. The Y-axis measures the amount of memory (MB) the MI operator is allowed (Op Memory axis). The X-axis measures the amount of total memory (MB) the query is allowed (Query Memory). In terms of the above equations, the Y-axis measures MI and the X-axis measures X. As shown in FIG. 3, the MI operator requires roughly 15 MB in order to avoid spilling over to disk. Since the relationship between MI and X is non-linear, the binary search algorithm is used to determine the appropriate X value. A first try may be X=128 MB. This results in MI being allocated about 7 MB. This is less than the 15 MB required, so a higher value of X may be chosen. A second try may be X=300 MB. As shown in the graph, this results in MI being allocated about 24 MB. Although this would satisfy the MI operator, it may be undesirable to allocate too much memory to the MI operator. Since we now know 128 MB<X<300 MB, we can further narrow the range using the same process. For example, the third guess may be X=200, which would result in roughly MI=13. This is closer to the required MI value. The binary search algorithm may be considered complete if the MI value calculated is within a pre-defined tolerance range. For example, if a system can tolerate MI=15 MB+/−1 MB, then a MI value of 16 would be acceptable, even though 1 MB would be allocated and unused.
The value of X may be associated with a particular query. For example, X may be stored as part of a query plan's fingerprint, indicating that a query with a specific set and sequence of operators should be allocated this much memory. In some embodiments, the amount of memory allocated may be stored as part of the query's execution plan. Each operator in the execution plan may be associated with a memory allocation that the operator is allowed to use.
FIG. 6 illustrates a method to process queries in accordance with some embodiments. In step 600, a query is performed with a first intensive memory allocation. In step 602, an indication that a memory intensive operator in the query spilled to a first storage device is received. In step 604, a resource queue memory allocation is adjusted based on the indication. In step 606, a second memory intensive allocation is computed based on the adjusted resource queue memory allocation. In step 608, the second memory intensive allocation is stored in a second storage device.
Though FIG. 6 illustrates only one adjustment of the memory intensive allocation, multiple readjustments may take place. For example, in some embodiments where the binary search algorithm is used to recalculate the memory intensive allocation, multiple readjustments may take place. The binary search algorithm's first result may be too low, second result may be too high, third attempt may be too low, and so on. Depending on the tolerance of a system, multiple tries may be needed before an acceptable memory intensive allocation is computed. It is anticipated that a third, fourth, fifth, etc. memory intensive allocation may be computed based on many different resource queue memory allocations.
Further, though FIG. 6 illustrates an operator spilling to a first storage device, and the second memory intensive allocation in a second storage device, these two storage devices may be the same storage device. For example, a memory intensive operator may be spilled onto a hard disk, and the second memory intensive allocation calculated may be stored on the same hard disk.
For the sake of clarity, the processes and methods herein have been illustrated with a specific flow, but it should be understood that other sequences may be possible and that some may be performed in parallel, without departing from the spirit of the invention. Additionally, steps may be subdivided or combined. As disclosed herein, software written in accordance with the present invention may be stored in some form of computer-readable medium, such as memory or CD-ROM, or transmitted over a network, and executed by a processor.
All references cited herein are intended to be incorporated by reference. Although the present invention has been described above in terms of specific embodiments, it is anticipated that alterations and modifications to this invention will no doubt become apparent to those skilled in the art and may be practiced within the scope and equivalents of the appended claims. More than one computer may be used, such as by using multiple computers in a parallel or load-sharing arrangement or distributing tasks across multiple computers such that, as a whole, they perform the functions of the components identified herein; i.e. they take the place of a single computer. Various functions described above may be performed by a single process or groups of processes, on a single computer or distributed over several computers. Processes may invoke other processes to handle certain tasks. A single storage device may be used, or several may be used to take the place of a single storage device. The disclosed embodiments are illustrative and not restrictive, and the invention is not to be limited to the details given herein. There are many alternative ways of implementing the invention. It is therefore intended that the disclosure and following claims be interpreted as covering all such alterations and modifications as fall within the true spirit and scope of the invention.

## Claims

1. A method for processing database queries in a database system, comprising:
performing a query that comprises memory intensive query operators and non-memory intensive query operators with a predetermined first memory allocation to a resource queue of the database system which performs said query;
assigning said non-memory intensive query operators a pre-set first portion of said first memory allocation and assigning a remaining second portion of said first memory allocation for the memory intensive query operators of said query;
receiving an indication that a memory intensive query operator of said query spilled to a storage device;
computing a new second portion of said predetermined first memory allocation for the memory intensive query operators based on the indication; and
dynamically adjusting the second portion of the first memory allocation assigned to said memory intensive query operators in accordance with the computed new second portion.

2. The method as recited in claim 1, further comprising associating the query with a query fingerprint that indicates a specific set and sequence of memory intensive operators of the query, and wherein said first memory allocation is based upon said query fingerprint.

3. The method as recited in claim 1, wherein computing the new second portion of the first memory allocation includes using a binary search algorithm to determine a value of the second portion that is sufficient to prevent said one of said memory intensive operators of the query from spilling over to said storage device.

4. The method as recited in claim 1, further comprising:
computing a second memory allocation for said resource queue based upon said indication;
performing a second query with the second memory allocation;
receiving a second indication that a memory intensive operator spilled to the storage device;
recomputing the second memory allocation based on the second indication; and
dynamically making a third memory allocation to said resource queue in accordance with said recomputed second memory allocation.

5. The method as recited in claim 1, further comprising, repeating said query with said new second memory portion, and prior to said repeating, dynamically allocating different amounts of said second portion to different ones of said memory intensive operators.

6. The method as recited in claim 4, wherein performing the second query with the second memory allocation includes adjusting the second memory allocation based upon the second query's association with a query fingerprint that indicates a specific set and sequence of memory intensive query operators of the second query.

7. A system for processing database queries, comprising a first storage device, a second storage device, a resource queue, and a processor configured to:
perform a query that comprises memory intensive query operators and non-memory intensive query operators with a predetermined first memory allocation to the resource queue;
assign said non-memory intensive query operators a pre-set first portion of said first memory allocation and assign a remaining portion of said first memory allocation for the memory intensive query operators;
receive an indication that a memory intensive query operator of said query spilled to first storage device;
compute a new second portion of said first memory allocation for the memory intensive query operators based on the indication;
dynamically adjust the second portion of the first memory allocation in accordance with the computed new second portion; and
store the computed new second portion of said first memory allocation in the second storage device.

8. A computer program product for processing database queries, comprising a non-transitory computer readable medium having program instructions embodied therein for controlling the operation of a computer for:
performing a query that comprises memory intensive query operators and non-memory intensive query operators with a predetermined first memory allocation to a resource queue of the database system which performs said query;
assigning said non-memory intensive query operators a pre-set first portion of said first memory allocation and assigning a remaining second portion of said first memory allocation for the memory intensive query operators of said query;
receiving an indication that a memory intensive query operator of said query spilled to a storage device;
computing a new second portion of said first memory allocation for the memory intensive query operators based on the indication; and
dynamically adjusting the second portion of the first memory allocation assigned to said memory intensive query operators in accordance with the computed new second portion.

9. The computer program product of claim 8 further comprising prior to performing said query with said new second portion of said first memory allocation, dynamically allocating different amounts of said computed new second portion to different ones of said memory intensive operators of said query.

10. The computer program product of claim 8, wherein there are a plurality of queries performed by said resource queue, and said assigning comprises dynamically readjusting the first memory allocation based upon the memory requirements of memory intensive operators of said plurality of queries.

11. The computer program product of claim 8 wherein there are a plurality of queries, and said dynamically adjusting comprises dynamically readjusting the first memory allocation based upon a number of queries being concurrently processed.