[Proposal] 'Materialised View' with pruned dimensions
Motivation
In our experience, the number of dimensions of a datasource typically range from a dozen to dozens. However, queries about the datasource always only involve several dimensions. As we know, the fewer dimensions a datasource has, the less time a query cost. Based on the idea of changing time with space, druid can build a derived datasource which only contains several common dimensions of the datasource. When users query the datasource, druid calculates results based on the derived datasource if the derived datasource contains all dimensions the query required.
From another perspective, it is also a compromise between druid and kylin. Druid has minimal degree of pre-calculation, while kylin has maximum degree of pre-calculation. That is, druid only pre-calculation the combined value of all dimensions as shown in Table1(assume there are two dimensions and one metrics). However, kylin pre-calculation all possible combinations of dimensions as shown in Table 1-3. As a result, druid calculates for each query, while kylin stores much useless result data.
Table 1:
| Dimension A | Dimension B | Metrics A |
|---|---|---|
| a1 | b1 | 1 |
| a2 | b1 | 3 |
| a1 | b2 | 5 |
Table 2:
| Dimension A | Metrics A |
|---|---|
| a1 | 6 |
| a2 | 3 |
Table 3:
| DimensionB | Metrics A |
|---|---|
| b1 | 4 |
| b2 | 5 |
Virtual datasource is proposed for druid to do more pre-calculation. The datasource which user ingests is called base-datasource, and the datasource which generated based on the base-datasource is called derived-datasource which is very similar to the notion of materialized views in traditional relational databases. Derived-datasource only involves some of dimensions of the base-datasource. It is noteworthy that user only need to know the base-datasource name. When user query the base-datasource, druid can automatically change the datasource of the query to a derived-datasource if the derived-datasource match some conditions, such as including all dimensions the query required.
In this version, we focus on the datasource which is loaded from files.
Implementation
Create and Delete Derived-datasource
Conditions:
- The timeline, metrics and granularities of a derived-datasource and its base-datasources are the same
- Derived-datasource dimensions is a subset of base-datasource dimensions.
Implementation
- Add two http interface in DatasourceResource.java. One is a POST request used to create derived datasource, and the other is a GET request to get information of derived-datasource.
curl -X POST -d @dimensions.json http://localhost:8081/druid/coordinator/v1/datasources/wikiticker/derivatives -H 'Content-Type:application/json'Dimensions.json stored all dimensions of derived-datasource, such as["metroCode","namespace","page","regionIsoCode","regionName","user"]curl -X GET http://localhost:8081/druid/coordinator/v1/datasources/wikiticker/derivatives -H 'Content-Type:application/json'The return result is all derived-datasources of wikiticker, and related dimensions:
{
"baseDataSource": "wikiticker",
"derivedDataSource": {
"wikiticker-0c224343": [
"metroCode",
"namespace",
"page",
"regionIsoCode",
"regionName",
"user"
]
}
}
-
Add an new class DerivedDatasourceManager, which is responsible for:
- Read and Write information of derived-datasource from database;
- Traverse all derived-datasource. If the timeline of derived-datasource is less than the base datasource, a hadoop-reindex-task will be submit to ingest missing data. On the contrary, if the timeline of derived-datasource is more than the base datasource, druid will set used=false for the excess segments and a kill task will be submit to remove the data.
-
Add a new table "druid_derivatives" to store information of derived-datasource. The table include 3 columns: basedatasource, deriveddatasource,dimensions. The primary key is combination of basedatasource and dimensions
-
For the purpose of submitting hadoop-reindex-tasks when timeline of derived-datasource is less than base-datasource, it is necessary to get metrics and granularities of base-datasource. Therefore, a table "druid_dataschema" is required to add. It includes 5 columns: basedatasource, start, end, ts, datashcema. The primary key is the combination of basedatasource, start, end and ts.
-
In HadoopIndexTask.java, after publishing segments to database, the schema of the task should be inserted into table "druid_dataschema". Besides, if the datasource of the task has derived datasource and the data of the derived-datasource in the interval of the task has already existed, it’s required to delete this data of derived-datasource.
-
When user delete a datasource, derived-datasource of the datasource should be deleted.
Optimizing Datasource
Conditions:
Once druid receives a query, the datasource of the query is replaced by a derived-datasource(Figure 1). The alternatives must meet the following conditions:
- The intersection between base-datasource timeline and query interval is equal to the intersection between derived-datasource timeline and query interval.
- derived-datasource includes all dimensions query need
- The chosen derived-datasource has the minimum amount of data among all derived-datasources which meet condition 1 and 2.
- If there is no suitable derived-datasource, base-datasource will not be replaced.
Figure 1:

Implementation
In BrokerResource.java, when receive a query, first optimize datasource: replacing datasource with a suitable derived-datasource. The process of optimization is as follows.
- Check if the datasource is a table datasource. If not, do not optimize. (In this version, we only support table datasource optimization)
- Check if the datasource has derived-datasource. If it has, get the information of these derived-datasources: name, dimensions and timeline.
- Traverse all derived-datasource, and find the derived-datasource which meet condition 1 and 2. If there are some derived-datasources meeting these two conditions, do the 4th step.
- Find the derived-datasource which has the minimum amount of data among all derived-datasources which meet condition 1 and 2.
@gianm @leventov What do you think about this proposal?
@jon-wei @himanshug Could you please give me some suggestions?
Pruning dimensions doesn't win you anything because when querying historical data, unused dimensions are neither accessed in memory nor loaded from disk.
This is the primary advantage of columnar storage. If it wasn't the case, there would be no point in Druid neither any other columnar data store to choose this architecture.
As a result, druid calculates for each query
Side note: enable caching (druid supports both historical- and broker-level query results cache) to reduce the amount of repetitive calculations.
@leventov Thanks for the new title.
Pruning dimensions doesn't win you anything because when querying historical data, unused dimensions are neither accessed in memory nor loaded from disk.
Pruning dimensions can reduce the number of rows. For example, there are two dimensions A and B in a segment. The cardinality of A is 10, and the cardinality of B is 100. The max number of rows in this segment is 10*100. However, if there is only dimension A in this segment, the max number of rows in is 10.
You can already do it via union queries. I don’t think it‘s worth adding it to druid itself.
@leventov
Side note: enable caching (druid supports both historical- and broker-level query results cache) to reduce the amount of repetitive calculations.
Indeed, query caching can reduce the amount of repetitive calculations. It stores the results on a per segment basis and a given query. This proposal pre-calculates the results based on dimensions and the results are considered as a datasource. We can calculate any query (with given dimensions) based on the datasource instead of a given query.
@sixtus I don't know how to do it via union queries. Could you please explain it? Thanks.
Pruning dimensions can reduce the number of rows. For example, there are two dimensions A and B in a segment. The cardinality of A is 10, and the cardinality of B is 100. The max number of rows in this segment is 10*100. However, if there is only dimension A in this segment, the max number of rows in is 10.
Could you explain what do you mean by "row" here? "Number of rows" in Druid terms could be "maximum" or "minimum", it's just static, and independent of dimensions's cardinalities.
I think what @zhangxinyu1 wants to point out here is that you will get better rollup when you have fewer dimensions with less cardinality, which means less rows to scan at query time which is expected to lead to better query performance.
Havn't really thought through all the proposed implementation details, Few initial questions -
- How would segment versioning work between derived datasource and normal datasource ?
- What would be behavior for queries for which the interval is partially covered by the derived datasource and partially by the original one ? Will they be served fully by original or derived datasource? I expect it would be a very common case for realtime ingestion.
- are you considering doing that via an extension or a core feature ? I think an extension would be better option.
Let me use a more detail example to explain it. Given a datasource with dimension A and B, such as:
| Dimension A | Dimension B | Metrics A |
|---|---|---|
| a1 | b1 | 1 |
| a2 | b1 | 3 |
| a1 | b2 | 5 |
| a2 | b2 | 7 |
In this datasource, there are 4 rows.
If we prune Dimension B, we can get the datasource:
| Dimension A | Metrics A |
|---|---|
| a1 | 6 |
| a2 | 10 |
There are only 2 rows in this datsource.
Then, we want to calculate select sum(Metrics A) where DimensionA='a1'. If we use the first datasource, druid need read the value of MetricsA in the first row and the 3rd row, and add them. But if we use the second datasource, druid only need read the value of MetricsA in the first row.
Ok, I understand now. It specializes for a specific aggregation, sum in your example.
I think your proposition with "transparent" data source names and special handling on historical query side is too complex. IMO it would be more reasonable to add handling of this on ingestion / batch indexing side, by adding some type of "multi data source ingestion spec", with multiple parts, where each part says the name of the output data source (not related to others, when segments are produced), dimensions to prune, and type of rollup aggregation (if any) which should be applied.
@nishantmonu51
I think what @zhangxinyu1 wants to point out here is that you will get better rollup when you have fewer dimensions with less cardinality, which means less rows to scan at query time which is expected to lead to better query performance.
Yes, that's exactly what I mean. Thanks.
- How would segment versioning work between derived datasource and normal datasource ?
In the same interval, segment versions of derived datasrouces are always after normal datasources. In the same interval, if the segment version of derived datasource is before the related normal datasource, the derived datasource in that interval should be reindexed.
- What would be behavior for queries for which the interval is partially covered by the derived datasource and partially by the original one ? Will they be served fully by original or derived datasource? I expect it would be a very common case for realtime ingestion.
It's a good question! I hope the query can be query can be served partially by derived datasource and partially by original. However, in this implementation, it's only served fully by original. Therefore, in this implementation, it cannot support realtime ingestion.
- are you considering doing that via an extension or a core feature ? I think an extension would be better option.
Yes, I am considering do it via an extension. As far as I can see, some interfaces need to be added into core, is it ok?
@zhangxinyu1 thanks for the proposal. I wonder what are the primary benefits of the derivedDataSource. Is the query speed the only one?
It's a good question! I hope the query can be query can be served partially by derived datasource and partially by original. However, in this implementation, it's only served fully by original. Therefore, in this implementation, it cannot support realtime ingestion.
This can also happen even in batch append ingestion. I think this is important and should be supported because most Druid users will append new data to existing dataSources in some ways.
What @zhangxinyu1 is proposing is a very old idea - the summary table. (Vertica called it a "projection" because they were academic relational folks, and so to them, projecting also implied removing duplicates.)
Summary tables tend to yield benefits if the number of rows decreases significantly, but their cost is additional storage and maintenance overhead. This would apply in Druid as in any other modern column-oriented database.
In my opinion, it is extremely important that summary tables are materialized views. That is, a user can write a query on the base data and the system transparently rewrites it to use the summary. This is important because it allows you to add/remove materialized views in future without breaking queries. We do this when we integrate Druid into Hive or Calcite; maybe Druid's native SQL (built using Calcite) could use Calcite's materialized views or lattices to achieve the same effect.
zhangxinyu1 thanks for the proposal.
here are my thoughts..
while tempting, it might not always be a good idea as you're duplicating data and need extra hardware to store/cache it. in some cases, it would be better to give that extra hardware to the original dataSources and queries might become faster without doing anything drastic.
it is already possible if customer desires so. Currently, it is done by many people by doing multiple ingestions to either create multiple dataSources from same input data .. or running batch reindexing on base dataSource. Clients don't directly talk to Druid, but to a Webservice on top of druid that handles query conversion. ( I did https://github.com/druid-io/druid/pull/3482 to add some convenience on creation of those multiple dataSources, it fell off my priority list but something that could be added )
when user is doing it explicitly, they know exactly what is happening and the results they are getting.
if Druid does above automatically then it needs to be able to handle various scenarios related to updates made to base dataSource which is doable but would definitely add more complexity to querying logic.
Think about following scenario... base dataSource A is indexed for interval Day1 , its segments have some version timestamp T. In the background, Druid submitted a job to create derived dataSource "A-Deriv" .. and around the same time user sent another indexing job for dataSource A for Day1... there is no guarantee that "version timestamp" generated for the segments for "A-Deriv" would be smaller than that of version timestamp of updated segments in A. Typically they are comparable for the same dataSource but not across because locking etc is per dataSource. and, there might be other such corner cases to handle.
@leventov
I think your proposition with "transparent" data source names and special handling on historical query side is too complex.
Historical node doesn't do anything. Instead, broker replaces the base datasource with a derived-datasource, and coordinator is responsible for submitting Kill-Task or Reindex-Task when the timeline or version of derived-datasource doesn't meet the conditions.
IMO it would be more reasonable to add handling of this on ingestion / batch indexing side, by adding some type of "multi data source ingestion spec", with multiple parts, where each part says the name of the output data source (not related to others, when segments are produced), dimensions to prune, and type of rollup aggregation (if any) which should be applied.
Thanks for your suggestion. It makes the design simpler, but how to add or remove derived datasources.
@jihoonson
I wonder what are the primary benefits of the derivedDataSource. Is the query speed the only one?
The primary benefits of the derivedDataSource is speed. Besides, it helps to use less CPU and memory.
This can also happen even in batch append ingestion. I think this is important and should be supported because most Druid users will append new data to existing dataSources in some ways.
Yes, it's really important. I also considered it before, but I didn't come up with ways to implement it. Do you have some suggestions?
@zhangxinyu1 thanks for the reply. DerivedDataSource for accelerating query speed makes sense to me.
I think two major challenges here are:
- How to update derivedDataSource on updates on original dataSource?
- How to rewrite user queries to choose the proper dataSource between derivedDataSource and original one (or both)?
The proposal explains well how these challenges are addressed, but I would like to fill some missing parts. I have two ideas for short term and long term solutions.
Idea for a short term solution
A derivedDataSource should be treated as a normal dataSource except that it is created or updated automatically on updates on its original dataSource. This means, it should be shown when listing dataSources like a normal dataSource. Of course, it would be great if the dataSource type is also provided like dataSource and derivedDataSource to figure out which dataSources are derived ones.
- How to update derivedDataSource on updates on original dataSource?
The proposed DerivedDatasourceManager looks good to maintain derivedDataSources, but it should be able to solve the problem @himanshug pointed out. A good way to do this is submitting a task for updating derivedDataSource whenever some segments are published for the original one.
- How to rewrite user queries to choose the proper dataSource between derivedDataSource and original one (or both)?
The proposal explains well how to choose the proper dataSource between derivedDataSource and the original one. However, if derivedDataSource is not prepared yet for currently ingesting data, the query should be rewritten into a unionQuery which reads a part from derivedDataSource and another from the original one. I'm not sure, but this might be possible using Calcite and I think this is the easiest way to implement this feature. @julianhyde @gianm do you think using Calcite makes sense to rewrite a query to an unionQuery?
Idea for a long term solution
I believe the derivedDataSource can be extended to be more powerful than the one proposed here. This makes it sense to have multiple derivedDataSources for a single dataSource. For example, we can create multiple derivedDataSources which are sorted in different orders like the projections in Vertica.
As a long term solution, I think we need a new layer which abstracts the original dataSource and multiple derivedDataSources. This new abstraction layer maintains the dependencies between original dataSources and their derived dataSources. The abstraction layer keeps track of all changes to original dataSources and is responsible for reflecting those changes to the derived ones. The difference from DerivedDatasourceManager is that this abstraction layer keeps track of only changes to original dataSources, and thus it doesn't have to traverse all timelines of all derivedDataSources.
For query processing, the abstraction layer returns a segment list for the dataSource and the interval specified in a user query no matter what it is a SQL or Druid's native query. The returned segment list can include the segments of either the original dataSource or a derived one. CachingClusteredClient can use a view for this abstraction layer instead of TimelineServerView to get an optimized segment list for more efficient query processing.
@jihoonson Thanks for your suggestions! I proposed the 2nd version of implementation for materialised view in #5304 based on all your suggestions. Could you please review it?