如何有效地使用逻辑应用程序检查foreach中的条件?

时间:2019-07-08 11:47:23

标签: azure azure-logic-apps

我有两个数组变量,每一个我进一步比较abd过程的值,但是问题是,当我有1000条记录时,要花很多时间执行。

Variable_1大约有10条记录 如果HTTP将包含约1000多个记录,则会输出Variable_2

Variable_1 = 
[
    {

      "Name": "AAA",
      "OwnerId": "121a",

    },
    {

      "Name": "BBB",
      "OwnerId": "123b",

    },
    ...
]

Variable_2 = 
[
    {     
      "Demo_Name": "AAA"     
    },
    {  
      "Demo_Name": "BBB"
    },
    {  
      "Demo_Name": "BBB"
    },
    {  
      "Demo_Name": "BBB"
    },
    {  
      "Demo_Name": "BBB"
    }
    .... 
]

我使用了2 for each one is for Variable_1 and another is for Variable_2''。

,然后比较Variable_1.Name == Variable_2.Demo_Name one by one

因此,当Variable_2的数量超过1000+ records时,大约要花一个小时才能一一检查状况

有什么解决方案可以快速实现吗? 外循环大约需要2小时 enter image description here

  

如果我尝试concurrent execution,则increment variable设置错误   值

1 个答案:

答案 0 :(得分:1)

我尝试使用Azure存储表作为结果的源来执行此操作,只是为了了解我可以快速遍历具有1465条记录的Var2和具有9条记录的Var1的过程。我在这里做了两次,只是想弄清楚最简单的方法。这里的结果是XXXX匹配,存储在Azure存储表中的值,我将在循环完成后获取该值。我添加了一个终止动作,您可以将它们移到动作For each之前,只是为了在每个循环中使用其中之一而不是两者。

作为记录,不确定是否正是您想要的。同样,通过这种方式,如果将For Each数组切换为在Var1而不是Var2上循环,则可以在不到10秒的时间内完成所有对象的操作。如果将其分成不同的数组,则可以过滤两个数组,也可以使用交集。您需要做的就是从JSON表示形式的Azure存储表中获取记录,现在将GUID作为ID。

4分钟 4 Minutes

这是两个循环中第一个循环的流程,我最喜欢的那个循环感觉更干净。 enter image description here

存储表结果,您需要将其截断或完成后执行以下操作: Azure Storage Table

您需要一个Azure存储表来将结果卸载到。 发布时,我减小了Var2的大小,因为发布1600个对象没有意义。 代码:

{ "$connections": { "value": { "azuretables": { "connectionId": "/subscriptions/*YOUR SUBSCRIPTION ID*/resourceGroups/*RESOURCEGROUP NAME*/providers/Microsoft.Web/connections/azuretables", "connectionName": "azuretables", "id": "/subscriptions/*YOUR SUBSCRIPTION ID*/providers/Microsoft.Web/locations/westeurope/managedApis/azuretables" } } }, "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "For_each": { "actions": { "For_each_2": { "actions": { "Condition": { "actions": { "Insert_Entity": { "inputs": { "body": { "PartitionKey": "@{items('For_each_2')['Name']}", "RowKey": "@{guid()}" }, "host": { "connection": { "name": "@parameters('$connections')['azuretables']['connectionId']" } }, "method": "post", "path": "/Tables/@{encodeURIComponent('stackoverflowforeachtmp')}/entities" }, "runAfter": {}, "type": "ApiConnection" } }, "expression": { "and": [ { "equals": [ "@items('For_each_2')['Name']", "@items('For_each')['Demo_Name']" ] } ] }, "runAfter": {}, "type": "If" } }, "foreach": "@variables('TestValueVariable1')", "runAfter": { "Set_TestVariable2": [ "Succeeded" ] }, "runtimeConfiguration": { "concurrency": { "repetitions": 50 } }, "type": "Foreach" }, "Set_TestVariable1": { "inputs": { "name": "TestValueVariable2", "value": "@{items('For_each')['Demo_Name']}" }, "runAfter": {}, "type": "SetVariable" }, "Set_TestVariable2": { "inputs": { "name": "TestValueVariable1", "value": "@variables('Variable 1')" }, "runAfter": { "Set_TestVariable1": [ "Succeeded" ] }, "type": "SetVariable" } }, "foreach": "@array(variables('Variable 2'))", "runAfter": { "For_each_3": [ "Succeeded" ] }, "runtimeConfiguration": { "concurrency": { "repetitions": 50 } }, "type": "Foreach" }, "For_each_3": { "actions": { "Filter_array": { "inputs": { "from": "@array(variables('Variable 1'))", "where": "@equals(items('For_each_3')['Demo_Name'], item()['Name'])" }, "runAfter": {}, "type": "Query" }, "For_each_4": { "actions": { "Insert_Entity_2": { "inputs": { "body": { "EvaluationKey": "@{items('For_each_3')['Demo_Name']}-@{items('For_each_4')['Name']}", "Owner": "@{items('For_each_4')['OwnerId']}", "PartitionKey": "@{items('For_each_4')['Name']}", "RowKey": "@{guid()}" }, "host": { "connection": { "name": "@parameters('$connections')['azuretables']['connectionId']" } }, "method": "post", "path": "/Tables/@{encodeURIComponent('stackoverflowforeachtmp')}/entities" }, "runAfter": {}, "type": "ApiConnection" } }, "foreach": "@body('Filter_array')", "runAfter": { "Filter_array": [ "Succeeded" ] }, "type": "Foreach" } }, "foreach": "@array(variables('Variable 2'))", "runAfter": { "TestValueVariable2": [ "Succeeded" ] }, "type": "Foreach" }, "Get_entities": { "inputs": { "host": { "connection": { "name": "@parameters('$connections')['azuretables']['connectionId']" } }, "method": "get", "path": "/Tables/@{encodeURIComponent('stackoverflowforeachtmp')}/entities" }, "runAfter": { "For_each": [ "Succeeded" ] }, "runtimeConfiguration": { "paginationPolicy": { "minimumItemCount": 30000 } }, "type": "ApiConnection" }, "Initialize_variable": { "inputs": { "variables": [ { "name": "Variable 1", "type": "Array", "value": [ { "Name": "AAA", "OwnerId": "121a" }, { "Name": "BBB", "OwnerId": "123b" }, { "Name": "AAA", "OwnerId": "123b" }, { "Name": "BBB", "OwnerId": "123b" }, { "Name": "FFF", "OwnerId": "123b" }, { "Name": "BBB", "OwnerId": "123b" }, { "Name": "DDD", "OwnerId": "123b" }, { "Name": "CCC", "OwnerId": "123b" }, { "Name": "BBB", "OwnerId": "123b" } ] } ] }, "runAfter": {}, "type": "InitializeVariable" }, "Initialize_variable_2": { "inputs": { "variables": [ { "name": "Variable 2", "type": "Array", "value": [ { "Demo_Name": "AAA" }, { "Demo_Name": "BBB" }, { "Demo_Name": "CCC" }, { "Demo_Name": "BBB" }, { "Demo_Name": "BBB" }, { "Demo_Name": "BBB" }, { "Demo_Name": "BBB" }, { "Demo_Name": "FFF" }, { "Demo_Name": "BBB" }, { "Demo_Name": "BBB" }, { "Demo_Name": "FFF" }, { "Demo_Name": "AAA" } ] } ] }, "runAfter": { "Initialize_variable": [ "Succeeded" ] }, "type": "InitializeVariable" }, "Set_variable": { "inputs": { "name": "TestValueVariable2", "value": "@{length(body('Get_entities')?['value'])}" }, "runAfter": { "Terminate": [ "Succeeded" ] }, "type": "SetVariable" }, "Terminate": { "inputs": { "runStatus": "Succeeded" }, "runAfter": { "Get_entities": [ "Succeeded" ] }, "type": "Terminate" }, "TestValueVariable1": { "inputs": { "variables": [ { "name": "TestValueVariable1", "type": "Array" } ] }, "runAfter": { "Initialize_variable_2": [ "Succeeded" ] }, "type": "InitializeVariable" }, "TestValueVariable2": { "inputs": { "variables": [ { "name": "TestValueVariable2", "type": "String" } ] }, "runAfter": { "TestValueVariable1": [ "Succeeded" ] }, "type": "InitializeVariable" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": { "$connections": { "defaultValue": {}, "type": "Object" } }, "triggers": { "Recurrence": { "recurrence": { "frequency": "Month", "interval": 3 }, "type": "Recurrence" } } } }