title | redirect_from | ||
---|---|---|---|
Caching query results |
|
If your question results don't change frequently, you may want to store the results in Metabase so that the next time you visit the question, Metabase can retrieve the stored results rather than query the database again.
For example, if your data only updates once a day, there's no point in querying the database more than once a day, as they data won't have changed. Returning cached results can be significantly faster, as the database won't have to recompute the results to load your question.
Metabase gives you the ability to automatically cache question results that meet a minimum query duration.
If your questions share a common model, you can enable model caching instead.
- Go to Admin settings > Caching (in the sidebar).
- Click the toggle under Saved Questions.
Once you've enabled caching, you can choose when and what to cache from your caching settings.
By default, questions will get cached once their average execution time meets a minimum query duration of 60 seconds.
If you're self-hosting Metabase, cached question results will be saved to your application database.
If you're using Metabase Cloud, cached question results will be saved to Metabase's servers in the United States.
Questions that use the cache will display a "last cached at" timestamp in the question's info panel.
To override a cached question result, re-run the question using the refresh button (counterclockwise arrow).
Your Metabase instance keeps track of how long it takes each question to run. The average query execution time is used in your caching settings.
On paid plans, you can view statistics about query execution time from your auditing tools.
You can tell Metabase when and what to cache from Admin settings > Caching:
Metabase uses this number to decide whether a question will be cached or not.
Choose a duration (in seconds) that will trigger the cache. For example, you'd enter "60" if you want to cache all questions that take longer than 1 minute to load (on average).
The TTL multiplier tells Metabase how long to persist a cached question result:
Cache lifetime per question = TTL multiplier x average execution time per question
For example, if you enter a TTL multiplier of 10, a question that takes 5 seconds on average will be cached for 50 seconds. A question that takes 10 minutes will be cached for 100 minutes. This way, each question's cache lifetime is proportional to that question's execution time.
To prevent cached results from taking up too much space on your server, you can set the maximum size of the cache (per question) in kilobytes.
{% include plans-blockquote.html feature="Advanced caching controls" %}
All Metabase editions include global caching controls. On paid plans, you can override your global time-to-live (TTL) setting to set different cache lifetimes for specific databases, questions, or dashboards.
This setting tells Metabase how long to keep the cached results from a specific database.
- Make sure caching is enabled.
- Go to Admin settings > Databases and select your database.
- Open Advanced options and find the Default result cache duration.
- Click Custom and enter a cache duration in hours.
The cache duration setting is useful for databases that take longer to query, or databases that are kept up to date on a special cadence.
This setting will override your global cache duration.
You can tell Metabase how long to keep the cached results for specific questions. You'll only find these cache settings on questions that exceed the minimum query duration.
- Make sure caching is enabled.
- Go to your question.
- Click on the info icon.
- Click Cache configuration.
- Enter a cache duration in hours.
- Click Save changes.
You can use this setting to update questions on the same cadence as your data. For example, if your data gets updated daily, you can set the Cache configuration to 24 hours.
If set, your question cache duration will override the:
You can tell Metabase how long to keep the cached results for each of the questions on a dashboard.
- Make sure caching is enabled.
- Go to your dashboard.
- Click on the info icon.
- Click Cache configuration.
- Enter a cache duration in hours.
- Click Save changes.
This setting won't cache the entire dashboard at once. The dashboard cache duration will only apply to questions that exceed the minimum query duration.
If set, your dashboard cache duration will override the: