0

We had a performance issue with one of our queries in our application that was taking 20 seconds to run. Using azure data studio we figured out the SQL that was long running and then eventually traced that back to the entity framework query that was executed.

I had an idea of adding a logging function to our code where it is called before any data access is done (insert, select, delete, update etc) in the entity framework code.

What the function would do is simple execute a "Select user_functionname_now" sql statement.

Then in azure data studio profiler we would see :

azure data studio result

The image tells me that the user ran the load invoice function and it took 2717 milliseconds.

Granted if you have 100 users doing things in the app the logs might get mixed up a bit but it would go a long way in being able to figure out where in the code the long running query is executing from.

I was also thinking that we could add a fixed column to each query run so that you could see something like this:

logging in select statement

But the issue with adding a column is you are returning extra data each time a query is run which requires more data back and forth between the SQL server and the application and that for sure is not a good thing.

So my question is: Is adding a "Select XYZ" before every CRUD call a bad idea? If we add this logging call to some or all of our code where it executes our queries will it cause a performance issue/slowdown that I haven't thought about?

jarlh
  • 42,561
  • 8
  • 45
  • 63
Jaydel Gluckie
  • 328
  • 4
  • 12
  • First, "SELECT" is not a logging function. You are treating as "logging" only if there is something can "see" this information as queries are executed. Is that really a useful implementation? The impact on overall performance depends on many factors. If your systems handles 5000 queries (batches) per hour, adding your SELECT stmt will double that (depending on implementation). Maybe the first thing to do is search "sql server find long running queries"? Ultimately there is only one way to know - try it. – SMor Nov 04 '21 at 20:41

2 Answers2

0

I don't think using any "select ..." is reasonable in your case.

Perhaps, SET CONTEXT_INFO or sp_set_session_context would be better.

Ole
  • 271
  • 1
  • 4
0

This is the scenario that EF Query Tags are for.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • Didn't think that sending tags to SQL through EF would be dependent on the version or what our application is written in... we are using xamarin... https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support says that the support for .NET Standard 2.1 from UWP is TBD. When I attempted update to EF Core 6 it also complained about xamarinios being incompatible which is an issue when using a Xamarin project. Given MAUI is coming out shortly I am not sure that this support will be added. So EF query tags are out for now till we upgrade to MAUI – Jaydel Gluckie Nov 09 '21 at 13:42