Search code examples
azure-application-insightsms-app-analytics

Avoid running a function multiple times in a query


I have the following query in Application Insights where I run the parsejson function multiple times in the same query.

Is it possible to reuse the data from the parsejson() function after the first invocation? Right now I call it three times in the query. I am trying to see if calling it just once might be more efficient.

EventLogs
| where Timestamp > ago(1h)  
        and tostring(parsejson(tostring(Data.JsonLog)).LogId) =~ '567890'
|   project Timestamp, 
    fileSize = toint(parsejson(tostring(Data.JsonLog)).fileSize),  
    pageCount = tostring(parsejson(tostring(Data.JsonLog)).pageCount)
| limit 10

Solution

  • You can use extend for that:

    EventLogs
    | where Timestamp > ago(1h)
    | extend JsonLog = parsejson(tostring(Data.JsonLog)
    | where tostring(JsonLog.LogId) =~ '567890'
    |   project Timestamp, 
        fileSize = toint(JsonLog.fileSize),  
        pageCount = tostring(JsonLog.pageCount)
    | limit 10