question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drastic performance difference between using SqlClient and SSMS

See original GitHub issue

We’re currently saving a complex object structure in XML form. For safety and convenience we’re using an SQL Express Server for handling the data. Right now we save the full document once every time changed (biggest being around 28MB of size) which should be no problem for SQL Server to handle.

The data is stored in a table with “Id (int, PK, Identity), Revision (int), Timestamp (datetime2), Data (xml)”. When I run the following query on SSMS (with Execution Plan), I see that it runs a clustered index seek which obviously doesn’t take long for a table with ~600 rows. Using the SQL profiler I get the full XML data (non-trimmed) in 184ms (CPU: 47, Reads: 27044).

Now I do the very same thing in my application using plain SqlClient connection with the very same query: 36923ms (CPU: 47, Reads: 18286)

It’s the exact same query when I look at the Profiler. Yet it takes ages longer until the query completes. And after that it also takes quite a while until ReadAsync() returns back with the XML data.

Can anybody explain to me why this happens? We currently have load times up to 6 minutes where as ~2.5 minutes are just retrieving the latest data from SQL Server.

As for the connection: The server is only running with TCP/IP protocols enabled.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Wraith2commented, Feb 28, 2020

In the end avoiding async/await here does not have any performance impact except speeding it up a lot.

If you don’t need that thread to be available for other work while it’s waiting for network packets then that’s ok. If you’re in a situation where many threads are competing for cpu time async would be better. Depends on your use case.

It might be possible to port the core fix to desktop. I haven’t looked. I choose to contribute my time to core issues since I know that codebase better and it’s where I wanted performance improvements.

1reaction
Skyppidcommented, Feb 28, 2020

I could but then I’d ask myself whether I should really upgrade to .Net Core if there are issues that severe… Nonetheless, it has changed nothing at all.

What I do see though in the time this query executes is that the GC is running like hell. I analyzed the heap and there are 3.023 TdsParserStateObject+PacketData objects on the heap totalling in ~25 MB memory. I see them less with the project targeting .Net Standard.

Anyway, still poor performance … it takes way too long for 25MBs of data to retrieve from SQL Server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL SSMS VS .Net Perfomance Query
OK, seems i found the answer in this POST. Same query with the same query plan takes ~10x longer when executed from ADO.NET...
Read more >
Why would SET ARITHABORT ON dramatically speed up a ...
The query is a single select containing a lot of grouping levels and aggragate operations. With SET ARITHABORT ON is takes less than...
Read more >
Query Tuning with SQL Server 2019
This article will walk you through the new features in SQL Server 2019 that help to improve query performance.
Read more >
SQL TOP statement performance tips
In this article, we will discuss the performance details of the SQL TOP statement, and we will also work on a performance case...
Read more >
Query is slower in database application than in SSMS
When you execute a query in a database application, it runs slower than the same query in an application like SQL Server Management...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found