How can I optimize ADX query performance to find last timestamp of a specific timeseries id ?

Galgani, Stefano 205 Reputation points
2023-11-06T17:33:45.7433333+00:00

Hi community,

I have a Telemetry table as followed:
Telemetry table

The partition is applied only on TimeseriesId field and the default on ingest_time
I have an use case where I need to find the last ServerTimeStamp of a specific TimeseriesId

When I have performed the followed query

Telemetry | 
where TimeseriesId == '1d4037bc-xxxx-xxxx-xxxx-xxxxxxxxx' | 
where isnotnull(TimeseriesName) | summarize arg_max(ServerTimeStamp, *) by TimeseriesName | 
project TimeseriesId, TimeseriesName,ServerTimeStamp, ServerTimeStampString = todatetime(unixtime_seconds_todatetime(ServerTimeStamp)), Value, Quality

The result is gotten about in 99 sec, as we can see from the followed image

querytoomuchlong

My questions are:

  • Why does query take long time (about 99 sec) ?
  • How can I improve the query ?
  • Do you have any suggestions ?

Thanks

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
502 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 30,786 Reputation points MVP
    2023-11-06T22:56:56.41+00:00

    Hello @Galgani, Stefano ,

    it's hard to optimes a table out of the blue.

    It's not clear neither how many records there are in the table, how large your cluster is, nor how much of the data is available in the cache.

    Any KQL query acts like a funnel so I would put the largest filter at the top.

    You say you need to find the last ServerTimeStamp of a specific TimeseriesId so I would leave out the TimeSeriesName part.

    the arg_max seems to be able to find the latest date already:

    Telemetry | summarize arg_max(ServerTimeStamp, *) by TimeseriesId | where TimeseriesId == '1d4037bc-xxxx-xxxx-xxxx-xxxxxxxxx' | project TimeseriesId, TimeseriesName, ServerTimeStamp, ServerTimeStampString = todatetime(unixtime_seconds_todatetime(ServerTimeStamp)), Value, Quality

    Does this help?

    Otherwise, an alternative is this top 1 by solution:

    Telemetry | order by ServerTimeStamp desc | where TimeseriesId == '1d4037bc-xxxx-xxxx-xxxx-xxxxxxxxx' | top 1 by TimeseriesId

    Try to find out which part is slowing you down by 'pealing off' statements of your query.


    This answer is written by myself, not by some AI. If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.

  2. Wilko van de Velde 2,226 Reputation points
    2023-11-07T06:58:51.25+00:00

    Hi @Galgani, Stefano ,

    Take a look at the best practices for Kusto queries, maybe there are some tips you can use:

    https://video2.skills-academy.com/en-us/azure/data-explorer/kusto/query/best-practices

    Kind regards,

    Wilko


    Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments