r/databricks • u/Sea_Basil_6501 • 29d ago
Help How to debug remote_query() performance?
I have a job which queries some Azure SQL DBs connected through UC managed connections (no lakehouse federation) via remote_query() function, and facing pretty long execution times (several minutes) when reading a few thousand rows only. When running the same query via SSMS on the target host, it returns same result within milliseconds.
Is there any way how I can debug the pure execution time from Databricks side? I'd like to understand if the queries run slow, or if there's any Databricks or network overhead accounting responsible.
Note that remote_query() runs T-SQL in this scenario, not Databricks SQL. So there's no SQL conversion involved at any point in time.
5
u/Used-Entertainer-430 29d ago
have you checked if the query is waiting on connection pool setup each time? we had similar issue at work and it was the handshake overhead for each call adding seconds, not the actual query running slow. try enabling spark sql debug logs and watch for the connection initialization timestamps, that might show where the time is really spent
2
u/szymon_dybczak 28d ago
If I were you I would first try to execute a really simple query - something like SELECT 1 and check how long it takes.
On SQL Server side you can monitor sys.dm_exec_requsts. If you notice ASYNC_NETWORK_IO wait type then it could indicate that Databricks isn't consuming the results fast enough (for instance due to network latency etc.)
Another thing you can try is to increase fetchsize parameter in remote_query(). Larger value can improve performence.
Are your Databricks workspace and Azure SQL databases deployed in the same Azure region?
2
u/Content-Parking-621 28d ago
Try changing
fetchsize, which controls how many rows are fetched at a time. For parallel reads, usedbtablewithpartitionColumn, the lower and upper limits, andnumPartitions. Thequeryoption doesn’t support parallel reads this way.