2018-12-13 14:47:20 -05:00
---
2019-08-21 00:48:59 -04:00
id: schema-design
title: "Schema design tips"
2018-12-13 14:47:20 -05:00
---
2018-11-13 12:38:37 -05:00
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
2019-08-21 00:48:59 -04:00
## Druid's data model
2015-07-24 15:00:36 -04:00
2021-08-13 11:42:03 -04:00
For general information, check out the documentation on [Druid's data model ](./data-model.md ) on the main
2019-08-21 00:48:59 -04:00
ingestion overview page. The rest of this page discusses tips for users coming from other kinds of systems, as well as
general tips and common practices.
2015-11-17 17:50:53 -05:00
2021-08-13 11:42:03 -04:00
* Druid data is stored in [datasources ](./data-model.md ), which are similar to tables in a traditional RDBMS.
* Druid datasources can be ingested with or without [rollup ](./rollup.md ). With rollup enabled, Druid partially aggregates your data during ingestion, potentially reducing its row count, decreasing storage footprint, and improving query performance. With rollup disabled, Druid stores one row for each row in your input data, without any pre-aggregation.
2015-07-24 15:00:36 -04:00
* Every row in Druid must have a timestamp. Data is always partitioned by time, and every query has a time filter. Query results can also be broken down by time buckets like minutes, hours, days, and so on.
2019-01-27 14:33:21 -05:00
* All columns in Druid datasources, other than the timestamp column, are either dimensions or metrics. This follows the [standard naming convention ](https://en.wikipedia.org/wiki/Online_analytical_processing#Overview_of_OLAP_systems ) of OLAP data.
* Typical production datasources have tens to hundreds of columns.
2021-08-13 11:42:03 -04:00
* [Dimension columns ](./data-model.md#dimensions ) are stored as-is, so they can be filtered on, grouped by, or aggregated at query time. They are always single Strings, [arrays of Strings ](../querying/multi-value-dimensions.md ), single Longs, single Doubles or single Floats.
2022-08-24 12:23:22 -04:00
* [Metric columns ](./data-model.md#metrics ) are stored [pre-aggregated ](../querying/aggregations.md ), so they can only be aggregated at query time (not filtered or grouped by). They are often stored as numbers (integers or floats) but can also be stored as complex objects like [HyperLogLog sketches or approximate quantile sketches ](../querying/aggregations.md#approximate-aggregations ). Metrics can be configured at ingestion time even when rollup is disabled, but are most useful when rollup is enabled.
2019-01-27 14:33:21 -05:00
## If you're coming from a...
### Relational model
(Like Hive or PostgreSQL.)
2019-08-21 00:48:59 -04:00
Druid datasources are generally equivalent to tables in a relational database. Druid [lookups ](../querying/lookups.md )
2019-01-27 14:33:21 -05:00
can act similarly to data-warehouse-style dimension tables, but as you'll see below, denormalization is often
recommended if you can get away with it.
Common practice for relational data modeling involves [normalization ](https://en.wikipedia.org/wiki/Database_normalization ):
the idea of splitting up data into multiple tables such that data redundancy is reduced or eliminated. For example, in a
"sales" table, best-practices relational modeling calls for a "product id" column that is a foreign key into a separate
"products" table, which in turn has "product id", "product name", and "product category" columns. This prevents the
product name and category from needing to be repeated on different rows in the "sales" table that refer to the same
product.
In Druid, on the other hand, it is common to use totally flat datasources that do not require joins at query time. In
the example of the "sales" table, in Druid it would be typical to store "product_id", "product_name", and
"product_category" as dimensions directly in a Druid "sales" datasource, without using a separate "products" table.
Totally flat schemas substantially increase performance, since the need for joins is eliminated at query time. As an
an added speed boost, this also allows Druid's query layer to operate directly on compressed dictionary-encoded data.
Perhaps counter-intuitively, this does _not_ substantially increase storage footprint relative to normalized schemas,
since Druid uses dictionary encoding to effectively store just a single integer per row for string columns.
2019-08-21 00:48:59 -04:00
If necessary, Druid datasources can be partially normalized through the use of [lookups ](../querying/lookups.md ),
2019-01-27 14:33:21 -05:00
which are the rough equivalent of dimension tables in a relational database. At query time, you would use Druid's SQL
`LOOKUP` function, or native lookup extraction functions, instead of using the JOIN keyword like you would in a
relational database. Since lookup tables impose an increase in memory footprint and incur more computational overhead
at query time, it is only recommended to do this if you need the ability to update a lookup table and have the changes
reflected immediately for already-ingested rows in your main table.
Tips for modeling relational data in Druid:
- Druid datasources do not have primary or unique keys, so skip those.
- Denormalize if possible. If you need to be able to update dimension / lookup tables periodically and have those
2019-08-21 00:48:59 -04:00
changes reflected in already-ingested data, consider partial normalization with [lookups ](../querying/lookups.md ).
2019-01-27 14:33:21 -05:00
- If you need to join two large distributed tables with each other, you must do this before loading the data into Druid.
Druid does not support query-time joins of two datasources. Lookups do not help here, since a full copy of each lookup
table is stored on each Druid server, so they are not a good choice for large tables.
- Consider whether you want to enable [rollup ](#rollup ) for pre-aggregation, or whether you want to disable
rollup and load your existing data as-is. Rollup in Druid is similar to creating a summary table in a relational model.
### Time series model
(Like OpenTSDB or InfluxDB.)
Similar to time series databases, Druid's data model requires a timestamp. Druid is not a timeseries database, but
2019-02-01 17:21:13 -05:00
it is a natural choice for storing timeseries data. Its flexible data model allows it to store both timeseries and
2019-01-27 14:33:21 -05:00
non-timeseries data, even in the same datasource.
To achieve best-case compression and query performance in Druid for timeseries data, it is important to partition and
2021-08-13 11:42:03 -04:00
sort by metric name, like timeseries databases often do. See [Partitioning and sorting ](./partitioning.md ) for more details.
2019-01-27 14:33:21 -05:00
Tips for modeling timeseries data in Druid:
- Druid does not think of data points as being part of a "time series". Instead, Druid treats each point separately
for ingestion and aggregation.
- Create a dimension that indicates the name of the series that a data point belongs to. This dimension is often called
"metric" or "name". Do not get the dimension named "metric" confused with the concept of Druid metrics. Place this
first in the list of dimensions in your "dimensionsSpec" for best performance (this helps because it improves locality;
2021-08-13 11:42:03 -04:00
see [partitioning and sorting ](./partitioning.md ) below for details).
2019-01-27 14:33:21 -05:00
- Create other dimensions for attributes attached to your data points. These are often called "tags" in timeseries
database systems.
2019-08-21 00:48:59 -04:00
- Create [metrics ](../querying/aggregations.md ) corresponding to the types of aggregations that you want to be able
2022-08-24 12:23:22 -04:00
to query. Typically this includes "sum", "min", and "max" (in one of the long, float, or double flavors). If you want the ability
to compute percentiles or quantiles, use Druid's [approximate aggregators ](../querying/aggregations.md#approximate-aggregations ).
2021-08-13 11:42:03 -04:00
- Consider enabling [rollup ](./rollup.md ), which will allow Druid to potentially combine multiple points into one
2019-01-27 14:33:21 -05:00
row in your Druid datasource. This can be useful if you want to store data at a different time granularity than it is
naturally emitted. It is also useful if you want to combine timeseries and non-timeseries data in the same datasource.
- If you don't know ahead of time what columns you'll want to ingest, use an empty dimensions list to trigger
2019-08-21 00:48:59 -04:00
[automatic detection of dimension columns ](#schema-less-dimensions ).
2019-01-27 14:33:21 -05:00
### Log aggregation model
(Like Elasticsearch or Splunk.)
Similar to log aggregation systems, Druid offers inverted indexes for fast searching and filtering. Druid's search
capabilities are generally less developed than these systems, and its analytical capabilities are generally more
developed. The main data modeling differences between Druid and these systems are that when ingesting data into Druid,
you must be more explicit. Druid columns have types specific upfront and Druid does not, at this time, natively support
nested data.
Tips for modeling log data in Druid:
- If you don't know ahead of time what columns you'll want to ingest, use an empty dimensions list to trigger
2019-08-21 00:48:59 -04:00
[automatic detection of dimension columns ](#schema-less-dimensions ).
2021-08-13 11:42:03 -04:00
- If you have nested data, flatten it using a [`flattenSpec` ](./ingestion-spec.md#flattenspec ).
- Consider enabling [rollup ](./rollup.md ) if you have mainly analytical use cases for your log data. This will
2019-01-27 14:33:21 -05:00
mean you lose the ability to retrieve individual events from Druid, but you potentially gain substantial compression and
query performance boosts.
## General tips and best practices
### Rollup
2019-08-21 00:48:59 -04:00
Druid can roll up data as it is ingested to minimize the amount of raw data that needs to be stored. This is a form
2021-08-13 11:42:03 -04:00
of summarization or pre-aggregation. For more details, see the [Rollup ](./rollup.md ) section of the ingestion
2019-08-21 00:48:59 -04:00
documentation.
2019-01-27 14:33:21 -05:00
### Partitioning and sorting
2019-08-21 00:48:59 -04:00
Optimally partitioning and sorting your data can have substantial impact on footprint and performance. For more details,
2021-08-13 11:42:03 -04:00
see the [Partitioning ](./partitioning.md ) section of the ingestion documentation.
2019-01-27 14:33:21 -05:00
2019-08-21 00:48:59 -04:00
< a name = "sketches" > < / a >
2015-07-24 15:00:36 -04:00
2019-01-27 14:33:21 -05:00
### Sketches for high cardinality columns
2015-07-24 15:00:36 -04:00
2019-01-27 14:33:21 -05:00
When dealing with high cardinality columns like user IDs or other unique IDs, consider using sketches for approximate
analysis rather than operating on the actual values. When you ingest data using a sketch, Druid does not store the
original raw data, but instead stores a "sketch" of it that it can feed into a later computation at query time. Popular
use cases for sketches include count-distinct and quantile computation. Each sketch is designed for just one particular
kind of computation.
In general using sketches serves two main purposes: improving rollup, and reducing memory footprint at
query time.
Sketches improve rollup ratios because they allow you to collapse multiple distinct values into the same sketch. For
example, if you have two rows that are identical except for a user ID (perhaps two users did the same action at the
same time), storing them in a count-distinct sketch instead of as-is means you can store the data in one row instead of
two. You won't be able to retrieve the user IDs or compute exact distinct counts, but you'll still be able to compute
approximate distinct counts, and you'll reduce your storage footprint.
Sketches reduce memory footprint at query time because they limit the amount of data that needs to be shuffled between
servers. For example, in a quantile computation, instead of needing to send all data points to a central location
so they can be sorted and the quantile can be computed, Druid instead only needs to send a sketch of the points. This
can reduce data transfer needs to mere kilobytes.
For details about the sketches available in Druid, see the
2022-08-24 12:23:22 -04:00
[approximate aggregators ](../querying/aggregations.md#approximate-aggregations ) page.
2019-01-27 14:33:21 -05:00
If you prefer videos, take a look at [Not exactly! ](https://www.youtube.com/watch?v=Hpd3f_MLdXo ), a conference talk
about sketches in Druid.
### String vs numeric dimensions
2017-02-28 22:04:41 -05:00
2017-07-20 03:14:14 -04:00
If the user wishes to ingest a column as a numeric-typed dimension (Long, Double or Float), it is necessary to specify the type of the column in the `dimensions` section of the `dimensionsSpec` . If the type is omitted, Druid will ingest a column as the default String type.
2017-02-28 22:04:41 -05:00
2017-03-10 15:48:59 -05:00
There are performance tradeoffs between string and numeric columns. Numeric columns are generally faster to group on
2019-01-27 14:33:21 -05:00
than string columns. But unlike string columns, numeric columns don't have indexes, so they can be slower to filter on.
You may want to experiment to find the optimal choice for your use case.
2017-02-28 22:04:41 -05:00
2021-08-13 11:42:03 -04:00
For details about how to configure numeric dimensions, see the [`dimensionsSpec` ](./ingestion-spec.md#dimensionsspec ) documentation.
2019-08-21 00:48:59 -04:00
2017-02-28 22:04:41 -05:00
2015-07-24 15:00:36 -04:00
2019-01-27 14:33:21 -05:00
### Secondary timestamps
2015-07-24 15:00:36 -04:00
2019-01-27 14:33:21 -05:00
Druid schemas must always include a primary timestamp. The primary timestamp is used for
2021-08-13 11:42:03 -04:00
[partitioning and sorting ](./partitioning.md ) your data, so it should be the timestamp that you will most often filter on.
2019-01-27 14:33:21 -05:00
Druid is able to rapidly identify and retrieve data corresponding to time ranges of the primary timestamp column.
If your data has more than one timestamp, you can ingest the others as secondary timestamps. The best way to do this
2021-08-13 11:42:03 -04:00
is to ingest them as [long-typed dimensions ](./ingestion-spec.md#dimensionsspec ) in milliseconds format.
If necessary, you can get them into this format using a [`transformSpec` ](./ingestion-spec.md#transformspec ) and
2019-08-21 00:48:59 -04:00
[expressions ](../misc/math-expr.md ) like `timestamp_parse` , which returns millisecond timestamps.
2019-01-27 14:33:21 -05:00
2022-02-11 17:43:30 -05:00
At query time, you can query secondary timestamps with [SQL time functions ](../querying/sql-scalar.md#date-and-time-functions )
2019-01-27 14:33:21 -05:00
like `MILLIS_TO_TIMESTAMP` , `TIME_FLOOR` , and others. If you're using native Druid queries, you can use
2019-08-21 00:48:59 -04:00
[expressions ](../misc/math-expr.md ).
2019-01-27 14:33:21 -05:00
### Nested dimensions
2015-07-24 15:00:36 -04:00
2019-08-21 00:48:59 -04:00
At the time of this writing, Druid does not support nested dimensions. Nested dimensions need to be flattened. For example,
2015-07-24 15:00:36 -04:00
if you have data of the following form:
2019-08-21 00:48:59 -04:00
2015-07-24 15:00:36 -04:00
```
{"foo":{"bar": 3}}
```
2019-08-21 00:48:59 -04:00
2015-07-24 15:00:36 -04:00
then before indexing it, you should transform it to:
```
{"foo_bar": 3}
```
2019-01-27 14:33:21 -05:00
Druid is capable of flattening JSON, Avro, or Parquet input data.
2021-08-13 11:42:03 -04:00
Please read about [`flattenSpec` ](./ingestion-spec.md#flattenspec ) for more details.
2018-09-04 15:54:41 -04:00
2019-08-21 00:48:59 -04:00
< a name = "counting" > < / a >
2015-07-24 15:00:36 -04:00
2019-01-27 14:33:21 -05:00
### Counting the number of ingested events
When rollup is enabled, count aggregators at query time do not actually tell you the number of rows that have been
ingested. They tell you the number of rows in the Druid datasource, which may be smaller than the number of rows
ingested.
In this case, a count aggregator at _ingestion_ time can be used to count the number of events. However, it is important to note
2019-08-21 00:48:59 -04:00
that when you query for this metric, you should use a `longSum` aggregator. A `count` aggregator at query time will return
2015-07-24 15:00:36 -04:00
the number of Druid rows for the time interval, which can be used to determine what the roll-up ratio was.
2016-01-04 13:39:10 -05:00
To clarify with an example, if your ingestion spec contains:
2015-07-24 15:00:36 -04:00
```
...
"metricsSpec" : [
{
"type" : "count",
"name" : "count"
},
...
```
You should query for the number of ingested rows with:
```
...
"aggregations": [
{ "type": "longSum", "name": "numIngestedEvents", "fieldName": "count" },
...
```
2019-08-21 00:48:59 -04:00
2019-01-27 14:33:21 -05:00
### Schema-less dimensions
2015-07-24 15:00:36 -04:00
2019-08-21 00:48:59 -04:00
If the `dimensions` field is left empty in your ingestion spec, Druid will treat every column that is not the timestamp column,
2018-11-30 14:53:57 -05:00
a dimension that has been excluded, or a metric column as a dimension.
2015-07-24 15:00:36 -04:00
2017-02-28 22:04:41 -05:00
Note that when using schema-less ingestion, all dimensions will be ingested as String-typed dimensions.
2019-01-27 14:33:21 -05:00
### Including the same column as a dimension and a metric
2015-07-24 15:00:36 -04:00
2019-08-21 00:48:59 -04:00
One workflow with unique IDs is to be able to filter on a particular ID, while still being able to do fast unique counts on the ID column.
If you are not using schema-less dimensions, this use case is supported by setting the `name` of the metric to something different than the dimension.
If you are using schema-less dimensions, the best practice here is to include the same column twice, once as a dimension, and as a `hyperUnique` metric. This may involve
2015-07-24 15:00:36 -04:00
some work at ETL time.
As an example, for schema-less dimensions, repeat the same column:
```
{"device_id_dim":123, "device_id_met":123}
```
and in your `metricsSpec` , include:
2019-08-21 00:48:59 -04:00
2015-07-24 15:00:36 -04:00
```
{ "type" : "hyperUnique", "name" : "devices", "fieldName" : "device_id_met" }
```
`device_id_dim` should automatically get picked up as a dimension.