microsoft / DurableFunctionsMonitor

A monitoring/debugging UI tool for Azure Durable Functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom sql Query performance

bhugot opened this issue · comments

Hello,
after a bit of try I found that the GetInstanceHistory query in sur custom back end for sql is not efficient at all.

Using instance in this query is a big cost.

I removed it my self and the query that was taking more than 2 min for an orchestration of 60k activities went down to 4s
I was going to propose the change for it but I saw the change you did to fallback to dbo taskhub that is killing the performance more due to missing index.

For me you should either load the TaskHubMode GlobalSettings to see if you need to fallback either do a request that try to load the instance from both dbo and taskhub to know the good taskhub value.

Here is the optimized query:

SELECT 
                    IIF(h2.TaskID IS NULL, h.Timestamp, h2.Timestamp) as Timestamp, 
                    IIF(h2.TaskID IS NULL, h.EventType, h2.EventType) as EventType,
                    h.TaskID as EventId,
                    h.Name as Name,
                    IIF(h2.TaskID IS NULL, NULL, h.Timestamp) as ScheduledTime,
                    p.Text as Result,
                    p.Reason as Details,
                    cih.InstanceID as SubOrchestrationId
                FROM
                    [{SchemaName}].History h
                    LEFT JOIN
                    [{SchemaName}].History h2
                    ON
                    (
                        h.EventType IN ('TaskScheduled', 'SubOrchestrationInstanceCreated')
                        AND
                        h2.EventType IN ('SubOrchestrationInstanceCompleted', 'SubOrchestrationInstanceFailed', 'TaskCompleted', 'TaskFailed')
                        AND
                        h.InstanceID = h2.InstanceID AND h.ExecutionID = h2.ExecutionID AND h.TaskHub = h2.TaskHub AND h.TaskID = h2.TaskID AND h.SequenceNumber != h2.SequenceNumber
                    )
                    LEFT JOIN
                    [{SchemaName}].Payloads p
                    ON
                    p.PayloadID = h2.DataPayloadID AND p.TaskHub = h2.TaskHub AND p.InstanceID = h2.InstanceID
                    LEFT JOIN
                    (
                        select 
                            cii.ParentInstanceID,
                            cii.InstanceID,
                            cii.TaskHub,
                            chh.TaskID
                        from 
                            [{SchemaName}].Instances cii
                            INNER JOIN
                            [{SchemaName}].History chh
                            ON
                            (chh.InstanceID = cii.InstanceID AND chh.TaskHub = cii.TaskHub AND chh.EventType = 'ExecutionStarted')
                    ) cih
                    ON
                    (cih.ParentInstanceID = h.InstanceID AND cih.TaskHub = h.TaskHub AND cih.TaskID = h.TaskID AND h.EventType = 'SubOrchestrationInstanceCreated')
                WHERE
                    h.EventType IN 
                    (
                        'ExecutionStarted', 'ExecutionCompleted', 'ExecutionFailed', 'ExecutionTerminated', 'TaskScheduled', 'SubOrchestrationInstanceCreated',
                        'ContinueAsNew', 'TimerCreated', 'TimerFired', 'EventRaised', 'EventSent'
                    )
                    AND
                    h.InstanceID = @OrchestrationInstanceId AND h.TaskHub = @TaskHub
                ORDER BY
                    h.SequenceNumber

Thanks, @bhugot , we'll look into this.

In the meanwhile, you can always customize the history fetching routine according to your specific needs.

yes it's what I did and working great it's more for the others ;p

@bhugot, you are totally correct.

Task hub fallback was a legacy stuff, from early days of SQL Durability Provider. I believe it can now be declared a thing of the past, especially since, as you say, it is causing severe performance issues. So do joins with Instances table.
Thanks!

I guess this can be closed now. Please, do open more tickets, if anything else is missing.