cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Does CACHE TABLE/VIEW have a create or replace like view?

fury88
New Contributor II

I'm trying to cache data/queries that we normally have as temporary views that get replaced when the code is run based on dynamic python. What I'd like to know is will CACHE TABLE get overwritten each time you run it? Is it smart enough to recognize the table is already cached and skip? Trying to avoid having this run every time if it exists.

1 REPLY 1

UmaMahesh1
Honored Contributor III

Hi @Matt Fury​ 

Yes...I guess cache overwrites each time you run it because for me it took nearly same amount of time for 1million records to be cached.

However, you can check whether the table is cached or not using .storageLevel method.

E.g. I have a table named table1. Before caching, if I run the below,

spark.table("table1").storageLevel -- Output will be StorageLevel(False, False, False, False, 1)

cache table table1; -- Now I'm caching the table

spark.table("table1").storageLevel -- Output will be StorageLevel(True, True, False, True, 1)

you can get individual flags using the respective storagelevel like

spark.table("table1").storageLevel.useMemory

spark.table("table1").storageLevel.useDisk

spark.table("table1").storageLevel.useOffHeap etc...

For more on storage levels, check out https://sparkbyexamples.com/spark/spark-persistence-storage-levels/

Cheers..

Uma Mahesh D

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group