{'fraction_non_alphanumeric': 0.02280248353241514, 'fraction_numerical': 0.005688802042295692, 'mean_word_length': 4.780561122244489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Probably Approximately Optimal Query Optimization

Evaluating query predicates on data samples is the only way to estimate their selectivity in certain scenarios. Finding a guaranteed optimal query plan is not a reasonable optimization goal in those cases as it might require an infinite number of samples. We therefore introduce probably approximately optimal query optimization (PAO) where the goal is to find a query plan whose cost is near-optimal with a certain probability. We will justify why PAO is a suitable formalism to model scenarios in which predicate sampling and optimization need to be interleaved. We present the first algorithm for PAO. Our algorithm is non-intrusive and uses standard query optimizers and sampling components as sub-functions. It is generic and can be applied to a wide range of scenarios. Our algorithm is iterative and calculates in each iteration a query plan together with a region in the selectivity space where the plan has near-optimal cost. It determines the confidence that the true selectivity values fall within the aforementioned region and chooses the next samples to take based on the current state if the confidence does not reach the threshold specified as problem input. We devise different algorithm variants and analyze their complexity. We experimentally compare them in terms of the number of optimizer invocations, samples, and iterations over many different query classes.


INTRODUCTION
The goal in traditional query optimization is to find an optimal query plan. This optimization goal is based on the assumption that accurate values for the selectivity of each query predicate are available. This assumption is already optimistic in traditional scenarios where predicate selectivity is estimated based on a machine-readable definition of the predicate and on statistics describing the input data. It is completely unjustified if query predicates are formulated in natural language (e.g., in the context of crowd databases where predicates are evaluated by human workers [15,26,27]), if predicates are evaluated by invoking remote services (e.g., a weather service or a credit rating service [17]), or if predicates are complex functions defined using a different language than SQL (e.g., predicates whose evaluation requires sophisticated text or video processing [17]). In all those cases, the optimizer cannot access or interpret the definition of query predicates and has a-priori no information about their selectivity.
In such cases it is still possible to evaluate predicates on data samples during optimization to estimate their selectivity. This is in fact required to make any guarantees on the quality of the generated plans. The traditional optimization goal is however not reasonable in this context: when taking it seriously, we would need to collect an unbounded number of samples in order to obtain infinitely precise selectivity estimates based on which we can produce a guaranteed optimal plan (as different plans might be optimal for different selectivity values). How should we formulate the optimization goal if optimization and selectivity sampling are interleaved? Which predicates shall we sample? How many samples should we take? How should we choose a query plan based on the collected samples? In the following we will first propose a generalization of the traditional query optimization problem and justify why it constitutes a reasonable optimization goal in our scenario. The answers to all other questions will follow from that.
Our adapted optimization goal is the following: find a query plan whose cost is not higher than optimal by more than factor α ≥ 1 with probability at least δ ∈ [0, 1]. Factor α and probability δ configure the optimizer and can be chosen by users or administrators, trading plan quality for optimization time and sampling overhead. We call this optimization problem probably approximately optimal query optimization (similar in spirit to the probably approximately correct learning framework [32]). It is a reasonable problem definition, meaning that it does not require an infinite number of samples. Simplified problem definitions do not possess that property as we show next.
Selectivity is a continuous variable that takes values between 0 and 1. Without making further assumptions, taking a finite number of samples can only yield confidence bounds for the true selectivity values but never a point estimate. The cost of query plans depends in general on the selectivity and may vary for selectivity values within the confidence bounds. Finding a plan that is optimal for all selectivity values within the confidence bounds is therefore in general not possible. We assume in the following that plan cost functions are continuous in a small area around the true selectivity values (additional assumptions are discussed in Section 3). This assumption is less strong than the standard assumption in parametric query optimization, called the guiding principle of parametric query optimization, stating that the cost of query plans is approximately linear in small areas of the selectivity space [12]. Under this assumption, we can shrink the selectivity confidence bounds using a finite number of samples until one plan has near-optimal cost (within multiplicative factor α of the optimum) within the entire cube in the selectivity space that is defined by the confidence bounds. This is why our goal must be to find a near-optimal and not an optimal plan.
In addition, we must consider the case that the true selectivity values fall outside the confidence bounds altogether. As long as we take a finite number of samples, this is always possible with non-zero probability. This is the reason that we aim at finding a near-optimal plan with probability at least δ and not to find a guaranteed near-optimal plan.
Prior work on selectivity estimation in the context of query optimization has often focused on the case of correlated predicates as they can lead to sub-optimal plan choices [9,31]. In contrast to that, we focus on novel application scenarios where even the handling of uncorrelated predicates is currently not solved in a principled manner. A recently proposed system for big data processing [23] for instance estimates the selectivity of user-defined predicates by sampling. The amount of data to sample is however decided heuristically such that no probabilistic guarantees on the optimality of the generated query plans result. In the context of crowd database systems, where users may define natural language predicates that are evaluated by crowd workers, query optimizers currently rely on the user to specify the predicate selectivity by hand [27]. Otherwise default values are used [28]. Both scenarios can benefit from the framework and approach that we introduce in the following.
Prior work (e.g., [9]) usually assumes that predicate selectivity is estimated based on data statistics which are generated in one atomic operation. This motivates a binary uncertainty model: either the selectivity of a predicate is known or unknown. With such a problem model, the goal of finding near-optimal plans is actually reasonable. We target scenarios where it is often not possible to estimate selectivity based on data statistics (e.g., how to automatically estimate selectivity for a predicate formulated in natural language for evaluation by crowd workers? [28]). We can only sample predicates and never know the precise selectivity. Hence we cannot adopt a binary uncertainty model. Another distinguishing factor between our approach and prior work is that our approach requires a minimal amount of information. We do for instance not assume that table cardinalities are known which makes it possible to support even scenarios related to crowdsourcing where tables are extracted from crowd workers and their cardinality is not known. Those seemingly subtle differences result in algorithmic challenges that are specific to our scenario.
We present a first algorithm for probably approximately optimal query optimization (PAO). This algorithm is generic and works with arbitrary plan spaces and cost models as long as cost grows monotonically in the selectivity. We assume that nothing is known about the selectivity of predicates before sampling starts and we do not make any assumptions at all about the type of probability distribution that the selectivity values follow. Furthermore, our approach is non-intrusive and can easily be implemented on top of existing optimizers. Possible application scenarios for the algorithm presented in this paper include cost-based optimizers in crowd database systems (which currently assume that users specify selectivity values together with their queries [28,27]) or optimization of queries including userdefined predicates (sampling is used in that context but without formal guarantees on plan quality [23]).
The optimizer consists of three components in our scenario: a standard optimizer that finds optimal plans for given selectivity values, a sampling component that evaluates predicates on data samples, and a meta-algorithm that coordinates invocations of the sampling component and the standard optimizer in order to meet the probabilistic guarantees specified by PAO. We propose a meta-algorithm for PAO in this paper while we assume that a standard optimizer and a sampling component are provided.
We use a standard optimizer that requires selectivity estimates for invocation. Those estimates are derived from samples. On the other hand, we can exploit information about the optimal cost function, namely its sensitivity to the selectivity of specific predicates, to choose the predicates to sample and to optimize the number of samples to take. This cyclic dependency motivates an iterative algorithm: our algorithm interleaves sampling and optimization and executes several stages of sampling and optimization until the PAO guarantees are met.
After having collected a small initial set of samples, our algorithm executes the following four steps in each iteration: First, it invokes the standard optimizer to obtain the best query plan for the current selectivity sample means. Second, it issues more calls to the standard optimizer in order to determine a cube-shaped region in the selectivity space for which the plan generated in the first step satisfies the nearoptimality guarantees specified as problem input. Third, it uses a probabilistic model based on the Hoeffding inequality [21] to lower-bound the confidence that the true selectivity values fall within the cube-shaped near-optimality region calculated in the second step. If this confidence value exceeds δ then optimization ends and the current query plan is returned. If the confidence is not yet sufficiently high, our optimizer finally chooses the number of samples to take in the next iteration and the best distribution of samples over different query predicates.
Finding a cube-shaped near-optimality region (step two) is challenging: on the one hand, we want to find a region of maximal extent as this maximizes the confidence and allows to terminate optimization early. On the other hand, finding the region of maximal extent might require a prohibitive number of calls to the standard optimizer as we need to find the optimal cost value for many points in the selectivity space. We will present and experimentally compare different variants of the region finding algorithm. All of them only execute a bounded number of standard optimizer invocations per iteration (in our experiments we use an optimizer budget of ten calls per iteration for instance). Our most sophisticated algorithm variant iteratively extends the near-optimality cube and calculates the gradient for the cost function at the cube bounds after each extension: this allows to estimate the extent of the near-optimality cube before extension and therefore to carefully select the selectivity vectors for which the standard optimizer is invoked.
We also propose different policies for selecting the next samples to take. We present variants that offer formal guarantees on achieving near-optimal tradeoffs between the number of optimzier calls and the number of samples taken and we present policies that significantly reduce the number of required iterations by estimating the required number of samples based on an analysis of the plan cost function and the selectivity sample distribution obtained so far. We evaluate those more sophisticated policies in comparison with uniform sampling.
In summary, our original scientific contributions in this paper are the following: • We introduce and formalize the probably approximately optimal query optimization problem (PAO).
• We present several variations of a generic and nonintrusive algorithm for PAO that is applicable in many scenarios.
• We formally analyze different algorithm variants and we experimentally compare them for different query sizes, join graph shapes, and number of predicates.
The remainder of this paper is organized as follows. We discuss related work in Section 2. In Section 3, we introduce our formal problem model and discuss the fundamental assumptions it is based on. We present several variants of a PAO algorithm in Section 4 and formally analyze it in Section 5. We finally compare different algorithm variants in an experimental evaluation in Section 6.

RELATED WORK
We propose an iterative algorithm that interleaves query optimization and predicate sampling. This connects our approach to the magic number sensitivity analysis (MNSA) algorithm by Chaudhuri and Narasayya [9] but there are several important differences. First, MNSA assumes that information is collected by collecting data statistics from which predicate selectivity is inferred while we assume that predicates are sampled directly. Our probabilistic model presented in Section 4.3 is specific to our scenario. Second, MNSA adopts a binary approach to uncertainty: either the selectivity of a predicate is completely unknown (within the range ε and 1−ε) or it is completely known. When sampling predicates, it can take prohibitively many samples to determine the true selectivity with negligible error. We therefore work with confidence bounds on predicate selectivities and our optimization goal (finding plans that are near-optimal with high probability) is different than the one of Chaudhuri and Narasayya (finding plans that are guaranteed nearoptimal). Many of the problems that our algorithm presented in Section 4 solves (e.g., finding regions within the selectivity space where a given plan is near-optimal with few optimizer calls) are specific to our scenario. Finally, MNSA assumes that one data statistic is created in each iteration. We assume that many samples can be taken per iteration (this reduces the required number of optimizer calls and allows to exploit parallelism during sampling) and deciding how many samples to take per iteration and how to partition them over different predicates is another important sub-problem that we consider.
MNSA was later generalized [7] but the proposed approach assumes that MNSA or a similar technique is applied in a first step to generate statistics on base tables and that hard bounds on predicate selectivity values (not probabilistic confidence bounds as in our case) can be inferred from those statistics. We cannot infer selectivity from data statistics in our case (e.g., no generic data statistic would allow to automatically estimate the selectivity of natural language predicates evaluated by crowd workers on a picture database [25]) and MNSA is not suitable for our scenario as discussed before.
Joklekar et al. [17] propose a sampling approach to estimate the selectivity of user-defined predicates (mainly for the case of one predicate while an extension is shortly discussed). Their focus is on how to avoid evaluation of userdefined predicates by approximation while our focus is on interleaved join ordering and sampling, assuming that each predicate is evaluated on each relevant tuple. Joklekar et al. and others [8] obtain confidence bounds on predicate selectivities but it is a-priori unclear how selectivity bounds translate into plan cost bounds and this is why we interleave optimization and sampling in an iterative approach.
Karanasos et al. [23] sample the selectivity of user-defined predicates by executing pilot runs; an initial query plan is generated based on those pilot runs, the plan might be reoptimized once new information becomes available during execution. Unlike our approach, the work by Karanasos et al. does not provide any probabilistic guarantees on the near-optimality of the generated plans. Another difference is that we carefully choose the number of samples to take for each predicate in each iteration based on how sensitive the optimal join order is to the selectivity of different predicates. Re-optimization [2,4,19,23] at run time is a generic technique to cope with uncertain predicate selectivities. However, an initial query plan with which to start execution must still be chosen and a sub-optimal initial plan can lead to huge overheads. Our approach can be used to generate that initial plan.
Seppi et al. [29] propose a Bayesian approach to query optimization: based on prior probability distributions on the selectivity of predicates, the optimizer decides how much sampling is appropriate to choose between alternative operator implementations. Their approach relates to ours since the amount of samples to take is decided based on statistical models. The algorithm by Seppi et al. is however noniterative and whether an appropriate number of samples is chosen depends entirely on the accuracy of the prior knowledge on predicate selectivities. In our scenario, nothing is known about the predicates a-priori. Further, the approach by Seppi et al. does not consider join order but only operator implementations. Finally, the probabilistic model by Seppi et al. directly operates on probability distributions over cost values; standard cost formulas need to be simplified in order to make the approach applicable [29] and the used formulas must be tailored to specific scenarios. Our approach operates on probability distributions over selectivity values but not on probability distributions over cost values. Our approach is therefore applicable for arbitrary cost models (as long as cost values grow monotonically in the selectivity) and does not require adaption to specific scenarios.
Optimization techniques that avoid selectivity estimation altogether [14,13] incur huge overheads during planning and execution (optimization time is in the order of hours and execution time exceeds the optimal execution time in practice by factor 10 for some queries). The work by Goel et al. [18,20] is related to ours as it addresses the question of how to efficiently invest resources to reduce uncertainty at optimization time. However, the problem model used by Goel et al. does not match our scenario. It assumes that probability distributions are initially known for all parameters and that a subset of parameters is selected for which exact values can be obtained by probing. In our case, no probability distributions on the selectivity of predicates are initially known and exact selectivity values cannot be obtained except by evaluating each predicate on each tuple which is equivalent to query execution in our scenario. The algorithms proposed by Goel et al. only treat the case of simple cost functions that are defined as maximum or minimum over all parameters. Query optimization is cited as application scenario by Goel et al. but with the focus of selecting an optimal subset of queries to execute; the presented approaches cannot find optimal join orders.
Parametric query optimization [10,16,22] assumes that predicate selectivities are unknown at optimization time. The goal is usually to generate a set of plans containing an optimal or near-optimal plan for each possible combination of selectivity values. In contrast, our goal is to find one plan that is near-optimal with a certain probability. Further, unlike in parametric query optimization, we assume that the optimizer can reduce uncertainty at optimization time by sampling predicates. This assumption motivates the design of our iterative algorithm. The goal in robust query optimization [1,3,11] is to produce plans that are robust towards selectivity estimation errors, using a given probability distribution over selectivity values to judge robustness. It does not consider the possibility of taking samples during optimization which is central to our scenario.

MODEL AND ASSUMPTIONS
Unlike a typical publication on query optimization, we will neither introduce a specific query model nor a specific cost model nor a specific plan model in this section. The reason is that, strictly speaking, we do not propose an algorithm for query optimization but rather a meta-algorithm that uses an existing optimizer and sampling infrastructure to generate plans that satisfy certain probabilistic quality guarantees.
A query describes data that needs to be generated. To make the following definitions more concrete, we can for instance imagine that queries are described in some extension of SQL that allows user-defined functions as predicates or predicates that are formulated in natural language and evaluated by crowd workers [26]. We make however only very generic assumptions on the query language and our algorithm is in principle applicable to other structured query languages as well.
Queries are associated with predicates that need to be evaluated on data subsets. In case of SQL, predicates are defined on single base tables or between table subsets. Predicates are associated with a selectivity value between 0 and 1 which is the fraction of input data items that will satisfy the predicate. Estimating the selectivity of predicates is crucial as the choice of the optimal processing plan depends on them. Queries might contain predicates whose selectivity is known or can be reasoned about. We assume however that queries contain additionally predicates whose selectivity is unknown and can only be estimated by evaluating predicates on randomly selected data samples which we call short to sample the predicate. For a given query q, we refer to the set of predicates with unknown selectivity values by pred(q).
Many alternative query plans are in general available to process a given query. The execution cost of a query plan depends on the predicate selectivities. We use in the following the term selectivity vector to refer to a vector containing a selectivity value for each predicate of a given query. Given two selectivity vectors s1 and s2, we write s1 s2 to indicate that the selectivity value assigned by s1 is smaller or equal to the selectivity assigned by s2 for each predicate.
As selectivity values are unknown in our scenario, we must think of plan cost as a function that maps selectivity vectors to cost values. We assume that a cost model is available that maps plans and selectivity vectors to cost estimates. We represent this cost model by the function call Cost(p, s) for a plan p and a selectivity vector s in our pseudo-code. If Cost(p, s)= c then we say that plan p has cost c at or for selectivity vector s.
The algorithm that we present in this paper is based on a generic assumption on the cost model: if s1 s2 then the cost of any fixed plan is not higher for s1 than for s2, i.e. cost functions are monotone in the selectivity. This assumption is common in query optimization [5,9]. It is intuitive as higher predicate selectivity may lead to larger intermediate results and processing larger results is usually more costly. In the context of SQL, it holds for single-block SQL queries while it may not always hold for multi-block queries [9].
For a fixed selectivity vector, we say that a plan is optimal if it has minimal cost among all possible query plans. We assume that a standard query optimizer is available that determines for a given selectivity value an optimal plan. We represent that optimizer by the function call BestPlan(q, s) for query q and selectivity vector s in our pseudo-code. We say that a plan is α-optimal (we also say near-optimal as long as it is clear from the context to which value of α we refer) for a specific selectivity vector if its cost at s is not higher than the optimal cost at s by more than factor α.
We call the set of all possible selectivity vectors for a given query the selectivity space and a subset of vectors a region. A plan is α-optimal in a selectivity space region if it is αoptimal for each contained vector. We are particularly interested in (hyper)-cube shaped regions of the selectivity space that are defined by two selectivity vectors, a lower bound vector lb and an upper bound vector ub such that lb ub. The corresponding cube contains all vectors s such that lb s and s ub. In two dimensions, a cube corresponds for instance to a rectangle with the upper bound as right-upper corner and the lower bound as lower-left corner. We will associate plans with selectivity cubes in which they are near-optimal and that we call near-optimality cubes in that context.
Our goal in this paper is to solve the probably approximately optimal query optimization problem: a problem instance is defined by a query q, an approximation factor α, and a confidence threshold δ. The goal is to find a query plan for q that is α-optimal with confidence δ.

ALGORITHM
We present a first algorithm for PAO in this section. We discuss the main function in Section 4.1. In Section 4.2, we present different algorithm variants for the sub-function that identifies a cube-shaped region in the selectivity space for which a given plan has near-optimal cost. In Section 4.3, we discuss how to calculate the confidence that the true selectivity values fall within a given region in the selectivity 1: // Returns query plan for query q that is α-optimal 2: // with probability at least δ. 3: function PAOQ(q, α, δ) 4: // Initialize selectivity sample set S 5: S ←InitSamples(q) 6: // Until sufficiently confident to have α-optimal plan 7: repeat 8: // Optimize for current selectivity means 9: p ←BestPlan(q, S.means) 10: // Find selectivity cube where p is α-optimal 11: noc ←NearOptCube(q, p, α, S) 12: // Calculate confidence that selectivity in cube 13: conf ←Confidence(noc, S) 14: // Determine which samples to take next 15: nrSamples ←ChooseSamples(q, p, α, δ, S, noc) 16: // Take corresponding number of samples 17: S ← S∪TakeSamples(nrSamples) 18: until conf ≥ δ 19: return p 20: end function Algorithm 1: Interleaved sampling and optimization for probably approximately optimal query optimization.
space. In Section 4.4, we present different policies for choosing the number of samples to take for each predicate in the next iteration.

Main Algorithm
Algorithm 1 is the main function of our algorithm. The input is a PAO problem specification, consisting of a query q to optimize, an approximation factor α defining what constitutes a near-optimal cost value, and a confidence threshold δ. The output is a query plan for query q whose execution cost is not higher than optimal by more than factor α with probability at least δ. We motivated this problem statement in Section 1.
Our algorithm obtains very rudimentary initial selectivity estimates by evaluating each predicate on a small data sample (function InitSamples in the pseudo-code). We can either use a small fixed number of evaluations per predicate. Or we can use an initialization method similar to the one used by Karanasos et al. [23] and evaluate each predicate on its input tuples until we find the first tuple for which the predicate is satisfied. The initial sample will in general be insufficient to generate a plan that is near-optimal with sufficiently high confidence. The bigger part of samples is taken during the iterations of the main loop (lines 7 to 18) that we explain next.
We execute the following steps in each iteration of the main loop. First, a query plan is created that is optimal when assuming that the selectivity value for each predicate is the arithmetic mean of all samples collected for that predicate so far. Function BestPlan represents the invocation of a standard query optimizer that generates the best query plan for given selectivity estimates. Having an optimal plan for the selectivity means, we calculate a cube-shaped region in the selectivity space for which that plan has nearoptimal cost (within factor α of the optimum). We use function NearOptCube to calculate the near-optimality region. We discuss alternative implementations of that function in the next subsection. Having a selectivity region for which the current plan is near-optimal, we calculate the confidence that the true selectivity values fall into that region using function Confidence. We discuss the formulas that we use to calculate confidence in Section 4.3.
We finally choose the number of samples to take for each predicate using function ChooseSamples. The output of ChooseSamples is a vector specifying for each query predicate the number of samples to take next. We discuss different policies for choosing the number of samples in Section 4.4. The choice of the number of samples might be based for instance on the sensitivity of the cost function of the current plan towards the selectivity in different predicates. Having chosen the number of samples to take, we draw those samples using a sampling component represented by function TakeSamples. Iterations end once the required confidence value is reached.
Note that we can of course check whether the current confidence value is sufficient immediately after calculating confidence (line 13) and avoid taking more samples in that case. The pseudo-code shown in Algorithm 1 is simplified while the implementation used for our experiments performs that check. Also note that we can introduce additional termination conditions, restricting for instance the number of iterations, standard optimizer calls, or the number of samples taken. In particular, we can introduce an additional termination condition that bounds the total number of samples based on the estimated execution cost of the current plan, thereby avoiding disproportional sampling overhead.

Finding Near-Optimality Cubes
We describe different possibilities to implement function NearOptCube in Algorithm 1. Our goal is to find a selectivity cube in which a given plan has near-optimal cost. If possible, we want to find the cube with maximal extent among all cubes satisfying the aforementioned constraints. The reason is that we terminate the iterations of the main loop once our confidence that the true selectivity values lie within that cube reaches threshold δ. Obtaining a bigger cube allows earlier termination. Verifying whether a plan has near-optimal cost in a given cube requires however calls to the standard optimizer. The number of optimizer calls required for finding a cube of maximal extent might be prohibitive. Hence we devise algorithms that try to find the largest possible near-optimality cube with a given budget of optimizer calls. Note also that the true region in which a given plan is optimal is in general not a cube. We still choose to approximate it by a cube since cubes are easy to represent and since the statistical model presented in Section 4.3 is specific to cube-shaped regions.
We present two alternative algorithms for implementing function NearOptCube in this subsection (Algorithm 2 and 3). Algorithm 2 is a rather naive approach at finding a large near-optimality cube with a given number of optimizer calls. The input is a plan p for query q together with the approximation factor α and the set of selectivity samples S collected so far. The output is a near-optimality cube, noc, in which p is α-optimal.
creasing the bounds by an additive constant. The problem is however that adding a constant value to small selectivity values has large impact on the plan cost function while adding the same constant to a large selectivity values has little impact; it is therefore preferable to extend the bounds by a constant ratio.
After each cube extension, function IsNearOptCube verifies whether plan p is still α-optimal within the extended cube. This check requires one optimizer call and compares the optimal cost at the lower cube bound, lbCost, with the cost of the current plan p at the upper cube bound, ubCost. The near-optimality check is based on the following theorem, similar results were exploited by Chaudhuri and Narasayya [9] and by Bizarro et al. [5].
Proof. The optimal cost lbCost at the lower cube bound is at the same time a lower bound for the optimal cost in the entire cube as cost functions are assumed to be monotone in the selectivity. The cost of plan p at the upper cube bound is an upper bound on the cost of plan p in the entire cube for the same reason. Hence if ubCost ≤ α · lbCost then the cost of plan p is not higher than the optimum by more than factor α in the entire cube.
We can improve Algorithm 2 in several ways. First, instead of terminating once a cube extension does not yield a near-optimality cube anymore, we can backtrack and try to extend the last cube with a smaller step size. Second, our goal should be to extend the upper and the lower bound by a similar amount since we will calculate confidence (that the true selectivity values falls into the cube) based on the minimal distance between selectivity mean and the cube bounds. Having extended either the upper or the lower bound by a big margin is useless if the other bound remains close to the mean as the confidence remains low. Third, extending the selectivity of each predicate by the same ratio is too inflexible as it neglects that the sensitivity of the cost function towards changes of the selectivity values might differ across different plans. Algorithm 3 integrates all of the aforementioned improvements.
The selectivity cube is initialized in the same way as before. Before starting the extensions, we calculate however a target cost value for the upper (variable ubtc) and the lower cube bound (variable lbtc). The semantics of those cost values is that our algorithm tries to extend the cube in a way such that the cost of plan p at the upper cube bound corresponds approximately to ubtc and the optimal cost value at the lower cube bound corresponds approximately to lbtc. This choice connects to the second improvement: if plan cost functions were linear then making the cost of p at the upper bound equal to ubts and the optimal cost at the lower cube bound equal to lbtc would result in the near-optimality cube maximizing the minimal distance between selectivity mean and cube bounds. Note that we do not require plan cost functions to be linear and our algorithm works for nonlinear cost functions as well. It will however tend to be more efficient the more closely the cost function resembles a linear function around the current selectivity means.
Each iteration of the outer loop in function NearOptCube performs two steps. First, a recommended step size is calculated separately for the lower and the upper cube bound. Second, starting with the recommended step sizes we try to extend the current cube, cutting the step sizes by two until either the extended cube is small enough such that p is α-optimal within it or the optimizer budget runs out.
Function StepSize calculates the recommended step size to start the extensions with. We choose the step size based on the sensitivity of the cost function which realizes the third of the three improvements we discussed before. We have calculated target cost values that we want to reach at the upper and lower cube bounds. The step size is based on the margin, i.e. on by how much the current cost value differs from the target. We also need to take into account how quickly the cost function changes in the selectivity parameters around the bounds. The expression Ones·∇Cost(fix p, X)| X=sel calculates the slope of the cost function of plan p in the direction of the unit vector (function Ones) and the recommended step size takes the slope and the margin into account. In order to choose the step size at the lower cube bounds we examine the cost function of the plan that is optimal at the lower cube bounds (as we are interested in the slope of the optimal cost function). At the upper bound we examine the cost function of plan p that is optimal for the current selectivity means. 1: // Calculates recommended step size when trying to 2: // reach target cost tcost for plan p by making 3: // a step from selectivity vector sel. 4: function StepSize(p, sel, tcost) 5: // Calculate plan cost at selectivity vector 6: cost ←Cost(p, sel) 7: // Calculate margin to target cost 8: margin ← tcost − cost 9: // Calculate cost slope at selectivity vector 10: slope ←Ones·∇Cost(fix p, X)| X=sel 11: // Return recommended step size 12: return margin/slope 13: end function 14: // Extends cube noc by adding ubStep and lbStep 15: // to upper and lower cube bounds respectively. 16 until extended ∨ nrOpt ≥ b 57: until nrOpt ≥ b 58: return noc 59: end function Algorithm 3: Finding near-optimality cubes with slopebased choice of extension step size.
The calculated step size is only used as a recommendation and if extension is not possible with that step size then it is reduced. This is the purpose of the inner loop in function NearOptCube and implements the first of the improvements proposed before. We compare both algorithms for finding near-optimality cubes in our experimental evaluation in Section 6.

Calculating Confidence
Given a cube-shaped selectivity region in which a plan p is α-optimal, function Confidence calculates the confidence that the true selectivity values fall within that region. This is at the same time our confidence that plan p has near-optimal cost. We do not provide pseudo-code for function Confidence but we describe the mathematical formulas that it applies.
We denote by t the vector of true selectivity values in the following. We are interested in the probability that the true selectivity values fall within cube noc, i.e. we are interested in Pr(t ∈ noc) = Pr(noc.lb t noc.ub). We want to lower-bound that probability and the resulting bound is our confidence value.
We denote by the subscript notation the selectivity value for one specific predicate (e.g., tr denotes the true selectivity of predicate r). The probability that the true selectivity is within the cube equals the probability that the selectivity of each predicate is within its corresponding cube bounds, i.e. Pr(t ∈ noc) = r∈pred(q) Pr(noc.lbr ≤ tr ≤ noc.ubr), denoting by pred(q) the set of predicates in query q that need to be sampled. Note that we assume independence between the deviations of the current mean from the true selectivity between different predicates.
The true selectivity for a predicate is at the same time the expected value for the mean of the corresponding selectivity samples. We can therefore upper-bound the probability that the true selectivity of one specific predicate falls outside the cube bounds by the probability that an expected value deviates from the current mean. The Hoeffding inequality [21] provides us with corresponding bounds while making no assumptions on the probability distribution of the sampling distribution.
More precisely, denote by ar the arithmetic average value of the selectivity samples collected for one specific predicate r. Denote by tr its true selectivity and denote by lr the number of samples (i.e., the number of tuples on which the predicate was evaluated) collected for that predicate so far. The cube bounds for r are given by noc.lbr and noc.ubr and due to how we generate cubes, the mean value must be contained between the cube bounds: noc.lbr ≤ ar ≤ noc.ubr. Denote by dr = min(ar − noc.lbr, noc.ubr − ar) the minimal distance between the current mean and the cube bounds. Then the probability that the true selectivity of r falls outside the cube bounds, Pr(tr < noc.lbr ∨ tr > noc.ubr), is bounded by the probability that the distance between current mean and true selectivity exceeds dr.
As the true selectivity is the expected value of the mean, we can bound that probability using the Hoeffding inequality. Assuming that noc.lbr > 0 and noc.ubr < 1, we have to apply the two-sided version of the Hoeffding inequality (otherwise we can use the one-sided version which provides a tighter bound). In that case, we obtain 2·exp(−2·d 2 r ·lr) as upper bound for Pr(tr < noc.lbr ∨tr > noc.ubr). This means our confidence for the true selectivity being within the current cube bounds is lower bounded by 1 − 2 · exp(−2 · d 2 r · lr). The confidence that the true selectivity falls within cube noc is therefore given by r∈pred(q) max(1 − 2 · exp(−2 · d 2 r · lr), 0).

Sampling Schemes
Function ChooseSamples, used in Algorithm 1, determines for each predicate how many samples to take when the next batch of samples is collected. We discuss different sampling strategies in this subsection. We do not provide pseudo-code but an implementation based on our descriptions is straight-forward.
We can split the problem of choosing the number of samples per predicate in the current iteration into two subproblems. First, we must decide how many samples to take in total. Second, we must decide how to distribute those samples over the different predicates.
There are multiple metrics according to which we can compare sampling strategies. We can compare them in terms of the number of main loop iterations that they lead to but we can also compare them in terms of how many samples are taken in total or how often the standard optimizer is invoked. The number of optimizer invocations and the number of iterations are certainly correlated as we execute a bounded number of optimizer invocations in each iteration. The number of samples and the number of iterations are however not necessarily correlated and the right answer to the first question (how many samples to take in total) depends on the tradeoff that we want to realize between them. If we take a relatively large number of samples in each iteration then the number of required iterations and optimizer invocations will be reduced while the total number of samples might be higher than optimal. If we take a small number of samples then the number of iterations required to reach the PAO guarantees tends to increase. Note that the time required for sampling might depend more on the number of iterations than on the total number of samples if we can exploit parallelism on the sampling infrastructure (e.g., if data is processed on a big cluster or on a crowdsourcing platform such as Amazon Mechanical Turk 1 ).
The optimal answer to the second question (how to distribute the total number of samples over different query predicates) depends on the samples collected so far, on the plan that is optimal for the current selectivity means, and on the extent of its near-optimality cube. Assume for instance that the current plan is near-optimal for all possible selectivity values of one predicate. Then sampling this predicate is not interesting. Or assume that the cost function of the current plan is equally sensitive to two predicates but that we already have very tight confidence bounds for one of them while the selectivity of the second one is rather uncertain. Clearly it is more interesting to sample the second predicate in this situation. The sampling strategies that we discuss in the following all consider a subset of the issues we raised before and we compare different sampling strategies formally in Section 5 and experimentally in Section 6.
The simplest sampling strategy that we consider is uniform sampling. We take the same number of samples in each iteration and samples are distributed uniformly over the predicates.
A slightly more sophisticated sampling strategy is exponential sampling. The total number of samples is still distributed uniformly over the different predicates. However, the total number of samples to take is chosen according to the following principle. We take the same number of samples as uniform sampling in the first iteration. Then, denoting by h the total number of samples taken in all prior iterations, we take ρ · h samples in the current iteration. Factor ρ > 0 is a user-defined constant that allows to trade between the number of iterations and the number of samples taken. We will see during formal analysis and during our experiments that this seemingly simple sampling strategy often achieves a good tradeoff between the number of iterations, samples, and optimizer invocations.
All sampling strategies discussed so far distribute the total number of samples uniformly over all predicates. This is not the case for our last sampling strategy that we call adaptive sampling. Adaptive sampling adapts the number of samples to take for each predicate to the plan that is optimal for the current selectivity means and to its nearoptimality cube. We make the conservative assumption that new samples will not change the current selectivity means significantly, that the plan that is optimal for the current selectivity means will remain optimal in the next iteration, and that its near-optimality cube remains the same as well. Under those assumptions, we can calculate the number of additional samples required for each predicate that are required in order to reach the required confidence value δ. More precisely, denote by m the number of query predicates. Then the confidence value δ is reached once the confidence for each single predicate that its true selectivity value falls within the cube bounds reaches value δ 1/m . For each predicate, we must therefore set 1 − 2 · exp(−2 · d 2 r · lr) ≥ δ 1/m which allows to calculate the required number of samples.

FORMAL ANALYSIS
We analyze the maximal number of samples that the algorithm presented in the last section has to take in order to satisfy the probabilistic PAO guarantees.
The number of required samples depends on properties of the cost function. We assume in the following that multiplying the selectivity estimate for each of m predicates by factor f might multiply the cost of a plan by up to factor f m . We call this assumption the maximal sensitivity assumption (MSA) in the following, as cost functions are assumed to be equally sensitive to a change in any of the predicate selectivity values. We require MSA only to hold locally in a small area around the true selectivity values. Note that our algorithm works even if MSA is not satisfied; the assumption is uniquely required for our formal analysis.
MSA is a pessimistic assumption as having cost functions that are very sensitive to the selectivity estimates means that plans tend to be near-optimal within smaller regions of the selectivity space. Then the number of samples required to shrink the selectivity confidence bounds until they are contained within those near-optimality regions increases.
We first establish a minimal extent for near-optimality regions under the MSA. Then we calculate the number of samples required to shrink the confidence bounds. We denote in the following by m the number of query predicates. Lemma 1. If a plan is optimal at selectivity vector s then it is α-optimal in the cube bounded by sα −1/(2m) and sα 1/(2m) .
Proof. Plan p is in the following the plan that is optimal for selectivity vector s and c the cost of p at s. We first calculate a lower cost bound for the entire cube. A different plan than p might be optimal at the lower cube bounds sα −1/(2m) . Let pL denote the plan that is optimal for the lower cube bounds. The cost of pL for s is lower-bounded by c as c is the optimal cost at s. Multiplying the selectivity by factor α −1/(2m) can reduce the plan cost at most by factor α −1/2 due to the MSA. Therefore c · α −1/2 is a lower bound for the cost at the lower cube bound which lower-bounds at the same time the cost in the entire cube as we assume that cost functions are monotone in the selectivity values. The cost of p at the upper cube bound is an upper bound on the cost of p within the entire cube. The cost of p at sα 1/(2m) is upper-bounded by c · α 1/2 due to MSA. It is cα 1/2 ≤ cα −1/2 · α and the sufficient condition for having a nearoptimality cube (see Theorem 1 in Section 4.2) satisfied.
The confidence that the true selectivity falls within the cube bounds is for each predicate calculated based on the minimal distance between mean and cube bounds.
Lemma 2. The plan p that is optimal for the current selectivity means a is α-optimal in a cube around a with minimal distance a·(1−α −1/(2m) ) between a and the cube bounds.
Proof. Lemma 1 specifies upper and lower bounds of a cube in which p is α-optimal. The distance between a and the upper cube bound is a · (α 1/(2m) − 1) and the distance between a and the lower cube bound is a·(1−α −1/(2m) ). It is The minimal distance between a and the cube bounds is therefore the distance to the lower cube bounds.
We calculate the number of samples required to achieve confidence δ. The following analysis is pessimistic since we assume that plans are only near-optimal within the minimal near-optimality cube described in Lemma 1.
We assume that the cube extension algorithm is able to establish that the best plan for the current selectivity means is near-optimal at least within its minimal cube. This seems particularly likely for the slope-based extension scheme (Algorithm 3): the target cost bounds it aims to achieve for the upper and lower cube bounds correspond precisely to the ones of the minimal selectivity cube if the plan cost functions are as sensitive to selectivity changes as maximally allowed by the MSA and if the plan that is optimal for the selectivity means is also optimal at the lower cube bounds.
We simplify the following analysis by assuming that all predicates have the same selectivity mean a and therefore the same minimal distance d = a · (1 − α −1/(2m) ) between a and the cube bounds, while the generalization is straight forward. We also assume that the selectivity means a remain fixed over the iterations (alternatively, we can say that a denotes the selectivity means in the final iteration without affecting the following analysis). We currently assume a uniform distribution of samples over predicates.
Proof. The algorithm terminates once the confidence that the true selectivity values lie within the minimal nearoptimality cube reaches the threshold δ. This is the case once the confidence for each of the m predicates that its selectivity lies within the corresponding cube bounds reaches value m √ δ. Hence we require 1−2 exp(−2·d 2 ·l) ≥ m √ δ where l denotes the number of samples taken for that predicate. This leads to the condition exp(−2 · d 2 · l) ≤ (1 − m √ δ)/2 and −2 · d 2 · l ≤ log((1 − m √ δ)/2) and finally l ≥ log(2/(1 − m √ δ))/(2 · d 2 ). This formula specifies the required samples per predicate so we need to multiply by m to obtain the total number of required samples.
The required number of samples grows in the confidence threshold δ. It grows once the minimal distance between mean and cube bounds decreases; the minimal distance increases in α. As expected, requiring a higher confidence or using a stricter definition of near-optimality increases the required number of samples. Our formula for the number of samples also grows for lower selectivity values (as the minimal distance is monotone in the selectivity). Note however that our formula establishes an upper bound on the required number of samples under pessimistic assumptions. The actual number of required samples might be less sensitive to low selectivity values and we will show in our experiments that our algorithms perform well for relatively low selectivity values of few per mille in average. More importantly, low selectivity values mean low execution cost as the size of intermediate results becomes very small. We discussed the natural extension of our algorithm to terminate iterations once the estimated execution cost becomes too low (e.g., with confidence δ). Low selectivity values lead to low execution cost and would trigger early termination.
Also, there are specialized sampling methods that reduce sampling effort for predicates with low selectivity in specific domains. A counting based selectivity estimation method has for instance been proposed recently in the context of crowd database systems [24] that can reduce the required number of samples significantly, in particular for image recognition tasks.
Note that the number of samples does not at all depend on the size of the processed data! The number of required samples will therefore become more and more negligible the bigger the data set on which the query operates is.
We denote by u(α, δ, m) the upper bound on the required number of samples in the following. We finally study how the required number of samples translates into the actual number of samples taken, the number of iterations and the number of optimizer calls, for the different sampling strategies described in Section 4.4. We denote the multiplicative factor used by the exponential sampling strategy by ρ in the following. Proof. The total number of samples taken in each iteration equals the total number of samples taken so far multiplied by factor ρ. Hence the total number of samples increases by factor ρ + 1 after each iteration. The number of iterations is in O(log ρ+1 u(α, δ, m)) = O(log ρ u(α, δ, m)) and the number of optimizer calls is proportional. The number of iterations determines the number of samples which is (ρ + 1) ⌈log ρ+1 u(α,δ,m)⌉ ∈ O(ρ · u(α, δ, m)).
Exponential sampling reduces the asymptotic number of iterations and optimizer invocations significantly while it increases the number of samples by factor ρ. Choosing a higher value for ρ decreases the number of iterations and optimizer calls while it may increase the number of samples. Note that our analysis is based on the simplifying assumptions that a plan is only near-optimal within its minimal cube and that this cube is reliably discovered in each iteration. We will see in our experiments that the number of samples increases sometimes by more than factor ρ when switching from uniform to exponential sampling.
Adaptive sampling tries to calculate the required number of samples for each predicate based on the current selectivity estimates. This results in one iteration and a constant number of optimizer calls under the (in practice overly optimistic) assumption that the selectivity values do not change significantly enough to invalidate the forecast. Based on that property, we can however certainly expect a relatively low number of iterations and optimizer calls. In addition, the adaptive sampling scheme is the only one that does not distribute samples uniformly over different predicates. This means that the number of samples taken by the adaptive scheme is for instance guaranteed to be lower than the one taken by all other schemes at least by factor m in cases where selectivity matters only for one out of the m predicates (e.g., if all predicates but one are defined on small tables whose processing cost is negligible).

EXPERIMENTAL EVALUATION
We experimentally compare the different variants of the PAO algorithm described in Section 4 against each other. Section 6.1 describes the experimental setup. Section 6.2 presents the result of a comparison between different algorithms for finding selectivity space regions and Section 6.3 compares different predicate sampling strategies.

Experimental Setup
We compare algorithms in terms of the number of iterations they perform, the number of standard optimizer calls that they require, and in terms of the total number of samples that they collect for solving a given PAO instance. We compare algorithms on randomly generated queries and use the query generation method described by Steinbrunn et al. [30] (which was reused in other publications [6]). This generation method results in predicates with rather moderate selectivity (median selectivity over 100 generated queries joining 10 tables was around 3 per mille). After generating a query, we randomly pick a given number of predicates for which no selectivity values are provided to the optimizer. Our optimization algorithm can only approximate those selectivity values via sampling while selectivity estimates for the other predicates are known. We simulate evaluating predicates on data samples by flipping a coin for each tuple with a success probability that corresponds to the selectivity that was determined at query generation.
We generate queries with different numbers of joined tables and predicates. We performed one series of experiments on chain queries and one on star queries; the results show     the same tendencies and hence we report only the results for chain queries in the following. We compare algorithms for different approximation factors α and for different confidence thresholds δ. Selectivity estimates are initialized by sampling each predicate until the first tuple satisfying the predicate is encountered [23]. The number of samples taken per iteration by uniform sampling corresponds to 0.25 percent of the average number of predicate evaluations required for the first join. Exponential sampling takes the same number of samples in the first iteration. The maximal number of samples per iteration is restricted to 100 times that number for adaptive sampling. The number of standard optimizer invocations per iteration is restricted to 10. All algorithms were implemented in Java 1.7 and executed on an iMac with i5-3470S 2.90GHz CPU and 16 GB of RAM.

Comparing Cube Extension Algorithms
We compare the two algorithms presented in Section 4.2 for finding near-optimality cubes using a given number of standard optimizer invocations. "Slope" designates the extension scheme that exploits the slope of the plan cost function (see Algorithm 3) while "FR(ǫ)" designates the naive version (see Algorithm 2) that uses a fixed extension ratio defined by ǫ. We experimented with different values for ǫ in preliminary experiments and report only results for the two best configurations in the following plots. All algorithms use the uniform sampling scheme in this subsection. Figures 1 to 4 compare the algorithms in terms of how many iterations are necessary (which is at the same time proportional to the number of required samples as we use uniform sampling) and how many standard optimizer calls are necessary. We compare algorithms for two query sizes and four different combinations of values for α and δ and for four different numbers of parameters with unknown selectivity values. Each data point represents the arithmetic average over 25 test cases. We interrupted iterations of the fixed ratio algorithm once the number of iterations exceeds the iterations required by Slope by more than one order of magnitude (this happened in average for one out of 25 test cases). Hence the reported numbers for FR correspond to lower bounds on the real values.
Slope wins according to the number of iterations (and samples) and according to the number of optimizer calls. The difference is significant (note the logarithmic axis) and shows the impact of the improvements that were discussed in Section 4.2. FR is less efficient than Slope at approximating the near-optimality region of a given plan. This means that FR requires tighter confidence bounds for the selectivity values before it can terminate and this results in the increased number of iterations.
The number of required iterations as well as the required number of optimizer calls increase for all algorithms once δ increases or α decreases as both corresponds to stronger probabilistic guarantees. Increasing the number of predicates increases the number of iterations and optimizer calls as well. Note however that Slope can easily optimize 10table queries with eight predicates having unknown selectivity values, using only around 1100 standard optimizer calls even for the strongest probabilistic guarantees. Algorithms for parametric query optimization [11,5], where the goal is to find optimal plans for all possible selectivity value combinations, are for instance typically evaluated on lower numbers of selectivity parameters and incur higher computational overhead. Our approach for cube extension is tailored to our scenario as we only approximate the nearoptimality region of one single plan instead of finding near-   optimal plans for each region in the selectivity space. In the next subsection, we will see how the number of iterations can be reduced by more sophisticated sampling schemes.

Comparing Sampling Schemes
Figures 5 to 10 compare the sampling strategies that were presented in Section 4.4 over different query sizes, predicate counts, and PAO guarantees. "Uni" denotes the uniform sampling strategy, "Exp(ρ)" is the exponential sampling strategy with multiplicative factor ρ and "Adapt" designates the adaptive sampling strategy in those plots.
The comparison shows that different sampling strategies are optimal according to different cost metrics. Uniform sampling requires the lowest number of samples but the highest number of iterations and optimizer calls. It is preferable if sampling is rather expensive compared with optimization overhead and if the sampling infrastructure offers a low degree of parallelism. The other sampling schemes reduce Iterations  α =5, δ =0.5 α =5, δ =0.9  the number of required iterations however often by one order of magnitude or more for elevated number of predicates and high confidence requirements. The exponential sampling strategy realizes different tradeoffs between the number of optimizer calls and iterations and the number of samples depending on the choice for ρ. Choosing a higher value for ρ decreases the number of iterations and optimizer calls while it usually increases the number of samples. Comparing with the other sampling strategies, the exponential sampling strategies offer often a good tradeoff between number of optimizer invocations and samples.
The adaptive sampling scheme usually requires the lowest number of iterations and optimizer calls among all sampling strategies. This shows that estimating how many samples are required to meet the confidence requirements is helpful even if the theoretical optimum of only one iteration is not reached. On the other hand, adaptive sampling has often the highest number of samples for moderate number of query predicates. Once the number of samples becomes elevated (at least four predicates for high confidence requirements and at least six for low confidence requirements), adaptive sampling starts requiring less samples than the exponential sampling strategies. This shows the benefit of distributing samples non-uniformly over predicates.
Altogether we recommend uniform sampling to minimize the number of samples, the exponential strategies to achieve a good and tunable tradeoff between the number of iterations and samples for queries with moderate number of predicates, and the adaptive sampling strategy to minimize the number of iterations and for queries with many predicates.

CONCLUSION
We introduced probably approximately optimal query optimization and a first corresponding algorithm.