Skip to main content

Posts

Cost-Based Optimizer

Cost-Based Optimizer in Databricks Spark SQL can use a Cost-Based Optimizer (CBO) to improve query plans. This is especially useful for queries with multiple joins. For this to work it is critical to collect table and column statistics and keep them up to date. This functionality requires Databricks Runtime 3.3 or above. Collect statistics To get the full benefit of the CBO it is important to collect both  column statistics  and  table statistics . Statistics can be collected using the Analyze Table command. Tip To maintain the statistics up-to-date, run  ANALYZE   TABLE  after writing to the table. Verify query plans There are several ways to verify the query plan. EXPLAIN  command Use the SQL Explain command to check if the plan uses statistics. If statistics are missing then the query plan might not be optimal. Below is the sample explain plan == Optimized Logical Plan == Aggregate [ s_store...

Z-Ordering

Z-Ordering in Databricks Z-Ordering is a technique to colocate related information in the same set of files. This co-locality is automatically used by Delta Lake on Databricks data-skipping algorithms to dramatically reduce the amount of data that needs to be read. To Z-Order data, you specify the columns to order on in the  ZORDER   BY  clause: OPTIMIZE events WHERE date >= current_timestamp () - INTERVAL 1 day ZORDER BY ( eventType ) You can specify multiple columns for  ZORDER   BY  as a comma-separated list. However, the effectiveness of the locality drops with each additional column. Z-Ordering on columns that do not have statistics collected on them would be ineffective and a waste of resources as data skipping requires column-local stats such as min, max, and count. You can configure statistics collection on certain columns by re-ordering columns in the schema and/or increasing the number of columns to collect s...

Garbage collection in Databricks

Clean up snapshots Delta Lake provides snapshot isolation for reads, which means that it is safe to run  OPTIMIZE  even while other users or jobs are querying the table. Eventually however, you should clean up old snapshots. You can do this by running the  VACUUM  command: VACUUM events You control the age of the latest retained snapshot by using the  RETAIN   <N>   HOURS  option: VACUUM events RETAIN 24 HOURS Test the garbage collection You can specify  DRY   RUN  to test the garbage collection and return a list of files to be deleted: VACUUM events DRY RUN Configure the retention threshold The  VACUUM  command removes any files that are no longer in the latest state of the transaction log for the table and are older than a retention threshold. The default threshold is 7 days, but you can specify an alternate retention interval. For example, to delete all stale files older t...

Time Travel - Query an earlier version of the table

Time travel in Databricks Delta Lake time travel allows you to query an older snapshot of a Delta Lake table. For  timestamp_string , only date or timestamp strings are accepted. For example,  "2019-01-01"  and  "2019-01-01'T'00:00:00.000Z" . To query an older version of a table, specify a version or timestamp in a  SELECT  statement. For example, to query version 0 from the history above, use: Copy SELECT * FROM events VERSION AS OF 0 or Copy SELECT * FROM events TIMESTAMP AS OF '2019-01-29 00:37:58' N ote Because version 1 is at timestamp  '2019-01-29   00:38:10' , to query version 0 you can use any timestamp in the range  '2019-01-29   00:37:58'  to  '2019-01-29   00:38:09'  inclusive. DataFrameReader options allow you to create a DataFrame from a Delta Lake table that is fixed to a specific version of the table. Copy df1 = spark . read . format ( "delta" ) . ...

Accelerate queries with Delta

Accelerate queries with Delta: This query is on a Delta table with many small files. To improve the performance of queries, run the OPTIMIZE command on the table `Schema`.`table_name` Optimize a table This is similar to collect stats command as in most of the relational database systems. Once you have performed multiple changes to a table, you might have a lot of small files. To improve the speed of read queries, you can use  OPTIMIZE  to collapse small files into larger ones. The query over the Databricks Delta table runs much faster after  OPTIMIZE  is run. How much faster the query runs can depend on the configuration of the cluster you are running on, however should be  5-10X  faster compared to the standard table. Any one of the below commands can be used to optimize the table performance. OPTIMIZE delta . `/ delta / events ` OPTIMIZE events

Error: [Simba][Hardy] (34) Error from server: Bad Status: HTTP/1.1 404 Not Found in databricks connection from Tableau

[Simba][Hardy] (34) Error from server: Bad Status: HTTP/1.1 404 Not Found in databricks connection from Tableau Thais happens when we are not using the correct HTTP Path in Spark SQL connection. To resolve the issue replace the HTTP Path with the correct one from Databricks cluster: Below highlights the connection settings to be changed in Tableau datasource connection.

Map vs FlatMap(Scala)

Map vs FlatMap (Scala) val data = Seq ( "his is sample string" , "one more sample string" , "third line" , "line" , "this is one more line" ) data: Seq[String] = List(his is sample string, one more sample string, third line, line, this is one more line) val df = spark . sparkContext . parallelize ( data ) df: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[7] at parallelize at command-2343346390787948:1 df . map ( x => ( x , x . length )). collect () res0: Array[(String, Int)] = Array((his is sample string,20), (one more sample string,22), (third line,10), (line,4), (this is one more line,21)) df . map ( x => x . split ( " " )). collect () res1: Array[Array[String]] = Array(Array(his, is, sample, string), Array(one, more,...