WorksComputer Science
Order-Dependent Complexity of Enumeration and Direct Access for Conjunctive Queries on Dynamic Data
Abstract
This note studies the fine-grained complexity of ranked enumeration and ranked direct access of conjunctive query answers over a dynamic database. We focus on lexicographically sorted answers, defined by an ordering on the query's free variables. From the literature, when no particular order is required, we know that when the query is self-join-free and q-hierarchical, we can build a data structure for enumeration (resp. direct access) with linear (resp. linearithmic) preprocessing and constant (resp. logarithmic) update time in the size of the database. For enumeration (resp. direct access), this data structure yields an algorithm that enumerates all query answers (resp. that returns the -th query answer) with constant delay (resp. logarithmic access time). Our goal is to extend these results to ranked enumeration and ranked direct access, with the ranking function defined by a lexicographic order. We define a property on the order over the variables of , which we call being hierarchy-consistent. Given a user-specified query and hierarchy-consistent order, we first propose, for ranked enumeration under updates, an algorithm with linear preprocessing and logarithmic update time that enumerates all the query answers according to the user-specified order with constant delay. Moreover, for ranked direct access under updates, we propose an algorithm with linear preprocessing and logarithmic update time that returns the -th tuple according to the user-specified order with logarithmic access time. In contrast, given a user-specified query and non-hierarchy-consistent order, we show a lower bound for ranked direct access under updates.
Introduction
For a conjunctive query (or CQ) , given a database , materializing the answers can be costly: the number of answers can be up to . Producing or holding the full answer set at once is therefore often impractical. Instead, it is more natural to find approaches that allow us to access partial information about the query result without materializing it. In particular, we may want to access the th answer to on , or to enumerate the answers one by one without repetitions.
These tasks are typically formalized in database theory in two ways. The first one is enumeration: rather than producing all tuples at once, an enumeration algorithm outputs them one by one, with a guarantee of bounded delay between two consecutive tuples. The second one is direct access: this is the task of outputting, on an input , the th answer for over .
It is well-known from the work of Bagan, Durand, and Grandjean [2] that we can enumerate the answers to self-join-free CQs with linear preprocessing and constant delay in data complexity provided that these queries satisfy the so-called acyclic free-convex condition; this restriction is (conditionally) necessary within the class of self-join-free CQs, and a characterization of tractable CQs with self-joins is still open [5, 13]. What is more, Brault-Baron [1] has shown that direct access was possible with linearithmic preprocessing and logarithmic access time in data complexity. We also note the line of work on factorized databases [12], which represent query results compactly using a structure derived from the query, and enumerate the answers with constant delay when no particular order is required or when the user-defined order is compatible with this structure.
The literature has then extended these questions along two independent axes: supporting efficient updates to the underlying database (and updating the structure used for enumeration or direct access without recomputing it from scratch); and providing results in a specific order (e.g., in some lexicographic order specified by the user). The goal of the present work is to bridge these two directions. We present existing results on each of them in turn.
First, in the dynamic setting, [3] show that q-hierarchical conjunctive queries can be enumerated with constant delay and constant update time after linear preprocessing, and that for self-join-free CQs this classification is (conditionally) optimal. An analogous result for direct access is shown in [10]. Further works have investigated which guarantees could be provided for CQs beyond this class; this is not the focus of the present work, but we review these directions nevertheless. Dynamic Yannakakis [9] maintains constant-delay enumeration for all free-connex queries (i.e., more generally than q-hierarchical conjunctive queries) with a more costly update time (namely, it is linear in the input database). This was then refined in [15] with an update cost that depends on the update sequence, which becomes constant for insertion-only sequences.
We now review the different types of data structures that are used to show efficient enumeration and direct access guarantees on dynamic data; we will re-use some of them in the present work. [10] use a circuit-like structure that stores the assignments of each variable of the q-tree in AVL trees. Circuits could also describe such a structure, but they are not so convenient to maintain in the dynamic setting, given that the domain of assignments is not fixed and updates add or remove edges of the circuit. In contrast, [11] use view-trees, whose interface and implementation can be defined separately; we re-use the structure of view-trees in the present paper. In all these works, the answers are produced in an arbitrary order determined by the data structure, and ranked enumeration or ranked direct access under updates is not handled.
Second, for ranked enumeration or direct access in the static setting, where answers are output according to a ranking function, an important earlier work is [14]. For acyclic free-connex queries, they build a data structure in linearithmic data complexity, after which the answers to on can be enumerated in any lexicographic order with logarithmic delay between consecutive tuples. Ranked direct access, which returns the -th answer in the user-defined order, has been studied by [6], who also build a data structure in quasilinear time, with logarithmic access time. Circuits, which are compact representations of the answers, have also been used for direct access, for conjunctive queries with negation [4], and for enumeration beyond conjunctive queries through knowledge compilation [8]. None of these works considers updates to the database.
Hence, our contribution sits at the intersection of these two lines of work: which orders can we support in the dynamic setting? Since we consider the dynamic setting, we build on the dichotomy established by [10] (and [3] for enumeration), which identifies q-hierarchical queries as the class for which linear preprocessing, constant delay or logarithmic access, and constant updates are possible under updates. We therefore restrict our attention to this class of queries, and we maintain the answers with view-trees [11].
Contributions. This note presents the preliminary results that we have obtained for ranked enumeration and ranked direct access to conjunctive queries under updates. We focus on q-hierarchical queries, because otherwise intractability holds (for self-join-free queries, and conditionally) even in the absence of ranking functions. We study lexicographic orders defined by an order on the query’s free variables, and introduce a condition on such orders, called being hierarchy-consistent. We show that, for hierarchy-consistent orders, we can perform ranked enumeration and ranked direct access following a lexicographic ordering, with the same favorable guarantees as in the case without a specific ordering, up to a logarithmic update time instead of constant update time for enumeration. By contrast, we show that, for non-hierarchy-consistent orders, a conditional intractability result rules out efficient ranked direct access.
Paper structure. The paper is organized as follows: § 2 introduces the notations and concepts necessary to understand the paper. In § 3, we define the data structure used for ranked enumeration and ranked direct access in the dynamic setting, and give an algorithm to build it. In § 4, we define a property on the user-defined order, called hierarchy-consistency, and give two algorithms for ranked enumeration and ranked direct access in the dynamic setting when the order satisfies this property, with bounds in combined complexity. In § 5, we show that when the order does not satisfy this property, ranked direct access in the dynamic setting is conditionally not achievable, in data complexity, with polynomial preprocessing, sublinear access time, and sublinear update time.
Preliminaries
Basic Notions
Database. A schema is a finite set of relation symbols , with every having a fixed arity . A database instance is of the form where, for each , is a finite set of tuples , and is a set of constant values called the domain, on which we assume a total order. The size of the database instance is the number of tuples that it stores, i.e. it is .
We fix an infinite set of variables denoted by . For a tuple indexed by variables and a set , we write for the restriction of to the variables of , preserving the order of . Conversely, for a set of variables ordered by a total order that agrees with on , a tuple indexed by is an extension of if , preserving the order .
Updates. We consider two types of updates: insertions and deletions. An insertion is defined as a command . Applied to a database instance , this results in a database instance with . Similarly, a deletion is defined as a command . Applied to a database instance , this results in a database instance with . Note that does not change if the inserted tuple already exists in ; likewise, is unchanged when we delete a tuple that does not contain.
Queries. A conjunctive query (CQ) is defined as where , and each is an atom of the form . We mainly focus on CQs that are self-join-free, written , if no relation symbol occurs more than once in the query. For an atom or a query , we write and for the set of variables occurring in and , respectively. For a variable , we denote by the finite set of atoms in in which appears, i.e. . Moreover, the set of variables is partitioned into free variables and quantified variables . In addition, for a CQ and a set of variables , the projection of over , denoted by , is the CQ .
Finally, given a query and a database instance , the set of answers to over , i.e. the set of tuples resulting from applying to , is defined as . Note that , i.e., the answers of the projection of a query over are the restrictions of its answers to .
Problem Definition
The model of computation is the RAM model with uniform cost measure and word size . Under this assumption, additions and multiplications on integers of size , as well as lookup table and array accesses, take constant time.
Given this computation model, we consider two query evaluation problems throughout this paper: ranked enumeration under updates and ranked direct access under updates. To define these problems, we first need to introduce the notion of orders.
Order on answers. Let be a database instance, a query, and a total order over . Let be the free variables of listed according to this order. We define an order on answers on induced by , called the lexicographic order, as follows. For two answers , we say that if there exists such that we have for all and we have , where denotes the total order on the domain values.
Enumeration. The enumeration problem consists of two phases. Given a database instance and a query , the preprocessing phase builds a data structure . The enumeration phase outputs all tuples in without repetition, and finally emits a special value end-of-enumeration to signal the end of enumeration. We consider several extensions of this problem. First, if a total order over free is given before the preprocessing, the task of ranked enumeration asks us to output all tuples in in the lexicographic order on induced by . Second, if an update command (insert or delete ) is issued after the preprocessing, letting be the original database instance and be the database instance obtained by applying the update command to , then the task of enumeration under updates asks us to modify the data structure to a data structure that represents the updated database instance, i.e., which allows us to enumerate the tuples of . If the update command occurs during the enumeration phase, we interrupt enumeration and restart it from scratch on the new database . Combining both variants, the ranked enumeration under updates is the version where we are given the database instance , the query , and a total order over free, and where we must be able to enumerate tuples of in the order and also support updates to in the sense above.
Direct access. The direct access problem consists of two phases. Given a database instance and a query , the preprocessing phase builds a data structure . Then, in the access phase, we receive user queries consisting of an index , and we must return the tuple of in an implementation-defined order, or a special value out-of-bounds if . We again extend this problem to support lexicographic orders and to support updates. First, if a total order over free is given with and before the preprocessing, then the task of ranked direct access asks us to support access queries where the implementation-defined order on tuples must coincide with the lexicographic order on , induced by . Second, the task of ranked direct access under updates asks us to support update commands (insert or delete ) after the preprocessing, and again we must modify the data structure so that it represents the updated database instance and can be used to access tuples in the new set of answers . Combining both variants, the ranked direct access under updates problem is the version where we are given the database instance , the query , and a total order over free, and where we must be able to support queries on the lexicographic order induced by and also to support updates to in the sense above.
Complexity measures. For the query evaluation problems defined earlier, we measure complexity using combined complexity, i.e., as a function of the database size and of the size of the query , i.e., the length of when written as a word over the alphabet .
For each query evaluation problem, we consider the preprocessing time , the update time when supporting updates, and either the delay time for enumeration problems or the access time for direct access problems. For ranked enumeration (with or without updates), the delay time is the maximum time between any two consecutive outputs, before the first output, and between the last output and the special value end-of-enumeration. For ranked direct access (with or without updates), the access time is the time to return the tuple or the special value out-of-bounds. Let , , and be numeric functions. For ranked enumeration under updates (resp., ranked direct access under updates), an algorithm is said to be in time (resp., ), if for every database instance and query , we have the preprocessing time , the delay time (resp., the access time ) and the update time .
Note that the task of (ranked) direct access (under updates) generalizes the task of (ranked) enumeration (under updates). Indeed, for every numeric function , if we have an algorithm in time for (ranked) direct access under updates, then we have an algorithm in time for (ranked) enumeration under updates, and likewise without updates. We can perform (ranked) enumeration (under updates) with (ranked) direct access (under updates) as follows. Once the data structure is built, in , from a q-hierarchical SJFCQ , a database instance and whenever it exists, an order over free, we can return the answer for in several queries in the access phase, with delay. Finally, whenever an update occurs, we modify in , and restart queries in the access phase with .
Initial Observations
The complexity of enumeration and direct access of conjunctive queries on dynamic data was studied in [10], which introduced the class of q-hierarchical CQs.
We now recall the definition of this class and the lower bounds and upper bounds that it yields.
Definition 2.1. A CQ is q-hierarchical if for any two variables the following is satisfied:
(i) or or , and
(ii) if and , then .
It was shown in [10] that, for SJFCQs that are not q-hierarchical, some complexity lower bounds apply to enumeration under updates and direct access under updates, even in the non-ranked versions. These lower bounds are conditional to the -conjecture, which we introduce below.
OMv-conjecture. The Online Matrix-vector multiplication () problem is defined as follows. We are given an Boolean matrix , on which we allow arbitrary polynomial preprocessing. Then, a sequence of Boolean vectors of dimension is given online, one vector at a time. For each , we must output before receiving . The complexity measure is the total running time required to output all vectors . The conjecture states that no algorithm solves the problem in time for any .
We now recall the lower bounds on enumeration under updates and direct access under updates from [10].
Theorem 2.2 ([10 Theorem 3.13]). Fix a number and a self-join-free conjunctive query . If is not q-hierarchical, there is no algorithm with polynomial preprocessing time and update time that enumerates with delay in data complexity, unless the -conjecture fails.
Theorem 2.3 ([10 Theorem 3.16]). Fix a number and a self-join-free conjunctive query . If is not q-hierarchical, there is no algorithm with polynomial preprocessing time and update time that solves the direct access problem in time delay in data complexity, unless the -conjecture fails.
In contrast, it was also shown in [10] that, for q-hierarchical SJFCQs, there are algorithms for efficient enumeration under updates and efficient direct access under updates.
Theorem 2.4 ([3 Theorem 3.2]). There is an algorithm that receives a q-hierarchical SJFCQ and a database instance , and computes within preprocessing time a data structure that can be updated in time and allows the following:
enumerate with delay
Theorem 2.5 ([10 Theorem 3.9]). There is an algorithm that receives a q-hierarchical SJFCQ and a database instance and computes within preprocessing time a data structure that can be updated in time and allows the following:
output the query result size in time and,
enumerate the tuples in in lexicographic order according to the implementation order with delay and,
upon input of an arbitrary , takes time to immediately output the answer that the enumeration procedure of (2) would have output
In the next section, we will review proof techniques used to show these results because we will extend them for our own results. In light of the lower bounds of Theorem 2.2 and Theorem 2.3, we will focus on q-hierarchical SJFCQs throughout the paper. Our goal is to investigate how this complexity picture is affected for ranked enumeration under updates and ranked direct access under updates as defined previously.
Data Structures: Q-Trees, View-Trees, and Grounded-Trees
In this section, we define the data structure built in the preprocessing phase from the q-hierarchical SJFCQ and the database instance given as input. This data structure is from [11], and we call it a grounded-tree. As we study combined complexity, we present an algorithm from [10] to build a so-called -tree for a given q-hierarchical SJFCQ. We then define from this the notions of view-trees and grounded-trees, and we show how to build grounded-trees from a q-hierarchical SJFCQ and a database instance.
From Q-trees to Grounded-Trees
In this part, we define the data structure built in the preprocessing by giving the definitions of q-trees, view-trees, and grounded-trees.
We recall the definition of q-trees from [10].
Definition 3.1. Let be a -hierarchical . A -tree for is a directed tree with rooted in where
for all atoms in , the set forms a directed path starting in at , and
is a connected subset in .
A is q-hierarchical if and only if it admits a q-tree [10 Lemma 4.2]. For each , we denote by the ancestors of in , including . We can then define the following as in [11]:
Definition 3.2. Let be a q-hierarchical and one of its q-trees. Let be the extended q-tree of which is defined as follows: we have and . The view-tree is the rooted tree with vertex set and edge set , where each node or is associated to a view or which is a relational algebra expression defined as follows:
(i) for each leaf labeled by an atom then the view associated to is ,
(ii) for each internal vertex with children , then the view associated to is ,
(iii) for each edge , then the view associated to is .
Given a database instance , we now enrich the view-tree with the data of , which gives a grounded-tree . Each vertex (resp. ) in the view-tree is associated to a table (resp. ) such that (resp. ) is the evaluation of (resp. ). In particular, for every leaf labeled by an atom , then . The other views of are then computed bottom-up from the leaf views, following their definitions. Note that some projection views are defined on the same set of variables as their child. For instance, in Figure 1, the view is identical to , therefore . We do not try to remove these redundant views, because doing so would complicate the definitions and would not improve the asymptotic complexities of our algorithms.

Figure 1. Representation of an extended q-tree, grounded-tree, and query result for (see Example 3.3)
In addition, whenever you have a vertex with associated view and a child , then letting and be the tables respectively associated to them, for each tuple , we denote by the set of tuples such that . Note that for a variable , the vertex has several children in . For each tuple and each of , we have that has exactly one tuple. A projection view has one child view . For each tuple , there exists with several tuples which are extensions of over .
Example 3.3. Figure 1 represents an extended -tree and its grounded-tree for the query , where by definition of a grounded-tree , and , and for the database instance given by the relations , and . Note that the -tree is the subtree of Figure 1 in black. In addition, for the tuple shown in gray, the tuples are in a lighter gray.
Data Structure Implementation
In this section, we first describe the implementation of the data structure defined previously, inspired by [10], using lookup tables and -trees. We then present the algorithms that we use when performing the preprocessing on this data structure.
-trees. Given a set of tuples and a ranking function that maps each tuple of to a value of an ordered domain, a -tree for is a balanced search tree that stores in sorted order according to . More precisely, all leaves are at the same depth, and each leaf stores a bounded number of consecutive tuples in the order , whereas the internal nodes only store keys and pointers that guide the search for a specific tuple. Moreover, the leaves are chained in a linked list following . It is well known that -trees admit algorithms allowing us to insert a tuple, delete a tuple, retrieve a tuple, and retrieve the minimum tuple for each in time , since the height of the tree is logarithmic in the number of stored tuples. What is more, given a pointer to a tuple stored in the tree, accessing the tuple that follows it in the order induced by takes time , by moving to the next tuple of the same leaf or, at the end of a leaf, by following the pointer to the next leaf.
Grounded-tree implementation. Let be a vertex of the view-tree , let be the view associated to , let be the table associated to , and let be the parent of . To navigate each table efficiently, we group all tuples by their restriction to . We implement each table as a lookup table whose keys are all tuples and whose value for each is . For each , given a ranking function that maps each to a value of an ordered domain, we store in a -tree sorted according to .
Therefore, given a table and a key , accessing the -tree takes constant time since is a lookup table. Besides, given and a ranking function , since each bucket is a -tree for , inserting or deleting a tuple in while keeping it sorted according to takes time . Further, given a pointer to a tuple in , retrieving its successor takes constant time.
Preprocessing
We next show how to build the grounded-tree from a SJFCQ . To do so, we first build a q-tree , extend it to a view-tree , and, given a database instance , we enrich into a grounded-tree with all the database facts from . We now recall the combined complexity of building a q-tree, given a q-hierarchical CQ.
Lemma 3.4 ([10 Lemma 4.2]). A CQ is q-hierarchical if and only if has a q-tree. Moreover, there is an algorithm that decides whether an input CQ is q-hierarchical, and if so, outputs a q-tree.
Given a database instance , a q-hierarchical SJFCQ , and a q-tree for , we now show how to build the grounded-tree for in the preprocessing phase. We can show a preprocessing upper bound in using the fact that we can sort in linear time in the RAM model [7].
Proposition 3.5. There exists an algorithm that, given a q-hierarchical SJFCQ , one of its q-trees , and a database instance , builds the grounded-tree , in which each bucket is sorted by a given ranking function , in time .
Note that if one wants to avoid linear-time sorting algorithms, the preprocessing can instead be performed in time , using any linearithmic sorting algorithm.
We can now prove Proposition 3.5:
Proof of Proposition 3.5. For a given q-hierarchical SJFCQ and one of its q-trees , we first compute the extended q-tree in time by adding, for each atom , a leaf as a child of the farthest variable from in such that . Afterwards, we compute the view-tree by traversing while adding for each vertex , and for each edge , in time . We then initialize all tables to the empty table by creating an empty table for each vertex of the view-tree, giving us the empty grounded tree . Given a database instance , we then set for every relation of . We then propagate the tuples bottom-up as follows. Let be a vertex, let be a child of , let and be the tables associated to and , and let be the view associated to . For each table , we evaluate the view and group the tuples of into buckets according to their restriction to : each tuple is placed in the bucket with key . If is a projection view, then contains the keys of the buckets of its only child . If is a join view, we fix one child and scan the keys of the buckets of ; for each of them, we check in constant time per other child that exists, and is added to only if all these buckets exist. Every table has at most tuples: the leaves are relations of , a projection view has at most as many tuples as its child, and a join view has at most as many tuples as any of its children. Hence, a projection vertex is processed in time , and a join vertex in time per child. Since the sum of the number of children over all vertices is the number of edges of the view-tree, which is , all views are evaluated in time . Using the fact that we can sort in linear time in the RAM model [7], we then sort each according to its ranking function and build, for each bucket, a -tree from the sorted list in linear time. Each table contains at most tuples in total, separated into several buckets, so this takes per table, and overall. Hence, there exists an algorithm that builds a grounded tree , in which each bucket is sorted according to , in time . ∎
For the query evaluation problems we tackle throughout this paper, we can therefore build a grounded-tree for a q-hierarchical SJFCQ in time , i.e., linear preprocessing in data complexity, by building one of its q-trees with Lemma 3.4 and applying Proposition 3.5.
Hierarchy-Consistent Orders
We now define a notion of order on the free variables of a q-hierarchical CQ.
Definition 4.1. Let be a q-hierarchical CQ. Then, induces a partial order on where if . A total order on is hierarchy-consistent for if it is a linear extension of this partial order.
Given a q-hierarchical CQ , and a hierarchy-consistent order over , we now want to perform ranked enumeration and ranked direct access. To do so, we need to build a data structure adapted to . We therefore want a q-tree, used to build the data structure, in which can be obtained easily, for instance by a topological sort. It follows from the proof of [10 Lemma 4.2] that several q-trees may represent a q-hierarchical CQ . However, every hierarchy-consistent order cannot be obtained by a topological sort of any q-tree. Indeed, for each pair , whenever and , the ancestor-descendant relation between these variables may vary: in one q-tree is an ancestor of , while in another is an ancestor of , as shown in Figure 2. Thus, a hierarchy-consistent order where cannot be obtained by a topological sort of the first q-tree. We make this clearer with an example:

Figure 2. Q-trees for
Example 4.2. Let be a q-hierarchical CQ. The linear extension such that is a topological sort of but cannot be obtained by a topological sort of in Figure 2.
Hence, in the preprocessing phase, we build a compatible q-tree for the order , defined as follows.
Definition 4.3. Let be a q-hierarchical CQ, one of its q-trees, and a hierarchy-consistent order on . We say is compatible with if is the restriction to of a topological order of .
We now show that such a q-tree exists, strengthening [10 Lemma 4.2] (which we had restated previously as Lemma 3.4).
Claim 4.4. Given a q-hierarchical CQ and a hierarchy-consistent order on its free variables, we can build a q-tree compatible with in .
Proof. To build such a q-tree compatible with a hierarchy-consistent order , we adapt the algorithm from [10 Lemma 4.2] using to break ties when, for , we have . This algorithm builds a q-tree of such that each path of is a path of the form where . In this algorithm, when for two variables , we have , we choose arbitrarily which variable will be the ancestor of the other. To get a q-tree in which is a topological sort, we break this tie following the order , i.e., when for two variables , we have , if then is an ancestor of , otherwise is an ancestor of . ∎
Before showing how to achieve ranked enumeration (resp. direct access) under updates for a hierarchy-consistent given order, we first show that, when the database changes due to the insertion or deletion of a tuple, we can update the grounded-tree in time . Recall that in a grounded-tree, each bucket is sorted by a ranking function . When the order used to build the grounded-tree is hierarchy-consistent, we define these ranking functions as the lexicographic ranking functions induced by .
Let be a non-root vertex of the grounded tree , let be the view associated to , let be the table associated to , and let be the parent of with associated view . Let be a q-hierarchical CQ, let be the hierarchy-consistent order over , let be a q-tree for compatible with , and let be a database instance. We extend arbitrarily to a topological order of over all of . For each bucket of the grounded tree , the lexicographic ranking function , induced by over , is simply the identity: for every , where the domain is the set of tuples over ordered lexicographically according to . In other words, for all , we have if and only if , where is the lexicographic order induced by .
Lemma 4.5. Let be a q-hierarchical SJFCQ, a hierarchy-consistent order over , a q-tree for compatible with , and a database instance. Let be the grounded-tree for and whose buckets are sorted using the lexicographic ranking functions defined by . Given an update command , let be the database instance obtained by applying to . There exists an algorithm that, given and given an update , computes in time the grounded tree , whose buckets are sorted using the lexicographic ranking functions .
Proof. Let be a grounded-tree and for each bucket of , let its tuples be sorted lexicographically. Let be an update of a tuple , and let be the database instance obtained by applying to . We build by inserting or removing database facts from as follows. We distinguish two cases depending on the kind of update.
When the update is an insertion, for the table with the set of free variables of the view , we insert in the bucket . Accessing takes constant time, and inserting in while keeping sorted lexicographically takes logarithmic time. Then, we propagate the insertion upwards in on the path from to as follows. When we insert a tuple in a bucket , with , we insert in , where . If already exists in , the propagation stops. Otherwise, when we insert a tuple in with , we insert in where , if and only if exists in all for each child of in the extended q-tree. It is equivalent to checking if exists, which takes constant time. In the worst case, we propagate an update until , which takes .
When the update is a deletion, for the table with the set of free variables of the view , we remove from the bucket . Accessing takes constant time, and removing from while keeping sorted lexicographically takes logarithmic time. Let be the deleted tuple. Then, we propagate the deletion upwards in on the path from to as follows. When we delete a tuple from a bucket , with , if becomes empty, we remove from , which takes time . If there still exists a tuple in the propagation stops. When we delete a tuple in , with , we remove from , where if it exists; otherwise, the propagation stops. In the worst case, we propagate a deletion until , which takes . Hence, an update takes .
We can now show how to achieve ranked enumeration under updates when the order in the input is hierarchy-consistent.
Ranked Enumeration Under Updates
In this part, given a q-hierarchical SJFCQ and a database instance , we propose an enumeration algorithm in delay in combined complexity, i.e., linear preprocessing, logarithmic update, and constant delay times in data complexity, when the order on the free variables of is hierarchy-consistent.
Theorem 4.6. There exists an algorithm that, given a q-hierarchical SJFCQ , a hierarchy-consistent total order on its free variables, a q-tree for compatible with and a database instance , enumerates in the lexicographic order defined by with preprocessing, update time, and delay.
Proof. Given a q-hierarchical SJFCQ , a database instance , and a hierarchy-consistent order over , we apply the preprocessing procedure to build the grounded-tree using Proposition 3.5. Then, when updates arrive, we handle them using Lemma 4.5. Hence, all that remains is to explain how to perform ranked enumeration, which we now present.
Let be the free variables of listed according to . Algorithm 1 is written as nested loops over for readability. In this algorithm, we traverse the grounded-tree from top to bottom to build each answer of the enumeration. For a non-root variable , the bucket to traverse is determined by a tuple chosen in table . Recall that there is only one tuple per bucket such that and , therefore . In the following, we traverse the grounded-tree by only visiting the join vertices . Moreover, since is hierarchy-consistent, is chosen before the tuple in , so is always known when we need it.
To enumerate the first answer of , we start from the root table . Let be the tuple of , if it exists. Otherwise, we return end-of-enumeration. Then, for every child of in , we save the bucket in a variable . Afterward, for in this order, we take the first tuple of the bucket saved in variable , determined by the tuple chosen for its parent. At last, we output the answer built from the first tuple of the corresponding bucket, and pause the algorithm with the command yield.
To enumerate the next answer of , we look for the rightmost variable whose current tuple is not the last of its bucket. If there is none, the enumeration ends. Otherwise, we replace the tuple of with the following tuple of its bucket, and for each with , in the order , we take the first tuple of the bucket determined by the tuple chosen for its parent. Only the variables after change between two consecutive answers.
Getting for a tuple and a variable takes constant time, since each vertex is a look-up table where keys are all assignments of the free variables of where is the parent of in the extended q-tree . Therefore, the delay to output a new tuple is at most the traversal of every join vertex of the grounded-tree until the last free variable of the order —i.e., the delay of Algorithm 1 is . Combining the delay bound with the bounds from Proposition 3.5 and Lemma 4.5, there exists an algorithm that enumerates with preprocessing, update time and delay.
Ranked Direct Access Under Updates
In this part, we first explain how to enrich the grounded-tree representing a database instance for a given hierarchy-consistent order on the free variables of a q-hierarchical SJFCQ . Then, we adapt an algorithm from [6] into an algorithm in that, given , a hierarchy-consistent order , a q-tree for compatible with , and a database instance, answers ranked direct access under updates in access time.
Enriched grounded-tree for ranked direct access under updates Let be a grounded-tree as described in § 3 for a given q-hierarchical SJFCQ , a given database instance , and a hierarchy-consistent order . Recall that each bucket is sorted according to a lexicographic ranking function . This ranking function is the identity: for every , meaning is sorted lexicographically. We call the subtree of containing all vertices ${V^{\bowtie}_{x_i}\cup V^{\Pi}_{(x_i,x_j)}\mid
Algorithm 1: Ranked enumeration according to a hierarchy-consistent order $\prec$
Input: $V_D(T_Q)$: a grounded-tree for $Q(D)$
Output: answers of $Q(D)$ in the lexicographic order induced by $\prec$
begin
Let $x_1 < \cdots < x_l$ be the variables of $\operatorname{free}(Q)$ in the desired hierarchy-consistent order $\prec$
for $t_0 \in Z^{\mathord{\vee}_{v_{\mathrm{root}}}}$ do
$t_{Q(D)} \leftarrow ()$
for $c$ child of $v_{\mathrm{root}}$ in $\operatorname{free}(Q)$ do
bucket$[Z_c^{\mathord{\vee}}] \leftarrow$ bucket$_{t_0}(Z_c^{\mathord{\vee}})$
for $t_1 \in$ bucket$[Z_{x_1}^{\mathord{\vee}}]$ do
$t_{Q(D)}[x_1] \leftarrow t_1[x_1]$
for $c$ child of $x_1$ in $\operatorname{free}(Q)$ do
bucket$[Z_c^{\mathord{\vee}}] \leftarrow$ bucket$_{t_1}(Z_c^{\mathord{\vee}})$
$\vdots$
for $t_l \in$ bucket$[Z_{x_l}^{\mathord{\vee}}]$ do
$t_{Q(D)}[x_l] \leftarrow t_l[x_l]$
yield $t_{Q(D)}$
yield end-of-enumeration
Let . As is q-hierarchical, is a connected subtree. Let be a vertex, let be the parent of , let and be the tables associated to and , and let be the view associated to . For each tuple in a bucket with , we add weights . Note that these weights are different from the ranking function , as they are not used to sort the tuples in a bucket. The weight represents the number of different answers in that extend over the variables in the views of the subtree rooted in in . We compute these weights bottom-up while building the grounded-tree , and when an update occurs. For leaves in the subtree , we initiate all weights to . When we propagate the tuple upward in , as explained in Proposition 3.5 and Lemma 4.5, we compute the weight for each tuple propagated as follows. Note that in this case, the propagation of an update never stops before achieving , since we need to update weights for the parent of each vertex. This still takes .
Let such that is a child of in , and the tuple restricted over the variable of the view associated to the parent of vertex .
Therefore, if , its weight is the sum of all the weights of the tuples that are extensions of in the table . Otherwise, recall that bucket contains exactly one tuple. Then, if , its weight is the product, over all children of , of the weight of the unique tuple bucket.
In addition to , we compute a prefix sum for each bucket and each tuple . This sum is the cumulative sum of the weights of the tuples that strictly precede , i.e., .
Example 4.7. We now consider the query , where by definition of a view-tree we have and .
We enrich the view-tree with the weights and the prefix sum . The result is represented in Figure 3.

Figure 3. Representation of q-tree, view-tree and query result for (see Example 4.7)
Note that, for , the value is the total number of tuples in .
To understand how weights and prefix sums are maintained after updates, we describe the implementation of a bucket . We use the -tree described in § 3, where the tuples of are sorted lexicographically by their values. Each leaf stores a bounded number of consecutive tuples of , together with their weights . Each internal node stores the sum of the values stored in its children, that is, the total weight of the tuples stored in the leaves below . Inserting or removing a tuple in , or changing its weight, takes time , including the update of the sums stored in the internal nodes along the path from the leaf to the root. Propagating an update of the database to the views of the grounded-tree thus takes time .
The prefix sums are not stored: the value of a tuple is computed when needed. To compute it, we traverse the -tree from the root to the leaf of . At each internal node, we add the values stored in the children located to the left of the child we visit, and, in the leaf, we add the weights of the tuples preceding . Since the height is and each node has a bounded number of children, computing the prefix sum of a tuple takes time . Then, in a bucket , given a value , we can find in time the last tuple sorted according to such that .
Theorem 4.8. There exists an algorithm that, given a q-hierarchical SJFCQ , a hierarchy-consistent total order on its free variables, a q-tree for compatible with and a database instance , answers ranked direct access for in the lexicographic order defined by with preprocessing, update time and access time.
Proof. This access algorithm is Algorithm 1: Lexicographic Direct-Access from [6], whose data complexity is . This algorithm consists of getting the right bucket for each view of the view-tree, which is done in constant time as views are implemented as look-up tables, and then picking the correct tuple in those buckets, which takes , as proven in [6], Lemma 3.8. In combined complexity, as we look for this tuple for each variable of the order , and we traverse each view of the view-tree, the complexity of the access algorithm is . Hence, combining this complexity with Proposition 3.5 and Lemma 4.5, there exists an algorithm that answers ranked direct access for and with preprocessing, update time and access time. ∎
Non-Hierarchy-Consistent Orders
In this section, given a q-hierarchical SJFCQ , an order over and a database instance , our goal is to develop an algorithm with preprocessing and update time for ranked direct access under updates (resp. ranked enumeration under updates) according to with access time (resp. delay), i.e., linear preprocessing, logarithmic update, and logarithmic access (resp. delay) times in data complexity.
We will show that for ranked direct access under updates, such an algorithm conditionally does not exist.
Ranked Direct Access Under Updates
Our contribution on ranked direct access under updates relies on Theorem 2.2. The following lower bound states that efficient ranked direct access under updates cannot be achieved for a q-hierarchical SJFCQ when the given order on its free variables is allowed to be non-hierarchy-consistent. Our lower bound holds in data complexity and applies to any q-hierarchical SJFCQ and non-hierarchy-consistent order on its free variables.
Theorem 5.1. Fix a number , , a q-hierarchical SJFCQ and an order over . If is not hierarchy-consistent for , then there is no algorithm with polynomial preprocessing time and update time that, given a database instance , answers ranked direct access for and with access time, assuming the OMv-conjecture in data complexity.
To prove this lower bound for ranked direct access, we first show that an efficient ranked direct access algorithm for any non-hierarchy-consistent order on any q-hierarchical SJFCQ would imply efficient enumeration under updates, for some non-q-hierarchical SJFCQ , which according to Theorem 2.2 is not possible (even without an order) when assuming the OMv-conjecture.
Lemma 5.2. Suppose there is a q-hierarchical SJFCQ and a non-hierarchy-consistent order over , such that there exists an algorithm in where is polynomial, and are for some , and that, given a database instance , answers ranked direct access for and . Then there exists a non-q-hierarchical SJFCQ , such that there is an algorithm in that, given a database instance , enumerates with .
To show this lower bound, we reduce the enumeration of a projection of to ranked direct access under updates on , where is a q-hierarchical SJFCQ with a non-hierarchy-consistent order over its variables. We choose the free variables of so that is not q-hierarchical. Given a database instance , we enumerate the tuples of as follows: we use ranked direct access on to retrieve the answer, and then find by binary search the smallest such that the answer has a different projection on the free variables of . Each such answer is a new tuple of , which we output. Let us give the formal proof of Lemma 5.2:
Proof of Lemma 5.2. Let be a database instance, be a q-hierarchical SJFCQ, and be a non-hierarchy-consistent order over . Let be the free variables of listed in the order of . Then there exist two variables such that and . Let be the projection of over . Thus, , with . Therefore, is not q-hierarchical, since condition (ii) of Definition 2.1 is violated on variables and . Indeed, and , whereas .
Suppose there exists an algorithm with update time that supports ranked direct access in time for and the order . Let denote the number of answers of on . Since is a q-hierarchical SJFCQ, we can retrieve in data complexity, after a preprocessing, and maintain this value in update time [10 Theorem 3.9]. Let be the lexicographic order of induced by , and let be the answers of listed according to , where each can be computed in time using the algorithm assumed in the lemma statement.
We now show how to design an efficient enumeration algorithm for . We start by checking if (or if accessing returns out-of-bounds), in which case we output end-of-enumeration and terminate; otherwise we compute and output the tuple ; this step takes time . Then, at each step, we compute the next tuple in the enumeration from the last outputted one. To do this, suppose we have already output the th tuple for some . We define the next relevant rank as the next index of an answer of whose projection to changes relative to . If no such exists, the enumeration algorithm will output the special value end-of-enumeration and terminate. Otherwise, the th tuple of the enumeration will be set as .
All that remains is to explain how we can efficiently find or detect termination. We first access and check whether ; if so, the enumeration is finished, and the algorithm will output end-of-enumeration. Otherwise, we show how to compute the next relevant rank . To do this, we compute by performing a binary search on the interval . At each step, let and access the tuple . If , then , and we set . Otherwise, we have , and we set . We repeat this process, updating the interval at each step, until we have . At that point, one can easily see that we must have . This binary search requires access calls, and therefore runs in time .
Thus, we obtain an algorithm with update time and delay for enumerating . Moreover, we have , therefore, we obtain delay in data complexity.
Having proved our auxiliary result (Lemma 5.2), we are ready to conclude the proof of Theorem 5.1:
Proof of Theorem 5.1. Let be a database instance, be a q-hierarchical SJFCQ, and be a non-hierarchy-consistent order over for . Suppose there is such that there exists an algorithm with polynomial preprocessing and update time that answers ranked direct access in access time for and .
By Lemma 5.2, there exists a non-q-hierarchical SJFCQ such that there is an algorithm that enumerates with delay and sublinear update time. Let . By Theorem 2.2, as is not q-hierarchical, no algorithm enumerates with delay and update time, unless the OMv-conjecture fails. Since this contradicts the OMv-conjecture.
Therefore, unless the OMv-conjecture fails, there is no dynamic algorithm with polynomial preprocessing time and update time that answers ranked direct access in access time for a q-hierarchical SJFCQ and a non-hierarchy-consistent order on its free variables.
Ranked Enumeration Under Updates
We have shown a dichotomy for ranked direct access under updates: assuming the OMv-conjecture, there exists an algorithm for ranked direct access with complexity if and only if the order in input is hierarchy-consistent. For ranked enumeration, we do not have a matching lower bound. We believe in fact that the situation with ranked enumeration under updates is different, and that it remains efficient even when the order is not hierarchy-consistent, at the cost of an additional logarithmic factor in the delay in data complexity.
Our approach is to adapt the algorithm anyK-part of [14], which relies on the Lawler-Murty procedure, to the grounded tree . The main difference with the static setting lies in the optimal weights of the tuples. In the static case, they are computed once, before the enumeration, by a dynamic programming pass over the tree. In our setting, we compute them during the preprocessing phase, as part of the construction of the grounded tree, and we maintain them when the database is updated. We leave the details and the analysis of this approach to a future version of the present work.
References
- [1]Johann Brault-Baron. De la Pertinence de l’Énumération. PhD thesis, Université Paris 7, 2013.
- [2]Guillaume Bagan, Arnaud Durand, and Étienne Grandjean. On Acyclic Conjunctive Queries and Constant Delay Enumeration. In International Workshop on Computer Science Logic, 2007.DOI
- [3]Christoph Berkholz, Jens Keppeler, and Nicole Schweikardt. Answering Conjunctive Queries under Updates. PODS, 2017.arxiv.org/abs/1702.06370
- [4]Florent Capelli, Nofar Carmeli, Oliver Irwin, and Sylvain Salvati. Direct Access for Conjunctive Queries with Negations. Logical Methods in Computer Science, Volume 22, Issue 1, March 2026.
- [5]Nofar Carmeli and Luc Segoufin. Conjunctive Queries with Self-Joins, Towards a Fine-Grained Enumeration Complexity Analysis. In PODS, 2023.
- [6]Nofar Carmeli, Nikolaos Tziavelis, Wolfgang Gatterbauer, Benny Kimelfeld, and Mirek Riedewald. Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries. ACM Transactions on Database Systems, 48(1), March 2023.
- [7]Étienne Grandjean. Sorting, Linear Time and the Satisfiability Problem. Annals of Mathematics and Artificial Intelligence, 16(1), December 1996.DOI
- [8]J. Hu. Ranked Enumeration over Circuits via Implicit Representations. PhD thesis, University of California, Santa Cruz, 2026. ProQuest ID: 32863524.
- [9]Muhammad Idris, Martin Ugarte, and Stijn Vansummeren. The Dynamic Yannakakis Algorithm: Compact and Efficient Query Processing Under Updates. SIGMOD, 2017.
- [10]Jens Keppeler. Answering Conjunctive Queries and FO+MOD Queries under Updates. PhD thesis, Humboldt-Universität zu Berlin, Mathematisch-Naturwissenschaftliche Fakultät, 2020.DOI
- [11]Ahmet Kara, Zheng Luo, Milos Nikolic, Dan Olteanu, and Haozhe Zhang. Tractable Conjunctive Queries over Static and Dynamic Relations. In ICDT, Dagstuhl, Germany, 2025.arxiv.org/abs/2404.16224
- [12]Dan Olteanu and Maximilian Schleich. Factorized Databases. SIGMOD Rec., 45(2), September 2016.
- [13]Clément Rouvroy, Nofar Carmeli, David Carral, and Luc Segoufin. Constant-Delay Enumeration of Conjunctive Queries with Self-Joins and Projections. In BDA, 2026.
- [14]Nikolaos Tziavelis, Wolfgang Gatterbauer, and Mirek Riedewald. Any-k Algorithms for Enumerating Ranked Answers to Conjunctive Queries. ACM Transactions on Database Systems, 51(1), 2025.arxiv.org/abs/2205.05649
- [15]Qichen Wang, Xiao Hu, Binyang Dai, and Ke Yi. Change Propagation Without Joins. Proc. VLDB Endow., 16(5), January 2023.DOI