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:ย 

spark sql update really slow

gideont
New Contributor III

I tried to use Spark as much as possible but experience some regression. Hopefully to get some direction how to use it correctly.

I've created a Databricks table using spark.sql

spark.sql('select * from example_view ') \
    .write \
    .mode('overwrite') \
    .saveAsTable('example_table')

and then I need to patch some value

%sql 
 
update example_table set create_date = '2022-02-16' where id = '123';
update example_table set create_date = '2022-02-17' where id = '124';
update example_table set create_date = '2022-02-18' where id = '125';
update example_table set create_date = '2022-02-19' where id = '126';

However, I found this awlfully slow since it created hundreds of spark jobs:

image.pngWhy it Spark doing this and any suggestion how to improve my code? Last thing I want to do is to convert it back to Pandas and update the cell values individually. Any suggestion is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Pat
Honored Contributor III

Hi, @Vincent Doeโ€‹ ,

Updates are available in Delta tables, but under the hood you are updating parquet files, it means that each update needs to find the file where records are stored, then re-write the file to new version, and make new file current version.

In your case maybe you should try something like this:

    spark.sql("""
select 
col1,
col2,
col3,
case 
when id = '123' then '2022-02-16'
when id = '124' then '2022-02-17'
end as create_date
...
 from example_view
""") \
        .write \
        .mode('overwrite') \
        .saveAsTable('example_table')

View solution in original post

2 REPLIES 2

Pat
Honored Contributor III

Hi, @Vincent Doeโ€‹ ,

Updates are available in Delta tables, but under the hood you are updating parquet files, it means that each update needs to find the file where records are stored, then re-write the file to new version, and make new file current version.

In your case maybe you should try something like this:

    spark.sql("""
select 
col1,
col2,
col3,
case 
when id = '123' then '2022-02-16'
when id = '124' then '2022-02-17'
end as create_date
...
 from example_view
""") \
        .write \
        .mode('overwrite') \
        .saveAsTable('example_table')

gideont
New Contributor III

@Pat Sienkiewiczโ€‹ . That's good tips. Thanks.

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