Skip to main content

Optimize Data

To optimize data for Apache Superset, make the database do less work per chart query. Superset turns every chart into a SQL query, so how the tables are stored, typed and indexed decides most of a dashboard's speed.

Server settings, caching and dashboard options are covered in Optimize Superset.

Build smaller datasets​

Not every chart needs all the data​

Don't build one wide dataset with every column and every row and point all charts at it. Build datasets around what a group of charts actually shows:

  • Select only the columns the charts use, not SELECT *.
  • Pre-aggregate where the charts only show totals, for example daily or monthly rows instead of raw events (see pre-aggregated facts below).
  • Limit the time window if dashboards only look at recent data.

Pull filter values from dimension tables​

A native filter fills its dropdown by running a DISTINCT query on the dataset it points at. If that dataset is a large fact table, every dashboard load scans it just to list filter options.

Point the filter at a small dimension table instead, such as a regions or products table, and use the same column name as the fact dataset. The filter can still apply to charts built on the fact table, as long as the column name matches.

Filter inside the base query with Jinja​

Superset runs a virtual dataset by wrapping its SQL as a subquery (or as a CTE, depending on the database) and adding chart and dashboard filters to the outer query:

SELECT region, SUM(amount)
FROM (
-- your dataset SQL runs here, without the filters
SELECT ... FROM sales JOIN ...
) AS virtual_table
WHERE region IN ('North') -- filters are applied only here
GROUP BY region

If the inner query has joins, aggregations or window functions, the database often can't push that WHERE down, so it processes the full data and then throws most of it away.

Use Jinja templating to put the filter inside the base query, so the database reads less data from the start:

SELECT region, product, amount
FROM sales
WHERE 1 = 1
{% if filter_values('region') %}
AND region IN {{ filter_values('region') | where_in }}
{% endif %}

Jinja needs the ENABLE_TEMPLATE_PROCESSING feature flag. Setup and more examples are in SQL Filters and Query Params with Jinja.

Use tables instead of views​

A regular database view is only a saved query: the database runs the whole view every time a chart queries it. For datasets behind busy dashboards, read from a physical table, or a materialized view, that a scheduled job refreshes. The expensive work then happens once per refresh, not once per chart load.

Convert calculations to pre-aggregated facts​

Calculated columns, custom SQL metrics and heavy expressions in a virtual dataset are computed on every chart load, over every row the query reads. Whenever possible, compute them once in your data pipeline and store the results in a pre-aggregated fact table:

  • Aggregate to the grain the dashboard shows. If charts show daily sales by region, store one row per day and region, not one row per order.
  • Store derived values as columns. Move CASE expressions, date truncation, unit conversions and lookups from joins into the table, so Superset reads a plain column.
  • Keep metrics re-aggregatable. Store the parts of a ratio, such as revenue and orders, rather than a precomputed avg_order_value. Superset can then calculate SUM(revenue) / SUM(orders) correctly at any level, while averaging stored averages gives wrong results.
  • Keep COUNT(DISTINCT ...) in mind. Distinct counts can't be summed across days or regions. Store them at each grain the charts need, or keep that metric on the detailed table.

A dataset on a pre-aggregated fact table returns in milliseconds, often makes caching unnecessary, and keeps the SQL in Superset simple.

Partition and cluster large tables​

Partition large fact tables by date, and set the partition column as the dataset's time column (Edit dataset → Settings → Default datetime). Dashboard time filters then become WHERE conditions on the partition column, and the database skips every partition outside the range.

  • BigQuery: partition by a DATE or TIMESTAMP column and cluster by the columns most used in filters.
  • Snowflake: add a clustering key on very large tables that are mostly filtered by date.
  • PostgreSQL: use declarative partitioning (PARTITION BY RANGE) on the date column.
  • ClickHouse: put the date and common filter columns first in ORDER BY, and partition by month.

Index filter and join columns​

On row-based databases such as PostgreSQL and MySQL, add indexes to:

  • the dataset's time column
  • columns used in native filters
  • columns used in joins inside virtual datasets
CREATE INDEX idx_sales_order_date ON sales (order_date);
CREATE INDEX idx_sales_region_date ON sales (region, order_date);

Check that an index is used by running the chart's query with EXPLAIN in SQL Lab. To get the query, open the chart's ⋯ menu and choose View query.

Use an analytical database​

Don't point dashboards at your production transactional database. Analytical queries scan many rows and compete with the application for resources.

  • Best: a columnar warehouse or OLAP engine such as ClickHouse, BigQuery, Snowflake, StarRocks, Apache Druid or Apache Doris. These read only the columns a query needs and aggregate quickly.
  • At minimum: a read replica of the transactional database, so dashboard queries can't slow down the application.

Store data in the right types​

Store dates as DATE or TIMESTAMP and numbers as numeric types, not as text. When a column needs CAST, TO_DATE or string parsing in every query:

  • the conversion runs on every row, every time
  • the database often can't use indexes or partitions on that column

Fix types once in the pipeline instead. If a dataset column is a date stored as text, mark it Is temporal and set a Datetime format in Edit dataset → Columns only as a short-term workaround.

Avoid high-cardinality group-bys​

Grouping by IDs, emails or timestamps to the second produces results with millions of rows. The database has to build them, Superset has to transfer them, and no chart can show them meaningfully.

  • Group by a coarser level: day instead of timestamp, category instead of product ID.
  • Use Time grain in the chart to truncate timestamps.
  • For "top N" charts, set Series limit or Row limit so only the top values are returned.

Make row-level security filters cheap​

Row-level security (Settings → Row Level Security) adds its filter clause to every query on the affected datasets. Write those clauses on columns that are indexed or partitioned, such as region_id = 3, rather than on expressions or subqueries, so the filter makes queries faster rather than slower.