c# 使用 SqlClient 和 ORDER BY 排序结果非常慢
昨天我在 C# 中使用 SqlClient 获取排序结果集时遇到了奇怪的情况。
以下 SQL 查询为例:
SELECT Num, Name FROM Customer WHERE Num LIKE '%V%' OR Name LIKE '%V%' ORDER BY Num ASC
在这种特殊情况下要排序的结果集约为100 行长。
问题如下:如果我在 sql-server 本身上运行查询,它的速度非常快!结果几乎在我单击“运行查询”的那一刻显示。但是当我使用 SqlClient 在 C# 中运行查询时,速度非常慢(大约 5-10 秒)。我对程序的每个小部分进行了基准测试,发现与 sql-server 的连接大约需要 10 毫秒,因此两台机器之间的链接不可能是问题。
我尝试了几乎所有方法,直到发现,如果我从查询中删除 ORDER BY Num ASC,使用 SqlClient 的 C# 查询返回结果的速度几乎与直接在 sql 服务器上运行查询一样快。
所以我的问题是:C# 中的 SqlClient 到底用 ORDER BY 做什么,与 sql-server 本身相比,将使用时间乘以数以亿计? =)
Following strange situation i got yesterday in c# when using SqlClient to get a sorted resultset.
Following SQL-Query for example:
SELECT Num, Name FROM Customer WHERE Num LIKE '%V%' OR Name LIKE '%V%' ORDER BY Num ASC
The resultset to order in this special case is about 100 lines long.
The problem is the following: If I run the query on the sql-server itself it is damn fast! The result is shown almost in the moment i click "Run query". But when I run the query in C# using SqlClient it's incredibly slow (about 5-10 seconds). I benchmarked every little part of my program and found out that the connect to the sql-server is made in about 10 milliseconds, so the link between the two machines cannot be the problem.
I tried almost everything until I found out, that the query in C# using SqlClient returns the results almost as fast as running the query directly on the sql-server, if I remove the ORDER BY Num ASC from the query.
So my question is: What the hell is SqlClient in C# doing with ORDER BY to multiply the used time by thousands of millions compared to the sql-server itself? =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试连续运行该查询两次,看看是否存在时间差异。这将确定是连接还是查询需要时间。
当你测量连接需要多长时间时,你只是测量了你所想的一部分。与数据库的实际会话直到您需要时才开始,即当您运行第一个查询时。
Try running the query twice after one another, and see if you get a time difference. That will determine if it's the connection or the query that takes time.
When you measure how long time the connection takes, you are only measuring part of what you think. The actual session with the database isn't started until you need it, i.e. when you run the first query.
看看这里关于同一主题的精彩描述:
http://social.msdn.microsoft .com/Forums/en/sqldataaccess/thread/aa4537ed-fb69-441d-82d3-0216f497f995
此查询可能会帮助您发现 SSMS 和您的应用之间的差异。
也看看@这个 http://forums.asp.net/t/917652.aspx< /a>
Have a look at good description over here on the same topic:
http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/aa4537ed-fb69-441d-82d3-0216f497f995
This query might help you spot differences between SSMS and your app.
Have a look @ this as well http://forums.asp.net/t/917652.aspx
抱歉占用了您的时间,我自己发现了这个问题。我使用了不同的数据库(在 sql-server 本身上测试查询时使用快速且优化的生产数据库,以及在 C# 中测试缓慢的非优化开发数据库)。 -.-
Sorry for stealing your time, I found the problem on my own. I used different databases (the fast and optimized production database when testing the query on the sql-server itself and the slow nonoptimized development db in c#). -.-