Docs.snowflake.net Manuals User Guide Querying Persisted Results.html

When a query is executed, the result is persisted (i.e. cached) for a period of time (currently 24 hours). At the end of the time period, the result is purged from the system.

  1. Docs.snowflake.net Manuals User Guide Querying Persisted Results.html 2017
  2. Docs.snowflake.net Manuals User Guide Querying Persisted Results.html Video

Snowflake uses persisted query results to avoid re-generating results when nothing has changed (i.e. “retrieval optimization”).

  • Timestamp Resistant Query Caching in Append-Only Databases Draft: September 27, 2018 ABSTRACT We demonstrate a method for caching and re-using interme.
  • May 27, 2019 In this blog, we will look at Snowflake’s ‘Result Cache’ feature and its usage in terms of cost, performance and pre-aggregation. Before we dive into it, here’s a look at where the Result Cache is positioned within the Snowflake Architecture.

Using Persisted Query Results¶ When a query is executed, the result is persisted (i.e. Cached) for a period of time (currently 24 hours). At the end of the time period, the result is purged from the system. Snowflake uses persisted query results to avoid re-generating results when nothing has changed (i.e. “retrieval optimization”).

In addition, you can use persisted query results to post-process the results (e.g. layering a new query on top of the results already calculated).

In this Topic:

Retrieval Optimization¶

If a user repeats a query that has already been run, and the data in the table(s) hasn’t changed since the last time that the query was run, then the result of the query is the same.Instead of running the query again, Snowflake simply returns the same result that it returned previously. This can substantially reduce query time because Snowflake bypasses queryexecution and, instead, retrieves the result directly from the cache.

Typically, query results are reused if all of the following conditions are met:

  • The user executing the query has the necessary access privileges for all the tables used in the query.

  • The new query syntactically matches the previously-executed query.

  • The table data contributing to the query result has not changed.

  • The persisted result for the previous query is still available.

  • Any configuration options that affect how the result was produced have not changed.

  • The query does not include functions that must be evaluated at execution (e.g. CURRENT_TIMESTAMP).

  • The table’s micro-partitions have not changed (e.g. been reclustered or consolidated) due to changes to other data in the table.

Results.html

Note

Meeting all these conditions does not guarantee that Snowflake reuses the query results.

By default, result reuse is enabled, but can be overridden at the account, user, and session level using the USE_CACHED_RESULT session parameter.

Docs.snowflake.net Manuals User Guide Querying Persisted Results.html 2017

Note

Each time the persisted result for a query is reused, Snowflake resets the 24-hour retention period for the result, up to a maximum of 31 days from the date and time that the query was firstexecuted. After 31 days, the result is purged and the next time the query is submitted, a new result is generated and persisted.

Post-processing Query Results¶

In some cases, you might want to perform further processing on the result of a query that you’ve already run. For example:

Docs.snowflake.net Manuals User Guide Querying Persisted Results.html Video

  • You are developing a complex query step-by-step and you want to add a new layer on top of the previous query and run the new query without recalculating the partial results from scratch.

  • The previous query was a SHOW <objects> or DESCRIBE <object> statement, which returns results in a form that are not easy to reuse.

Docs.snowflake.net

Post-processing can be performed using the RESULT_SCAN table function. The function returns the results of the previous query as a “table” and a new query can thenbe run on the tabular data.

Examples¶

Process the result of a SHOW TABLES command and extract the following columns and rows from the result:

  • schema_name, table_name, and rows columns.

  • Rows for tables that are empty.

Additional examples are provided in RESULT_SCAN.