[英]SQLSERVER 2008 executing a select statement repeatedly causes High CPU usage
Is there any alternative from using a loop in my code to execute a select statement repeatedly to get value of live status column in my table? 除了在代码中使用循环重复执行select语句以获取表中活动状态列的值之外,还有其他选择吗?
It's a simple "Select status from dbo.mytable". 这是一个简单的“从dbo.mytable中选择状态”。 But,I have no idea why this simple sql statement takes so much CPU when being executed on a loop.
但是,我不知道为什么这个简单的sql语句在循环上执行时会占用这么多的CPU。
Right now,the loop makes sqlserver use more than 50% of the CPU. 现在,该循环使sqlserver使用超过50%的CPU。 Please advice any alternative or a way to solve this.
请提供任何替代方法或解决方法的建议。
Thanks. 谢谢。
Edit: Before someone says "why do you need a loop?", "don't use a loop",etc, let me explain that this is not my requirement.
编辑:在有人说“为什么需要循环?”,“不要使用循环”等之前,让我解释一下这不是我的要求。 It's from my company.Thanks
是我公司的。谢谢
As stated we need some code to properly debug but these are my thoughts... 如前所述,我们需要一些代码来正确调试,但这是我的想法...
How fast is this loop going? 这个循环进行的速度有多快? If it is quite literally
如果从字面上看
while (x == false) {
x = SQL STATEMENT
}
then yes, you will cause a massive amount of SQL load because the query will be running many thousands of times a second. 那么,是的,您将导致大量的SQL负载,因为查询每秒将运行数千次。
If on the other hand you are using a timer and looping every few seconds then such load is unusual unless: 另一方面,如果您使用计时器并每隔几秒钟循环一次,那么这种负载是不寻常的,除非:
Finally you could perhaps look at building some caching into your DAL to reduce the number of queries actually making it to the SQL server. 最后,您或许可以考虑在DAL中建立一些缓存,以减少实际进入SQL Server的查询数量。 You can implement your own logic to do this or perhaps use something like this: http://bltoolkit.net/Doc.CacheAspect.ashx
您可以实现自己的逻辑来执行此操作,或者也许使用类似以下的方式: http : //bltoolkit.net/Doc.CacheAspect.ashx
why this simple sql statement takes so much CPU
为什么这个简单的sql语句占用这么多CPU
status
status
索引 Having a non-clustered index on status
can hugely improve the performance. 非聚集
status
索引可以极大地提高性能。 Reason is SQL Server would not need to touch the table at all. 原因是SQL Server根本不需要触摸表。 But still does not make much sense to call it in a loop.
但是在循环中调用它仍然没有多大意义。
How often does your loop call the statement? 您的循环多久调用一次该语句?
There is a huge difference between 两者之间存在巨大差异
Foreach day
at noon: call query
and 和
Foreach millisecond
call query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.