Let’s Get Certified
Module 05 / 13 · Spark SQL

Choose a table or a temporary view

Distinguish session-local SQL names from persistent tables and deliberate physical layout.

Choose a table or a temporary view

Work through the decision

A notebook registers a DataFrame under the name daily_counts. A second job starts tomorrow in a different session and cannot find that name. Should the team increase cache retention?

Check your reasoning

A local temporary view belongs to its SparkSession and exposes a DataFrame to SQL. It does not publish durable table metadata or write the rows. For tomorrow’s independent job, save data in an appropriate persistent table or durable location and arrange catalog access. Clearing cached data is separate from dropping a view or table.

Watch the source lesson at 51:32

Compare the choices

Session temporary view
Register a DataFrame with createOrReplaceTempView to query it from SQL in the same session. Its name disappears at the end of that session.
Persistent catalog table
Write or register a table with durable data and metadata that authorized future sessions can resolve. Verify catalog, schema and storage lifecycle.
Directory partitioning
Physically organize output by chosen column values so eligible filters can prune files. Watch partition cardinality and small files.
Bucketing
Use a supported fixed-bucket layout when repeated compatible operations justify it. Confirm the table provider supports the intended write and reads exploit it.
Explicit result ordering
Use orderBy or ORDER BY with tie breakers when consumers require a stable sorted result. Physical file order alone gives no cross-partition guarantee.

Test the decision

Choose partition columns that are frequently filtered and have manageable cardinality. A unique transaction ID can create excessive small partitions. Bucketing distributes rows into a fixed number of buckets using a key under supported table formats and catalogs. Sorting within buckets or partitions is distinct from a guaranteed final query order. Apply an explicit orderBy when output order is part of the contract.

Predict the busiest station before running the SQL

trips = spark.createDataFrame([("A",), ("B",), ("A",)], "station string")
trips.createOrReplaceTempView("station_visits")
spark.sql("""SELECT station, COUNT(*) AS visits FROM station_visits
             GROUP BY station ORDER BY visits DESC, station""").show()
Check the result

A has two visits and B has one. The underlying data stays in the DataFrame plan. This local temporary view belongs to the current session. A catalog table is the appropriate contract if other sessions must find the result after this one ends.

Practice the guide skills

  1. Plan a catalog table and choose directory partitioning, table bucketing or sorting for a demonstrated access pattern. Watch 51:32 · Continue at 53:05
  2. Register a DataFrame as a session-scoped view, query it with SQL and predict what another session can resolve. Watch 50:55

Official objectives for this lesson

Related lesson · Tables and views

Knowledge check

Choose a table or a temporary view quiz

Answer 3 questions. Check each answer for an explanation.