0

In SQL Server profiler I am seeing pairs of completed statements. A pair is shown below. The TextData (query) is exactly the same. The SPIDs are different (86 & 88). The # of reads and writes are a little different. (as an FYI, I am running a custom template where I am filtering out exec sp_reset_connection statements and showing just the completed statements). I am seeing a bunch of these pairs of SQL:StmtCompleted and RPC:Completed and every pair contains the same SQL in TextData.

The statement shown was executed once by Entity Framework in the client.

My question: Is SQL Server actually running the queries twice? If yes, does this mean definitely the client is sending the query twice?

enter image description here

4
  • 1
    How can you be sure of this: "The statement shown was executed once by Entity Framework in the client." I've literally never heard of SQL Server doing things twice when only asked to do it once.
    – Hannah Vernon
    Dec 8, 2020 at 21:52
  • Agreed with Max Vernon, you might want to double check the code base to ensure it isn't accidentally firing this query twice. Breakpoint at the root most part of the code that would execute this query. Also might want to search the entire Solution for the Entity name used in this query to ensure there's no other forgotten places it could be accessed in the code.
    – J.D.
    Dec 8, 2020 at 22:06
  • So the screenshot actually indicates these are two distinct independent queries which ran? Before I go check the client code. Dec 8, 2020 at 23:24
  • clearly, they are on two different server processes.
    – Hannah Vernon
    Dec 9, 2020 at 0:46

2 Answers 2

1

My question: Is SQL Server actually running the queries twice?

Yep! Given that the SPIDs are different, that means they were run from completely different connections. The fact that the duration and reads are different, as you mentioned, is also a good indicator that the query was run twice (possibly with different parameters).

If yes, does this mean definitely the client is sending the query twice?

I would say yes. You should look into your application code. Maybe more than one user was accessing the system at the same time, or the query is being run from multiple threads, or something else.

Since this is EF, I might think this was the N+1 query problem (disclosure: that's my blog), but that would normally be all on the same SPID.

0

I've seen this happening with applications written in Access: basically there was the same query fired by the same client twice.

One was Microsoft Office and the other was Microsoft Office 2013 and my hypothesis was that the developers avoided any sort of compatibility issue running the query twice and then passing the data to the user application no matter if the user had installed Microsoft Office or Microsoft Office 2013, in a way or another the result would be there.

I see that you are using .Net something application. Discuss this with the development team that wrote the application

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.