High CPU Usage SQL Server (One Bad Query)
High CPU Usage SQL Server (One Bad Query)
My colleague reported to me that one of our database server is reporting consistent high CPU usage so I looked at it I noticed CPU was at 100% from last one week when I contacted the application owner and I foundthat they implemented a new feature that polls the database for every second to ensure the data collection process is running properly as it was necessary to ensure that we are under compliance in terms of reporting and auditing. So I ran a query to pull the queries with high cpu utilization with execution count. I certainly noticed a query running more often with high cpu usage.
I know that above highlighted query is causing the high cpu usage, next I looked at query stats and noticed this query is running twice every second, so I looked at the plan
Select top 1 col1 from table order by 1
Table is clustered and col1 is not part of clustered index and does not have an index. simple enough SQL server decides to do Clustered index scan and sorts(fully blocking) col1 and selects 1 row with no predicate SQL server doesn’t think its missing an index.
SQL Server Execution Times: with out non clustered index
CPU time = 2296 ms, elapsed time = 658 ms.
So I created a non clustered index on col1 in desc on the table
SQL Server Execution Times: with non clustered index
CPU time = 0 ms, elapsed time = 0 ms.
Cpu Usage dramatically reduced
Bottom line:
Its very important to understand no matter how much physical resources you might have on a server its very important understand that one bad query can literally bring the server down to it knees.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-05-31 WCF Error Handling
2019-05-31 Jira中的Tempo查看component以及issue的工作量汇总
2019-05-31 “The creator of this fault did not specify a Reason” Exception
2019-05-31 Fault Contract
2019-05-31 redirect thread aborted
2019-05-31 How-To-Ask-Questions-The-Smart-Way提问的技巧 提问的智慧
2018-05-31 NSubstitute