简体   繁体   中英

Bad performance on simple SQL update on Azure DB

I have a table with about 4 million rows. What I'd like to do is to add two more columns and then update the values of these two columns based on the third column in this same table. Basically I'm trying to set IsoWeek and IsoYear based on ReportDate.

I've added the columns and all the values are NULL, now I've started with simple update all script like below:

UPDATE Report
SET IsoWeek = DATEPART(ISO_WEEK, ReportDate), IsoYear = dbo.ISO_YEAR(ReportDate)

It took 5sec locally, but it was over 10min on Azure test DB so I cancelled and reimplemented the query with batches. It was around the same 5sec locally, but on Azure test DB it was still super slow. This time I've waited more and it completed in about 45 minutes.

I have to run a similar script on PROD Azure DB, so now I'm trying to find ways to optimize this update. I've added WHERE Id <= 50000 to update only one chunk:

UPDATE Report
SET IsoWeek = DATEPART(ISO_WEEK, ReportDate), IsoYear = dbo.ISO_YEAR(ReportDate)
WHERE Id <= 50000

This query executed locally in 0sec and about 7sec on Azure TEST db. This seems like a good comparison test and I started comparing execution plans.

Locally:

在此处输入图像描述

Azure TEST db:

在此处输入图像描述

So I'm not sure why is it different on local and Azure Test DB and how can I make it faster on Azure.

Any ideas?

UPD: When I removed dbo.ISO_YEAR, execution plan is now better but execution time went down from 7sec to 6sec only.

在此处输入图像描述

Looks like you have a scalar UDF in your query, causing a table spool, plus a lot of context switching. Azure will not inline these UDFs.

The table spool might be removed by changing the UDF to use SCHEMABINDING , but you're best off inlining it yourself, either direct in the query or as an iTVF.

Here is a request to add scalar UDF inlining to Azure: https://feedback.azure.com/forums/217321-sql-database/suggestions/38955436-bring-scalar-udf-inlining-to-azure-sql-database

There are many things that could be different on Azure SQL vs SQL Server on-premises and that may affect performances. For example:

  • are you using Simple Recovery model on SQL Server? Azure SQL always run in Full Recovery
  • are you using ADR on SQL Server? Azure SQL always run with ADR on
  • are you using TDE on SQL Server? Azure SQL has TDE enabled by default

Also, you don't mention with Azure SQL Tier are you using. Standard/GeneralPurpose or Premium/BusinessCritical? Or Hyperscale? How many cores or DTUs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM