Sql-server – Decrease Data IO operation on query to lower the DTU cost on Azure

azureoptimizationquery-performancesql server

We have a FunctionApp in Azure that triggers on a timer and does some DB operations every 5 minutes. These operation are taking a very long time to complete. When I run the same query on my computer with the same data in the DB the query executes in about 2 seconds. On Azure it took almost almost 100 seconds.

We saw in the metrics on Azure that the Data IO limit hit the roof with 100% DTU. So we increased the DTU limit and the query now runs in about 48 seconds, but still hits the DTU limit for Data IO.

It's easy to say, just increase the DTU limit, but the customer isn't soo keen on paying more every month.

Soo, Is there something I can do with the below query to limit the amount of Data IO it does? Can I do it in another way? Split the query or something?

WITH CombinedTables AS 
(SELECT SourceTable,a, b, c, d, e, f, g, 
ROW_NUMBER() OVER (PARTITION BY a, b, c, d, e, f, g ORDER BY a, b, c, d, e, f, g) Row_Nbr
FROM (SELECT '2' AS SourceTable, a, b, c, d, e, f, g FROM [abc].[ExternalView]      
             UNION ALL 
             SELECT '1' AS SourceTable, a, b, c, d, e, f, g FROM [LocalTable] WITH (NOLOCK)
) AS CombinedTables GROUP BY a, b, c, d, e, f, g, SourceTable
) SELECT DISTINCT * FROM CombinedTables WHERE CombinedTables.Row_Nbr < 2 AND CombinedTables.SourceTable = '2' 

Or is the solution to limit the number of rows we operate with, so that the amount of row that needs to be processed will be lower? there are about 84k rows in the table now, and it increases some every month.

edit: Added execution plan https://prnt.sc/uuys04

Best Answer

I see a table scan and no use of any indexes in the execution plan. Create indexes to support your query and consider creating one to eliminate the sort. Apply page compression to your tables and any nonclustered indexes. Make sure you are updating stats and indexes - this maintenance is still required in Azure.