How to troubleshoot slow queries in RDS for SQL Server

What will affect the speed of queries?

  1. Index fragmentation
    Fragmentation exists when indexes have pages in which the logical ordering, based on the key value, does not match the physical ordering inside the data file. Heavily fragmented indexes can degrade query performance and cause your application to respond slowly.
  2. Old statistics
    SQL Server collects statistical information of index and column data. SQL Server query optimizer uses it to estimate cost of query execution plans and chooses a quality-high execution plan. Large number of insertupdatedelete operations will make the statistics out of date which will cause SQL Server generates a bad execution plan.

    If you want to know details about how statistics affect SQL Server, please refer to https://blogs.msdn.microsoft.com/ggaurav/2015/04/10/how-statistics-in-sql-server-determines-performance/

  3. No suitable index (to specific slow query)
  4. Instance configurations

Now you get the general ideas of why queries are slow. Let's start the troubleshooting steps.

  1. Using the following statements to list index fragmentation in a databases.
    SELECT dbschemas.[name] as 'Schema',

dbtables.[name] as 'Table',
dbindexes.[name] as 'Index'
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID()
ORDER BY indexstats.avg_fragmentation_in_percent desc

After the degree of fragmentation is known, use the following table to determine the best method to correct the fragmentation.
avg_fragmentation_in_percent value Corrective statement

5% and < = 30% ALTER INDEX REORGANIZE
30% ALTER INDEX REBUILD WITH (ONLINE = ON)*

For more details, please refer to https://msdn.microsoft.com/en-us/library/ms189858.aspx .

  1. Check the statistics update time in this database
    SELECT t.name TableName, s.[name] StatName, STATS_DATE(t.object_id,s.[stats_id]) LastUpdated

FROM sys.[stats] AS s
JOIN sys.[tables] AS t

ON [s].[object_id] = [t].[object_id]

WHERE t.type = 'u'

A simple way to know whether the statistics are out of date is to compare the actual rows and estimated rows in an actual execution plan. If different, you need to update statistics manually. Please refer to https://msdn.microsoft.com/en-us/library/ms187348.aspx.

  1. The following statement will help check missing index in a database and give suggestions about new indexes.
    SELECT

dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) + '_'

  • REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') +
    CASE

WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END

  • REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
  • ']'
  • ' ON ' + dm_mid.statement
  • ' (' + ISNULL (dm_mid.equality_columns,'')
  • CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN ',' ELSE
    '' END
  • ISNULL (dm_mid.inequality_columns, '')
  • ')'
  • ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
    FROM sys.dm_db_missing_index_groups dm_mig

INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
GO

  1. When a specific query is slow, we can open statistics io, time and profile on to collect execution io, time and execution plan to check the bottle neck.
  2. statistics io on

set statistics time on
set statistics profile on
select a,b from Demotable where a>1
set statistics io off
set statistics time off
set statistics profile off

  1. Check the configurations of RDS for SQL Server instance, and upgrade the instance if needed.
时间: 2024-11-05 18:57:33

How to troubleshoot slow queries in RDS for SQL Server的相关文章

How to Troubleshoot High CPU in RDS for SQL Server

Sometimes we come with high CPU usage of RDS for SQL Server instance. Here's some common steps to troubleshoot this issue.   What will cause high CPU usage in SQL Server? MAXDOP T-SQL queries I/O issue caused high CPU and so on   How to troubleshoot

使用RDS for SQL Server的一些最佳实践

了解RDS的概念 这也是第一条,也是最重要的一条,在使用某项产品和服务之前,首先要了解该产品或服务的功能与限制,就像你买一个冰箱或洗衣机,通常也只有在阅读完说明书之后才能利用起来它们的所以功能,以及使用时的注意事项,RDS for SQL Server也是如此. RDS for SQL Server与传统的自建机房提供SQL Server的主要不同就是用户所需负责数据库的模块多与少的问题,自建SQL Server与阿里云RDS for SQL Server所需负责的部分图1所示: 当然,便捷的代

SQLServer · 最佳实践 · RDS for SQL Server 2012 权限限制的提升与改善

title: SQLServer · 最佳实践 · RDS for SQL Server 2012 权限限制的提升与改善 author: 石沫 背景 SQL Server 作为一种强大的关系型数据库,能够提供所有场景的应用.在绝大多数云计算公司中,都提供了SQL Server作为服务的云数据库服务,譬如阿里云.但既然是服务,那么服务就需要可管理,可控制,因此,在云计算初期,都对云数据库服务进行了严格的权限控制,好处就是可控可管理,但给用户会带来一些限制,某些限制实际上是可以再细粒度管控.因此,今

SQL Server 云下数据增量同步至阿里云 RDS for SQL Server

近期有一个需求,将云下 SQL Server 的数据同步至云上 RDS for SQL Server.不考虑增量的话,可以直接使用 SSMS 数据库导出功能直接迁移数据到 RDS,但如果要进行增量同步的话则无法直接使用,尤 其是还要做数据过滤的场景. 那该如何解呢?可以使用 kettle 工作来做增量的插入.更新及删除,以达到增量同步的效果.具体操作步骤参考如下: 1. 前期准备 安装 Java 及配置 JAVA_HOME 安装 Kettle 2. 配置源数据库 新建转换.新建一个转换任务,"文

性能测试:自建数据库与RDS性能对比SQL Server案例排查分析

近期经常遇到用户将自建数据库与RDS进行对比,简单的对比结果是自建库比RDS实例查询快.我们这里来看看一个实例,有一家物流公司,刚开始使用RDS SQL Server数据库,发现通过ECS访问RDS实例,执行语句需要60s左右,但是访问ECS本地自建库只需要2-3s.那么RDS是否是真的不如自建数据库呢? 接下来,我们来探讨对比自建库和RDS的正确姿势,如何公平地对比自建库和RDS的性能. 对比自建库和RDS的语句执行性能,下面这些因素必须都考虑到: 1. 可用区和网络链路. 可用区.网络链路的

从运维的角度分析使用阿里云数据库RDS的必要性--你不应该在阿里云上使用自建的MySQL/SQL Server/Oracle/PostgreSQL数据库

开宗明义,你不应该在阿里云上使用自建的MySQL or SQL Server数据库,对了,还有Oracle or PostgreSQL数据库. 云数据库 RDS(Relational Database Service)是一种稳定可靠.可弹性伸缩的在线数据库服务.基于飞天分布式系统和全SSD盘高性能存储,支持MySQL.SQL Server.PostgreSQL和PPAS(高度兼容Oracle)引擎,默认部署主备架构且提供了容灾.备份.恢复.监控.迁移等方面的全套解决方案. 当然,并不是指所有用户

通过excel的VBA 是否可以 直接访问阿里云的RDS数据库(SQL Server)

1.我的需求:我已经做了一个C/S的信息管理系统,客户端用的是EXCEL,数据库是局域网内的SQL SERVER,现在我只想把数据库迁移到阿里云中去(比如:迁移到云虚拟主机,或RDS数据库都可以),通过租用阿里的云,实现原来用户仍能通过EXCEL 直接访问SQL数据库中的数据,实现信息的集中存储.共享和业务应用. 2.我当前了解到:RDS数据库可以设置外网访问RDS数据库模式,并将应用服务IP加入白名单! 3.我的问题是:我的c/s客户端软件(就是EXCEL),能不能以应用服务的名义,把每个员工

RDS SQL Server - 专题分享 - 巧用执行计划缓存之Table Scan

背景引入 执行计划中的Table Scan或者是Clustered Index Scan会导致非常低下的查询性能,尤其是对于大表或者超大表.执行计划缓存是SQL Server内存管理中非常重要的特性,这篇系列文章我们探讨如何从执行计划缓存的角度来发现RDS SQL数据库引擎中的Table Scan行为,以及与之相应SQL查询语句详细信息. 问题分析 其实,我们大家都知道,Table Scan或者Clustered Index Scan是关系型数据库查询性能很差的一种表扫描查询方式,如果在数据库引

RDS SQL Server - 专题分享 - 巧用执行计划缓存之索引缺失

title: RDS SQL Server - 专题分享 - 巧用执行计划缓存之索引缺失 author: 风移 摘要 执行计划缓存是MSSQL Server内存管理十分重要的部分,同样如何巧用执行计划缓存来解决我们平时遇到的一系列问题也是一个值得深入研究的专题.这篇文章是如何巧用执行计划缓存的开篇,分享如何使用执行计划缓存来分析索引缺失(Missing Indexes). 问题引入 缺失索引是SQL Server CPU使用率居高不下的第一大杀手,也是SQL Server数据库非常大的潜在风险点