Skip to main content
Version: current [24.3.x]

Best Practices for Creating Raw and Aggregation Reflections

Dremio recommends that you follow these best practices when creating reflections.

Design reflections for expensive query patterns

  1. Review query history (jobs) to determine the most expensive and most-frequent queries being submitted.
  2. Look in the job profiles for these queries. Tables and views referenced by multiple queries that perform expensive scans, joins, and aggregations are good candidates for reflections.
  3. Examine the SQL for the selected queries that reference the same table or view to find patterns that can help you define a reflection on that table or view that satisfies as many of those queries as possible.

Avoid the “more are always better” approach

Creating more reflections than are necessary to support your data consumers can lead to the use of more resources than might be optimal for your environment, both in terms of system resources and the time and attention devoted to working with them.

Establish criteria for when to create reflections

Create them only when data consumers are experiencing slow query responses, or when reports are not meeting established SLAs.

Create reflections where they do the most work without duplicating the work of other reflections

Dremio recommends that, when you create tables and views, you create them in layers:

  • The bottom or first layer consists of your tables.
  • In the second layer are views, one for each table, that do lightweight preparation of data for views in the next layers. Here, administrators might create views that do limited casting, type conversion, and field renaming, and redacting sensitive information, among other prepping operations. Administrators can also add security by subsetting both rows and fields that users in other layers are not allowed to access. The data has been lightly scrubbed and restricted to the group of people who have the business knowledge that lets them use these views to build higher-order views that data consumers can use. Then, admins grant access to these views to users who create views in the next layer, without being able to see the raw data in the tables.
  • In the third layer, users create views that perform joins and other expensive operations. This layer is where the intensive work on data is performed. These users then create reflections (raw, aggregation, or both) from their views.
  • In the fourth layer, users can create lightweight views for dashboards, reports, and visualization tools. They can also create aggregation reflections, as needed.

Establish a routine for checking how often reflections are used by the query planner

At regular intervals, check for reflections that are no longer being used and evaluate whether they should be removed. Query patterns can change over time, and frequently-used reflections can gradually become less relevant.

Use Supporting Anchors

Anchors for reflections are views that data consumers have access to from their business-intelligence tools. As you develop a better understanding of query patterns, you might want to support those patterns by creating reflections from views that perform expensive joins, transformations, filters, calculations, or a combination of those operations. You would probably not want data consumers to be able to access those views directly in situations where the query optimizer did not use any of the reflections created from those views. Repeated and concurrent queries on such views could put severe strain on system resources.

You can prevent queries run by data consumers from accessing those views directly. Anchors that perform expensive operations and to which access is restricted are called supporting anchors.

For example, suppose that you find these three, very large tables are used in many queries:

  • Customer
  • Order
  • Lineitem

You determine that there are a few common patterns in the user queries on these tables:

  • The queries frequently join the three tables together.
  • Queries always filter by commit_date < ship_date
  • There is a calculated field in most of the queries: extended_price * (1-discount) AS revenue

You can create a view that applies these common patterns, and then create a raw reflection to accelerate queries that follow these patterns.

First, you create a folder in the Dremio space that your data consumers have access to. Then, you configure this folder to be invisible and inaccessible to the data consumers.

Next, you write the query to create the view, you follow these guidelines:

  • Use SELECT * to include all fields, making it possible for the query optimizer to accelerate the broadest set of queries. Alternatively, if you know exactly which subset of fields are used in the three tables, you can include just that subset in the view.
  • Add any calculated fields, which in this case is the revenue field.
  • Apply the appropriate join on the three tables.
  • Apply any filters that are used by all queries, which in this case is only commit_date < ship_date.
  • Always use the most generic predicate possible to maximize the number of queries that will match.

Next, you run the following query to create a new view:

Create a new view
SELECT *, extendedprice * (1 - discount) AS revenue FROM customer AS c, orders AS o, lineitem AS l WHERE c.c_custkey = o.o_custkey AND l.l_orderkey = o.o_orderkey AND o.commit_date < o.ship_date

Then, you save the view in the folder that you created earlier.

Finally, you create one or more raw reflections on this new supporting anchor. If most of the queries against the view were aggregation queries, you could create an aggregation reflection. In both cases, you can select fields, as needed, to sort on or partition on.

The result is that, even though the data consumers do not have access to the supporting anchor, Dremio can accelerate their queries by using the new reflections as long as they have access to the tables that the reflections are ultimately derived from: Customer, Order, and Lineitem.

If the query optimizer should determine that a query cannot be satisfied by any of the reflections, it is possible, if no other views can satisfy it, for the query to run directly against the tables, as is always the case with any query.

Horizontally Partition Reflections that Have Many Rows

If you select a field for partitioning in a data reflection, Dremio physically groups records together into a common directory on the file system. For example, if you partition by the field Country, in which the values are two-letter abbreviations for the names of countries, such as US, UK, DE, and CA, Dremio stores the data for each country in a separate directory named US, UK, DE, CA, and so on. This optimization allows Dremio to scan a subset of the directories based on the query, which is an optimization called partition pruning.

If a user queries on records for which the value of Country is US or UK, then Dremio can apply partition pruning to scan only the US and UK directories, significantly reducing the amount of data that is scanned for the query.

When you are selecting a partitioning field for a data reflection, ask yourself these questions:

  1. Is the field used in many queries?
  2. Are there relatively few unique values in the field (low cardinality)?

To partition the data, Dremio must first sort all records, which consumes resources. Accordingly, partition data only on fields that can be used to optimize queries. In addition, the number of unique values for a field should be relatively small, so that Dremio creates only a relatively small number of partitions. If all values in a field are unique, the cost to partition outweighs the benefit.

In general, Dremio recommends the total number of partitions for a reflection to be less than 10,000.

Because reflections are created as Apache Iceberg tables, you can use partition transforms to specify transformations to apply to partition columns to produce partition values. For example, if you choose to partition on a column of timestamps, you can set partition transforms that produce partition values that are the years, months, days, or hours in those timestamps. The following table lists the partition transforms that you can choose from.

note
  • If a column is listed as a partition column, it cannot also be listed as a sort column for the same reflection.
  • In aggregation reflections, each column specified as a partition column or used in transform must also be listed as a dimension column.
  • In raw reflections, each column specified as a partition column or used in transform must also be listed as a display column.
ValueType of Partition TransformDescription
IDENTITYidentity(<column_name>)Creates one partition per value. This is the default transform. If no transform is specified for a column named by the name property, an IDENTITY transform is performed. The column can use any supported data type.
YEARyear(<column_name>)Partitions by year. The column must use the DATE or TIMESTAMP data type.
MONTHmonth(<column_name>)Partitions by month. The column must use the DATE or TIMESTAMP data type.
DAYday(<column_name>)Partitions on the equivalent of dateint. The column must use the DATE or TIMESTAMP data type.
HOURhour(<column_name>)Partitions on the equivalent of dateint and hour. The column must use the TIMESTAMP data type.
BUCKETbucket(<integer>, <column_name>)Partitions data into the number of partitions specified by an integer. For example, if the integer value N is specified, the data is partitioned into N, or (0 to (N-1)), partitions. The partition in which an individual row is stored is determined by hashing the column value and then calculating <hash_value> mod N. If the result is 0, the row is placed in partition 0; if the result is 1, the row is placed in partition 1; and so on.

The column can use the DECIMAL, INT, BIGINT, VARCHAR, VARBINARY, DATE, or TIMESTAMP data type.
TRUNCATEtruncate(<integer>, <column_name>)If the specified column uses the string data type, truncates strings to a maximum of the number of characters specified by an integer. For example, suppose the specified transform is truncate(1, stateUS). A value of CA is truncated to C, and the row is placed in partition C. A value of CO is also truncated to C, and the row is also placed in partition C.

If the specified column uses the integer or long data type, truncates column values in the following way: For any truncate(L, col), truncates the column value to the biggest multiple of L that is smaller than the column value. For example, suppose the specified transform is truncate(10, intColumn). A value of 1 is truncated to 0 and the row is placed in the partition 0. A value of 247 is truncated to 240 and the row is placed in partition 240. If the transform is truncate(3, intColumn), a value of 13 is truncated to 12 and the row is placed in partition 12. A value of 255 is not truncated, because it is divisble by 3, and the row is placed in partition 255.

The column can use the DECIMAL, INT, BIGINT, VARCHAR, or VARBINARY data type.

Note: The truncate transform does not change column values. It uses column values to calculate the correct partitions in which to place rows.

Partition Reflections to Allow for Partition-Based Incremental Refreshes

Incremental refreshes of data in reflections are much faster than full refreshes. Partition-based incremental refreshes are based on Iceberg metadata that is used to identify modified partitions and to restrict the scope of the refresh to only those partitions. For more information about partition-based incremental refreshes, see "Types of Refresh for Reflections on Apache Iceberg Tables, Filesystem Sources, Glue Sources, and Hive Sources" in Refreshing Reflections.

For partition-based incremental refreshes, both the base table and its reflections must be partitioned, and the partition transforms that they use must be compatible. The following table lists which partition transforms on the base table and which partition transforms on reflections are compatible:

Partition Transform on the Base TableCompatible Partition Transforms on Reflections
IdentityIdentity, Hour, Day, Month, Year, Truncate
HourHour, Day, Month, Year
DayDay, Month, Year
MonthMonth, Year
YearYear
TruncateTruncate
note
  • If both a base table and a reflection use the Truncate partition transform, follow these rules concerning truncation lengths:
    • If the partition column uses the String data type, the truncation length used for the reflection must be less than or equal to the truncation length used for the base table.
    • If the partition column uses the Integer data type, the remainder from the truncation length on the reflection (A) divided by the truncation length on the base table (B) must be equal to 0: A MOD B = 0
    • If the partition column uses any other data type, the truncation lengths must be identical.
  • If a base table uses the Bucket partition transform, partition-based incremental refreshes are not possible.

Partitioning Aggregation Reflections on Timestamp Data in Very Large Base Tables

Suppose you want to define an aggregation reflection on a base table that has billions of rows. The base table includes a column that either uses the TIMESTAMP data type or includes a timestamp as a string, and the base table is partitioned on that column.

In your aggregation reflection, you plan to aggregate on timestamp data that is in the base table. However, to get the benefits of partition-based incremental refresh, you need to partition the reflection in a way that is compatible with the partitioning on the base table. You can make the partitioning compatible in either of two ways:

  • By defining a view on the base table, and then defining the aggregation reflection on that view
  • By using the advanced reflection editor to define the aggregation reflection on the base table

Defining an Aggregation Reflection on a View

If the timestamp column in the base table uses the TIMESTAMP data type, use one of the functions in this table to define the corresponding column in the view. You can partition the aggregation reflection on the view column and use the partition transform that corresponds to the function.

Function in View DefinitionCorresponding Partition Transform
DATE_TRUNC('HOUR', <base_table_column>)HOUR(<view_col>)
DATE_TRUNC('DAY', <base_table_column>)DAY(<view_col>)
DATE_TRUNC('MONTH', <base_table_column>)MONTH(<view_col>)
DATE_TRUNC('YEAR', <base_table_column>)YEAR(<view_col>)
CAST <base_table_column> as DATEDAY(<view_col>)
TO_DATE(<base_table_column>)DAY(<view_col>)

If the timestamp column in the base table uses the STRING data type, use one of the functions in this table to define the corresponding column in the view. You can partition the aggregation reflection on the view column and use the partition transform that corresponds to the function.

Function in View DefinitionCorresponding Partition Transform
LEFT(<base_table_column>, X)TRUNCATE(<view_col>, X)
SUBSTR(<base_table_column>, 0, X)TRUNCATE(<view_col>, X)
SUBSTRING(<base_table_column>, 0, X)TRUNCATE(<view_col>, X)

Defining an Aggregation Reflection on a Base Table

When creating or editing the aggregation reflection in the Advanced View, as described in Creating and Editing Reflections, follow these steps:

  1. Set the base table's timestamp column as a dimension.Setting the column as a dimension.
  2. Click the down-arrow next to the green circle.
  3. Select Date for the date granularity.Selecting the granularity.

Sort Reflections on High-Cardinality Fields

The sort option is useful for optimizing queries that use filters or range predicates, especially on fields with high cardinality. If sorting is enabled, during query execution, Dremio skips over large blocks of records based on filters on sorted fields.

Dremio sorts data during the execution of a query if a reflection spans multiple nodes and is composed of multiple partitions.

Sorting on more than one field in a single data reflection typically does not improve read performance significantly and increases the costs of maintenance tasks.

For workloads that need sorting on more than one field, consider creating multiple reflections, each being sorted on a single field.

Create Reflections from Joins that are Based on Joins from Multiple Queries

Joins between tables, views, or both tend to be expensive. You can reduce the costs of joins by performing them only when building and refreshing reflections.

As an administrator, you can identify a group of queries that use similar joins. Then, you can create a general query that uses a join that is based on the similar joins, but does not include any additional predicates from the queries in the group. This generic query can serve as the basis of a raw reflection, an aggregation reflection, or both.

For example, consider the following three queries which use similar joins on views A, B and C:

Three queries with joins on views A, B, and C
SELECT a.col1, b.col1, c.col1 FROM a join b on (a.col4 = b.col4) join c on (c.col5=a.col5) 
WHERE a.size = 'M' AND a.col3 > '2001-01-01' AND b.col3 IN ('red','blue','green')
SELECT a.col1, a.col2, c.col1, COUNT(b1) FROM a join b on (a.col4 = b.col4) join c on (c.col5=a.col5)
WHERE a.size = 'M' AND b.col2 < 10 AND c.col2 > 2 GROUP BY a.col1, a.col2, c.col1
SELECT a.col1, b.col2 FROM a join b on (a.col4 = b.col4) join c on (c.col5=a.col5)
WHERE c.col1 = 123

You can write and run this generic query to create a raw reflection to accelerate all three original queries:

Create a reflection to accelerate three queries
SELECT a.col1 , a.col2, a.col3, b.col1, b.col2, b.col3, c.col1, c.col2 FROM a join b on (a.col4 = b.col4) join c on (c.col5=a.col5)