<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: collect() in SparkR and sparklyr in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/collect-in-sparkr-and-sparklyr/m-p/119257#M45822</link>
    <description>&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/156543"&gt;@jlynlangford&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This is a tricky situation, and multiple resolutions can be tried to address the performance gap,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Schema Complexity: If the DataFrame contains nested structs, arrays, or map types, collect() can become significantly slower due to complex serialization. Flattening the schema before collecting can help reduce this overhead and improve performance.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Data Partitioning: Before using collect(), inspect how the data is partitioned using, sdf_num_partitions.If there are too many or skewed partitions, consider repartitioning the DataFrame.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Switch Interface: As an effective workaround, use %sql to save the result as a Delta table, and then read it into R using a CSV or Parquet connector. This method avoids the slower collection pipeline in sparklyr.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;If any of these steps help improve performance, please share your results—and kudos to you for tackling a challenging optimization issue.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 14 May 2025 21:23:15 GMT</pubDate>
    <dc:creator>niteshm</dc:creator>
    <dc:date>2025-05-14T21:23:15Z</dc:date>
    <item>
      <title>collect() in SparkR and sparklyr</title>
      <link>https://community.databricks.com/t5/data-engineering/collect-in-sparkr-and-sparklyr/m-p/119253#M45821</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm have a vast difference in performance between SparkR:collect() and sparklyr:collect. I have a somewhat complicated query that uses WITH AS syntax to get the data set I need; there are several views defined and joins required. The final data set of this particular query is only about 2.5M rows. I am running this in an R notebook. sql(query) and&lt;SPAN&gt;sdf_sql(sc, query) take similar time to run, so I believe it is the collect method that is taking the longest amount of time.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;~ 22 seconds to run:&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%sql &amp;lt;query&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;~ 2 minutes to run:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;library&lt;/SPAN&gt;&lt;SPAN&gt;(SparkR)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data_SparkR = SparkR::collect(sql(query))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;~ 30 minutes to run:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;library(sparklyr)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;sc &amp;lt;- spark_connect(method = &lt;/SPAN&gt;&lt;SPAN&gt;"databricks"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data_sparklyr = sparklyr::sdf_collect(sparklyr::sdf_sql(sc, query))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Can anyone help me understand why sparklyr is taking so much longer than SparkR? SparkR has been deprecated for more recent Databricks environments, so I can't simply switch to SparkR. Having to wait an extra 15x for my queries to run is quite cumbersome. Any insight would be greatly appreciated.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;Jess&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;PS I'm using sparklyr 1.8.6, as the latest version (1.9.0) gives errors related to JAR files. Not sure if being able to use the most recent version of sparklyr would fix the performance issues.&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 14 May 2025 20:19:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/collect-in-sparkr-and-sparklyr/m-p/119253#M45821</guid>
      <dc:creator>jlynlangford</dc:creator>
      <dc:date>2025-05-14T20:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: collect() in SparkR and sparklyr</title>
      <link>https://community.databricks.com/t5/data-engineering/collect-in-sparkr-and-sparklyr/m-p/119257#M45822</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/156543"&gt;@jlynlangford&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This is a tricky situation, and multiple resolutions can be tried to address the performance gap,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Schema Complexity: If the DataFrame contains nested structs, arrays, or map types, collect() can become significantly slower due to complex serialization. Flattening the schema before collecting can help reduce this overhead and improve performance.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Data Partitioning: Before using collect(), inspect how the data is partitioned using, sdf_num_partitions.If there are too many or skewed partitions, consider repartitioning the DataFrame.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Switch Interface: As an effective workaround, use %sql to save the result as a Delta table, and then read it into R using a CSV or Parquet connector. This method avoids the slower collection pipeline in sparklyr.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;If any of these steps help improve performance, please share your results—and kudos to you for tackling a challenging optimization issue.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 14 May 2025 21:23:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/collect-in-sparkr-and-sparklyr/m-p/119257#M45822</guid>
      <dc:creator>niteshm</dc:creator>
      <dc:date>2025-05-14T21:23:15Z</dc:date>
    </item>
  </channel>
</rss>

