解决数据库性能问题

发布于 2024-10-31 05:35:41 字数 661 浏览 3 评论 0原文

我遇到了数据库问题,这是我每天都要处理的场景:通常应用程序可以处理流量等,但每天我都会处理几次性能问题。当发生这种情况时,存储过程的执行时间会从 200%-1000% 增加。
我有一个,如下所述: https://stackoverflow.com/questions/5585726 /query-with-large-table-joins-optimization-techniques 。通常它的执行需要大约 6-8 秒(我已经从 20 秒优化了它,因为一开始我认为该过程是罪魁祸首),但是当某事发生时,该过程甚至可以执行 60 秒。
今天,我意识到应用程序中有更多地方冻结了,而且,我意外地意识到,从 SQL Management Studio 发出的示例查询(通常在眨眼间执行)在某些情况下开始变慢 发生在数据库服务器上。好消息是,由于这一发现,我消除了应用程序和应用程序。 Web 服务器是瓶颈。
这是数据库服务器无法处理请求。我想知道是否有一种方法可以对实时数据库进行故障排除并发现缓慢的查询或其他操作,例如重建索引、统计数据、内存问题等,或者可能导致所描述的性能下降的其他问题?

谢谢,帕维尔

I've a problem with database, and here is a scenario I'm dealing with every day: usually application can deal with traffic etc. but several times a day I'm dealing with performance issue. When it's happening stored procedures increase their execution time from 200%-1000% .
I have one, described here: https://stackoverflow.com/questions/5585726/query-with-large-table-joins-optimization-techniques . Usually its execution takes about 6-8seconds (I've already optimized it from 20seconds cos at the beginning I thought the procedure is the culprit), but when something is happening, that procedure can execute even 60seconds.
And today I realized that there are more places in application that freeze, and what is more, I accidentaly realized that sample queries issued from SQL Management Studio, that usually execute in the blink of an eye, start to slow down when something happens with database server. Good news is, that thanks to this finding I eliminated both application & web servers as bottlenecks.
It's database server that's not able to handle requests. I wonder is there a way to troubleshoot live database and spot slow queries or other action like rebiulding indexes, statistics, memory problems etc. or perhaps other issues that could result in described performance decline?

Thanks, Pawel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-11-07 05:35:41

这是我经常使用的查询。

它显示您当前正在执行的代码等待信息阻塞信息、正在运行哪些 spid、运行了多长时间、当前命令是什么、正在执行的命令是什么。查询文本为,以及 xml 查询计划(如果缓存中有):

SELECT 
d2.wait_type,
d1.session_id,
d2.blocking_session_id, 
d2.status,
d1.login_name,
d2.start_time, 
d2.command,
d3.[text] as Batch, 
SUBSTRING(d3.[text], (d2.statement_start_offset/2) + 1,
    ((CASE statement_end_offset 
        WHEN -1 THEN DATALENGTH(d3.text)
        ELSE d2.statement_end_offset END 
            - d2.statement_start_offset)/2) + 1) AS Current_Statement,
qp.query_plan,
d1.login_time, 
d2.wait_time,
d2.cpu_time, 
d1.memory_usage,
d2.total_elapsed_time, 
d2.reads,d2.writes,
d2.logical_reads
FROM sys.dm_exec_sessions d1
JOIN sys.dm_exec_requests d2 ON d1.session_id=d2.session_id
CROSS APPLY sys.dm_exec_sql_text(d2.sql_handle) d3 
CROSS APPLY sys.dm_exec_query_plan(d2.plan_handle) AS qp
WHERE d1.session_id <> @@SPID

This is a query that I use pretty much constantly.

It shows you currently executing code, Wait information, blocking information, what spids are running, how long they have been running, what the current command is, what the query text is, and the xml query plan if there is one in the cache:

SELECT 
d2.wait_type,
d1.session_id,
d2.blocking_session_id, 
d2.status,
d1.login_name,
d2.start_time, 
d2.command,
d3.[text] as Batch, 
SUBSTRING(d3.[text], (d2.statement_start_offset/2) + 1,
    ((CASE statement_end_offset 
        WHEN -1 THEN DATALENGTH(d3.text)
        ELSE d2.statement_end_offset END 
            - d2.statement_start_offset)/2) + 1) AS Current_Statement,
qp.query_plan,
d1.login_time, 
d2.wait_time,
d2.cpu_time, 
d1.memory_usage,
d2.total_elapsed_time, 
d2.reads,d2.writes,
d2.logical_reads
FROM sys.dm_exec_sessions d1
JOIN sys.dm_exec_requests d2 ON d1.session_id=d2.session_id
CROSS APPLY sys.dm_exec_sql_text(d2.sql_handle) d3 
CROSS APPLY sys.dm_exec_query_plan(d2.plan_handle) AS qp
WHERE d1.session_id <> @@SPID
溺渁∝ 2024-11-07 05:35:41

在 SSMS 中,如果右键单击服务器名称(而不是数据库名称),它将显示一个“活动监视器”菜单,单击该菜单,这将是一个很好的起点。

In SSMS if you right click on the Server name (not DB name) it will show you an Activity Monitor menu, click that and it would be a good place to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文