Do I have to run .cache() on my dataframe before returning aggregations like count?

User16826992666
Databricks Employee
Databricks Employee
 

Srikanth_Gupta_
Databricks Employee
Databricks Employee

Better to use cache when dataframe is used multiple times in a single pipeline.

Using  cache()  and persist()  methods, Spark provides an optimization mechanism to store the intermediate computation of a Spark DataFrame so they can be reused in subsequent actions.

sean_owen
Databricks Employee
Databricks Employee

You do not have to cache anything to make it work. You would decide that based on whether you want to spend memory/storage to avoid recomputing the DataFrame, like when you may use it in multiple operations afterwards.