- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2022 10:56 AM
i am reading data from IBM DB2 and saving into a MS SQL server (the first step is moving the code itself to databricks, and then we will move the databases to databricks itself).
Problem I'm running into is doing something like the below will take > 1 hour before I stop it, but doing each step individually (using a pandas dataframe in the middle) results in the same thing taking maybe 15-20 minutes. I was wondering why, and what I can do to avoid using pandas.
code that doesn't work/takes forever:
(
(
spark.read.format("jdbc")
.option("driver", "com.ibm.db2.jcc.DB2Driver")
.option("url", connection_url)
.option("query", query)
.load()
)
.write.format("jdbc")
.option("url", sqlsUrl)
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.option("dbtable", table_name)
.option("user", username)
.option("password", password)
.save(mode=mode)
)
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 07:56 AM
Hi, it is related to partitioning optimization. By default, the JDBC driver queries the source database with only a single thread. So write was from one partition as one partition was created, so it was using a single core. When you used pandas, it did some transformation/actions and divided your dataset into small partitions, and then every core was writing a chunk of your dataset (partition) to SQL. Please use the below options to optimize the read, as it is auto divided
# a column that can be used that has a uniformly distributed range of values that can be used for parallelization
.option("partitionColumn", "<partition_key>")
# lowest value to pull data for with the partitionColumn
.option("lowerBound", "<min_value>")
# max value to pull data for with the partitionColumn
.option("upperBound", "<max_value>")
# number of partitions to distribute the data into. Set it to number of cores on workers
.option("numPartitions", 😎
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 07:56 AM
Hi, it is related to partitioning optimization. By default, the JDBC driver queries the source database with only a single thread. So write was from one partition as one partition was created, so it was using a single core. When you used pandas, it did some transformation/actions and divided your dataset into small partitions, and then every core was writing a chunk of your dataset (partition) to SQL. Please use the below options to optimize the read, as it is auto divided
# a column that can be used that has a uniformly distributed range of values that can be used for parallelization
.option("partitionColumn", "<partition_key>")
# lowest value to pull data for with the partitionColumn
.option("lowerBound", "<min_value>")
# max value to pull data for with the partitionColumn
.option("upperBound", "<max_value>")
# number of partitions to distribute the data into. Set it to number of cores on workers
.option("numPartitions", 😎
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 06:28 AM
I am always amazed at how good the answers can be on this site. Thank you so so much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 07:10 AM
You are welcome. Thank you for choosing my answer as the best one.

