Xi12
Xi12

Reputation: 1211

How to clear last run query (Cache) in snowflake

I want to test query performance.

-- Example:
SELECT * FROM VW_TESTROLE;

vw_testrole- has lot of joins. Since the data is cached, it is returning in less time. I want to see the query plan and How to see it or clear cache or that I can see original time taken to execute.

Thanks, Xi

Upvotes: 17

Views: 31915

Answers (2)

Anand
Anand

Reputation: 564

You can clear cache by setting

-- VIEW CURRENT PARAMETER SETTING
SHOW PARAMETERS LIKE '%CACHE%';

-- TURN OFF CACHING
ALTER SESSION UNSET USE_CACHED_RESULT;

-- TURN OFF CACHING ALTERNATIVE
ALTER SESSION SET USE_CACHED_RESULT = FALSE;

-- TURN BACK ON CACHING (THIS IS THE DEFAULT)
ALTER SESSION SET USE_CACHED_RESULT = TRUE;

To get plan of last query Id , you can use below stmt:

SELECT SYSTEM$EXPLAIN_PLAN_JSON(LAST_QUERY_ID()) AS EXPLAIN_PLAN;

-- or if you want it formatted:
SELECT PARSE_JSON(SYSTEM$EXPLAIN_PLAN_JSON(LAST_QUERY_ID())) AS EXPLAIN_PLAN;

Upvotes: 9

Gokhan Atil
Gokhan Atil

Reputation: 10039

Some extra info, as you are planning to do some "performance tests" to determine the expected execution time for a query.

The USE_CACHED_RESULT parameter disables to use of cached query results. It doesn't delete the existing caches. If you disable it, you can see the query plan (as you wanted), and your query will be executed each time without checking if the result is already available (because of previous runs of the same query). But you should know that Snowflake has multiple caches.

  1. The Warehouse cache: As Simeon mentioned in the comment, Snowflake caches recently accessed the remote data (on the shared storage) in the local disks of the warehouse nodes. That's not easy to clean. Even suspending a warehouse may not delete it.

  2. The Metadata cache - If your query access very big tables and compile time is long because of accessing metadata (for calculating stats etc), then this cache could be very important. When you re-run the query, it will probably read from the metadata cache, and significantly reduce compile time.

  3. The result cache: This is the one you are disabling.

And, running the following commands will not disable it:

ALTER SESSION UNSET USE_CACHED_RESULT=FALSE;
ALTER SESSION UNSET USE_CACHED_RESULT; 

The first one will give an error you experienced. The last one will not give an error but the default value is TRUE, so actually, it enables it. The correct command is:

ALTER SESSION SET USE_CACHED_RESULT=FALSE;

Upvotes: 22

Related Questions