0

We're reviewing the top running queries in the Azure Portal for SQL database. Wondering if there is a way to determine all of the application server requests that call that query, so we can focus on our code to optimize it (use caching, etc).

In app insights, you can go down, from the application server calls, then drill down to dependencies, but I want to go up the tree....i.e. search for the dependency call query, then work up to see which application server calls are invoking it.

4 Answers 4

2

If you cannot find the query in your code because you are using Entity Framwork, then you can log all the queries with just a few lines of code. I also added the stack trace so you know where each query is being invoked.

In the constructor of your DbContext class add the following code:

Database.Log = (sql =>
            {
                System.Diagnostics.Debug.WriteLine(System.Environment.StackTrace);
                System.Diagnostics.Debug.WriteLine(sql);
            });
Sign up to request clarification or add additional context in comments.

Comments

1

Query Performance Insight PM here. Our feature take advantage of information stored in Query Store.

Unfortunately, currently Query Store doesn't track any information about query source or host where query came from. Query Store provides aggregated view on your queries, e.g. Query X was executed Y times and took Z resources in the interval t. It's a bit hard to store all ip addresses/hosts in such setup.

So I guess for now, easiest way to get information you're looking for:

  1. Find query text
  2. Find this query in your code
  3. Add custom logging to see when it's been executed.

Also, I encourage you to create an item on our feedback portal so this request collect votes and gets into our backlog eventually.

Hope it helps. Andrejs

2 Comments

Thanks but that suggestion won't work for ASP.Net apps using Entity Framework as in code we write LINQ queries, which Entity Framework then converts to SQL, so we can't search our code for SQL. You wouldn't need to track each IP address that requested it, just the server operation name. This info is already tracked in Application Insights (AI), it is just missing a good way to search it and link it to the query Id....in Query Performance (QP), you have a query ID, it would be great to be able to search on that in AI. But even tighter interoperability between AI and QP would be great.
0

you could use AI the search tools in the portal to find the dependencies you want, and then from the details, use "all requests for this operation" or other related items links there.

if you use the AI Analytics Portal, you could write whatever kql queries you want to query that information, joining between the two tables (requests and dependencies) presuming that the dependencies properly have operationId or some other field set to join on.

2 Comments

thanks for replying. It sort of works. When you search the slow/frequent queries in Query Performance Insights, the only unique identifiers you have are the Query Id and the query command (T-SQL) . Then you to to the AI, click on Server Response Time graph, then on the new blade, click on the Dependency Duration. From here, you can't search by that Query Id (which would be ideal), but you can put in some text to search for the Query command. But you cannot simply copy the entire command and search on it, so you have to selectively choose a portion of the command text to search on.
Is that the process you were envisioning, or did I interpret that incorrectly?
0

I have exactly the same need (voted the feature request). A possible workaround (haven't tried yet) is to use IDbCommandInterceptor to modify the sql before it is sent yo the db server. The idea is to add a comment at the beginning that tags/identifies yhe method/code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.