Query #57 Buffer Usage

-- Breaks down buffers used by current database by object (table, index) in the buffer cache

-- Note: This query could take some time on a busy instance

SELECT OBJECT_NAME(p.[object_id]) AS [Object Name], p.index_id,

CAST(COUNT(*)/128.0 AS DECIMAL(10, 2)) AS [Buffer size(MB)],

COUNT(*) AS [BufferCount], p.Rows AS [Row Count],

p.data_compression_desc AS [Compression Type]

FROM sys.allocation_units AS a WITH (NOLOCK)

INNER JOIN sys.dm_os_buffer_descriptors AS b WITH (NOLOCK)

ON a.allocation_unit_id = b.allocation_unit_id

INNER JOIN sys.partitions AS p WITH (NOLOCK)

ON a.container_id = p.hobt_id

WHERE b.database_id = CONVERT(int,DB_ID())

AND p.[object_id] > 100

GROUP BY p.[object_id], p.index_id, p.data_compression_desc, p.[Rows]

ORDER BY [BufferCount] DESC OPTION (RECOMPILE);

-- Tells you what tables and indexes are using the most memory in the buffer cache

-- It can help identify possible candidates for data compression

Query #58 Table Sizes

-- Get Table names, row counts, and compression status for clustered index or heap

SELECT OBJECT_NAME(object_id) AS [ObjectName],

SUM(Rows) AS [RowCount], data_compression_desc AS [CompressionType]

FROM sys.partitions WITH (NOLOCK)

WHERE index_id < 2 --ignore the partitions from the non-clustered index if any

AND OBJECT_NAME(object_id) NOT LIKE N'sys%'

AND OBJECT_NAME(object_id) NOT LIKE N'queue_%'

AND OBJECT_NAME(object_id) NOT LIKE N'filestream_tombstone%'

AND OBJECT_NAME(object_id) NOT LIKE N'fulltext%'

AND OBJECT_NAME(object_id) NOT LIKE N'ifts_comp_fragment%'

AND OBJECT_NAME(object_id) NOT LIKE N'filetable_updates%'

AND OBJECT_NAME(object_id) NOT LIKE N'xml_index_nodes%'

AND OBJECT_NAME(object_id) NOT LIKE N'sqlagent_job%'

AND OBJECT_NAME(object_id) NOT LIKE N'plan_persist%'

GROUP BY object_id, data_compression_desc

ORDER BY SUM(Rows) DESC OPTION (RECOMPILE);

-- Gives you an idea of table sizes, and possible data compression opportunities

Query #59 Table Properties

-- Get some key table properties

SELECT OBJECT_NAME(t.[object_id]) AS [ObjectName], p.[rows] AS [Table Rows], p.index_id,

p.data_compression_desc AS [Index Data Compression],

t.create_date, t.lock_on_bulk_load, t.is_replicated, t.has_replication_filter,

t.is_tracked_by_cdc, t.lock_escalation_desc, t.is_memory_optimized, t.durability_desc, t.is_filetable,

t.temporal_type_desc, t.is_remote_data_archive_enabled, t.remote_data_archive_migration_state_desc, t.is_external -- new for SQL Server 2016

FROM sys.tables AS t WITH (NOLOCK)

INNER JOIN sys.partitions AS p WITH (NOLOCK)

ON t.[object_id] = p.[object_id]

WHERE OBJECT_NAME(t.[object_id]) NOT LIKE N'sys%'

ORDER BY OBJECT_NAME(t.[object_id]), p.index_id OPTION (RECOMPILE);

-- Gives you some good information about your tables

-- Is Memory optimized and durability description are Hekaton-related properties that were new in SQL Server 2014

-- temporal_type_desc, is_remote_data_archive_enabled, remote_data_archive_migration_state_desc, is_external are new in SQL Server 2016

Query #60 Statistics Update

-- When were Statistics last updated on all indexes?

SELECT SCHEMA_NAME(o.Schema_ID) + N'.' + o.NAME AS [Object Name], o.type_desc AS [Object Type],

i.name AS [Index Name], STATS_DATE(i.[object_id], i.index_id) AS [Statistics Date],

s.auto_created, s.no_recompute, s.user_created, s.is_incremental, s.is_temporary,

st.row_count, st.used_page_count

FROM sys.objects AS o WITH (NOLOCK)

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON o.[object_id] = i.[object_id]

INNER JOIN sys.stats AS s WITH (NOLOCK)

ON i.[object_id] = s.[object_id]

AND i.index_id = s.stats_id

INNER JOIN sys.dm_db_partition_stats AS st WITH (NOLOCK)

ON o.[object_id] = st.[object_id]

AND i.[index_id] = st.[index_id]

WHERE o.[type] IN ('U', 'V')

AND st.row_count > 0

ORDER BY STATS_DATE(i.[object_id], i.index_id) DESC OPTION (RECOMPILE);

-- Helps discover possible problems with out-of-date statistics

-- Also gives you an idea which indexes are the most active

Query #61 Volatile Indexes

-- Look at most frequently modified indexes and statistics

SELECT o.name AS [Object Name], o.[object_id], o.type_desc, s.name AS [Statistics Name],

s.stats_id, s.no_recompute, s.auto_created,

sp.modification_counter, sp.rows, sp.rows_sampled, sp.last_updated

FROM sys.objects AS o WITH (NOLOCK)

INNER JOIN sys.stats AS s WITH (NOLOCK)

ON s.object_id = o.object_id

CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS sp

WHERE o.type_desc NOT IN (N'SYSTEM_TABLE', N'INTERNAL_TABLE')

AND sp.modification_counter > 0

ORDER BY sp.modification_counter DESC, o.name OPTION (RECOMPILE);

Query #62 Index Fragmentation

-- Get fragmentation info for all indexes above a certain size in the current database

-- Note: This query could take some time on a very large database

SELECT DB_NAME(ps.database_id) AS [Database Name], OBJECT_NAME(ps.OBJECT_ID) AS [Object Name],

i.name AS [Index Name], ps.index_id, ps.index_type_desc, ps.avg_fragmentation_in_percent,

ps.fragment_count, ps.page_count, i.fill_factor, i.has_filter, i.filter_definition

FROM sys.dm_db_index_physical_stats(DB_ID(),NULL, NULL, NULL , N'LIMITED') AS ps

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON ps.[object_id] = i.[object_id]

AND ps.index_id = i.index_id

WHERE ps.database_id = DB_ID()

AND ps.page_count > 2500

ORDER BY ps.avg_fragmentation_in_percent DESC OPTION (RECOMPILE);

-- Helps determine whether you have framentation in your relational indexes

-- and how effective your index maintenance strategy is

Query #63 Overall Index Usage – Reads

-- Index Read/Write stats (all tables in current DB) ordered by Reads

SELECT OBJECT_NAME(i.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,

s.user_seeks, s.user_scans, s.user_lookups,

s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],

s.user_updates AS [Writes],

i.type_desc AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,

s.last_user_scan, s.last_user_lookup, s.last_user_seek

FROM sys.indexes AS i WITH (NOLOCK)

LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)

ON i.[object_id] = s.[object_id]

AND i.index_id = s.index_id

AND s.database_id = DB_ID()

WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1

ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -- Order by reads

-- Show which indexes in the current database are most active for Reads

Query #64 Overall Index Usage – Writes

-- Index Read/Write stats (all tables in current DB) ordered by Writes

SELECT OBJECT_NAME(i.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,

s.user_updates AS [Writes], s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],

i.type_desc AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,

s.last_system_update, s.last_user_update

FROM sys.indexes AS i WITH (NOLOCK)

LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)

ON i.[object_id] = s.[object_id]

AND i.index_id = s.index_id

AND s.database_id = DB_ID()

WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1

ORDER BY s.user_updates DESC OPTION (RECOMPILE);                         -- Order by writes

-- Show which indexes in the current database are most active for Writes

Query #65 XTP Index Usage

-- Get in-memory OLTP index usage

SELECT OBJECT_NAME(i.[object_id]) AS [Object Name], i.index_id, i.name, i.type_desc,

xis.scans_started, xis.scans_retries, xis.rows_touched, xis.rows_returned

FROM sys.dm_db_xtp_index_stats AS xis WITH (NOLOCK)

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON i.[object_id] = xis.[object_id]

AND i.index_id = xis.index_id

ORDER BY OBJECT_NAME(i.[object_id]) OPTION (RECOMPILE);

-- This gives you some index usage statistics for in-memory OLTP

-- Returns no data if you are not using in-memory OLTP

Query #66 Lock Waits

-- Get lock waits for current database

SELECT o.name AS [table_name], i.name AS [index_name], ios.index_id, ios.partition_number,

SUM(ios.row_lock_wait_count) AS [total_row_lock_waits],

SUM(ios.row_lock_wait_in_ms) AS [total_row_lock_wait_in_ms],

SUM(ios.page_lock_wait_count) AS [total_page_lock_waits],

SUM(ios.page_lock_wait_in_ms) AS [total_page_lock_wait_in_ms],

SUM(ios.page_lock_wait_in_ms)+ SUM(row_lock_wait_in_ms) AS [total_lock_wait_in_ms]

FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) AS ios

INNER JOIN sys.objects AS o WITH (NOLOCK)

ON ios.[object_id] = o.[object_id]

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON ios.[object_id] = i.[object_id]

AND ios.index_id = i.index_id

WHERE o.[object_id] > 100

GROUP BY o.name, i.name, ios.index_id, ios.partition_number

HAVING SUM(ios.page_lock_wait_in_ms)+ SUM(row_lock_wait_in_ms) > 0

ORDER BY total_lock_wait_in_ms DESC OPTION (RECOMPILE);

-- This query is helpful for troubleshooting blocking and deadlocking issues

Query #67 UDF Statistics

-- Look at UDF execution statistics

SELECT OBJECT_NAME(object_id) AS [Function Name], execution_count,

total_elapsed_time/1000 AS [time_milliseconds], fs.[type_desc]

FROM sys.dm_exec_function_stats AS fs WITH (NOLOCK)

WHERE database_id = DB_ID()

ORDER BY OBJECT_NAME(object_id) OPTION (RECOMPILE);

-- New for SQL Server 2016

-- Helps you investigate UDF performance issues

Query #68 QueryStore Options

-- Get QueryStore Options for this database

SELECT actual_state, actual_state_desc, readonly_reason,

current_storage_size_mb, max_storage_size_mb

FROM sys.database_query_store_options WITH (NOLOCK)

OPTION (RECOMPILE);

-- New for SQL Server 2016

-- Requires that QueryStore is enabled for this database

Query #69 High Aggregate Duration Queries

-- Get highest aggregate duration queries over last hour

WITH AggregatedDurationLastHour

AS

(SELECT q.query_id, SUM(count_executions * avg_duration) AS total_duration,

COUNT (distinct p.plan_id) AS number_of_plans

FROM sys.query_store_query_text AS qt WITH (NOLOCK)

INNER JOIN sys.query_store_query AS q WITH (NOLOCK)

ON qt.query_text_id = q.query_text_id

INNER JOIN sys.query_store_plan AS p WITH (NOLOCK)

ON q.query_id = p.query_id

INNER JOIN sys.query_store_runtime_stats AS rs WITH (NOLOCK)

ON rs.plan_id = p.plan_id

INNER JOIN sys.query_store_runtime_stats_interval AS rsi WITH (NOLOCK)

ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id

WHERE rsi.start_time >= DATEADD(hour, -1, GETUTCDATE())

AND rs.execution_type_desc = N'Regular'

GROUP BY q.query_id),

OrderedDuration AS

(SELECT query_id, total_duration, number_of_plans,

ROW_NUMBER () OVER (ORDER BY total_duration DESC, query_id) AS RN

FROM AggregatedDurationLastHour)

SELECT OBJECT_NAME(q.object_id) AS [Containing Object], qt.query_sql_text,

od.total_duration AS [Total Duration (microsecs)],

od.number_of_plans AS [Plan Count],

p.is_forced_plan, p.is_parallel_plan, p.is_trivial_plan,

q.query_parameterization_type_desc, p.[compatibility_level],

p.last_compile_start_time, q.last_execution_time,

CONVERT(xml, p.query_plan) AS query_plan_xml

FROM OrderedDuration AS od

INNER JOIN sys.query_store_query AS q WITH (NOLOCK)

ON q.query_id  = od.query_id

INNER JOIN sys.query_store_query_text AS qt WITH (NOLOCK)

ON q.query_text_id = qt.query_text_id

INNER JOIN sys.query_store_plan AS p WITH (NOLOCK)

ON q.query_id = p.query_id

WHERE od.RN <= 50

ORDER BY total_duration DESC OPTION (RECOMPILE);

-- New for SQL Server 2016

-- Requires that QueryStore is enabled for this database

Query #70 Recent Full Backups

-- Look at recent Full backups for the current database (Query 70) (Recent Full Backups)

SELECT TOP (30) bs.machine_name, bs.server_name, bs.database_name AS [Database Name], bs.recovery_model,

CONVERT (BIGINT, bs.backup_size / 1048576 ) AS [Uncompressed Backup Size (MB)],

CONVERT (BIGINT, bs.compressed_backup_size / 1048576 ) AS [Compressed Backup Size (MB)],

CONVERT (NUMERIC (20,2), (CONVERT (FLOAT, bs.backup_size) /

CONVERT (FLOAT, bs.compressed_backup_size))) AS [Compression Ratio], bs.has_backup_checksums, bs.is_copy_only, bs.encryptor_type,--2014 onwords

DATEDIFF (SECOND, bs.backup_start_date, bs.backup_finish_date) AS [Backup Elapsed Time (sec)],

bs.backup_finish_date AS [Backup Finish Date]

FROM msdb.dbo.backupset AS bs WITH (NOLOCK)

WHERE bs.database_name = DB_NAME(DB_ID())

AND bs.[type] = 'D' -- Change to L if you want Log backups

ORDER BY bs.backup_finish_date DESC OPTION (RECOMPILE);

-- Are your backup sizes and times changing over time?

-- Are you using backup compression?

-- Have you done any backup tuning with striped backups, or changing the parameters of the backup command?

SQL Server 诊断查询-(5)的更多相关文章

  1. SQL Server 诊断查询-(1)

    Query #1 is Version Info. SQL and OS Version information for current instance SELECT @@SERVERNAME AS ...

  2. SQL Server 诊断查询-(2)

    Query #13 SQL Server Error Log(FC) -- Shows you where the SQL Server failover cluster diagnostic log ...

  3. SQL Server 诊断查询-(4)

    Query #41 Memory Clerk Usage -- Memory Clerk Usage for instance -- Look for high value for CACHESTOR ...

  4. SQL Server 诊断查询-(3)

    Query #27 Database Properties    -- Recovery model, log reuse wait description, log file size, log u ...

  5. Expert for SQL Server 诊断系列

    Expert for SQL Server 诊断系列 Expert 诊断优化系列------------------锁是个大角色   前面几篇已经陆续从服务器的几个大块讲述了SQL SERVER数据库 ...

  6. Sql Server中查询今天、昨天、本周、上周、本月、上月数据

    Sql Server中查询今天.昨天.本周.上周.本月.上月数据 在做Sql Server开发的时候有时需要获取表中今天.昨天.本周.上周.本月.上月等数据,这时候就需要使用DATEDIFF()函数及 ...

  7. Sql Server参数化查询之where in和like实现详解

    where in 的参数化查询实现 首先说一下我们常用的办法,直接拼SQL实现,一般情况下都能满足需要 string userIds = "1,2,3,4"; using (Sql ...

  8. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参

    转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...

  9. 【转】Sql Server参数化查询之where in和like实现详解

    转载至:http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARINDE ...

随机推荐

  1. latextools \cite 自动补全

    最近在用latex写毕业论文,编辑环境用的是Sublime Text 2 加 latextools 插件,在使用latextools的\cite命令来引用参考文献时,我们希望输入\cite{ 后自动弹 ...

  2. Android UI系列-----Dialog对话框

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  3. 使用MySQL Migration Toolkit快速将Oracle数据导入MySQL[转]

    使用MySQL Migration Toolkit快速将Oracle数据导入MySQL上来先说点废话本人最近在学习一些数据库方面的知识,之前接触过Oracle和MySQL,最近又很流行MongoDB非 ...

  4. icmp,tcp,traceroute,ping,iptables

    有东莞的监控主机到北京BGP出问题了: 报警短信疯狂发送: 找东莞IDC和北京BGP服务商协查: 有个奇怪的问题:北京到东莞trcaceroute都有路由信息 东莞143段到北京全无路由信息:但,东莞 ...

  5. Windows搭建SVN

    1.服务器下载 VisualSVN 地址:http://subversion.apache.org/packages.html 2.然后下载TortoiseSVN客户端,如果要下中文语言包 也在这个页 ...

  6. Windows无法安装到GPT分区形式磁盘的解决办法

    现在很多新买的硬盘都是GTP格式,这种格式需要使用UEFI BIOS模式安装系统,我们以前传统的windows系统安装都是“MBR+legacy BIOS”模式安装 Windows无法安装到GPT分区 ...

  7. Ubuntu12.04 安装PyCharm

    1. 下载 选择Linux Tab,选择下载免费的Community Edition[1].当前版本是3.4 2. 安装PyCharm 按照官网给出的安装指导[2]进行安装. (1) Copy the ...

  8. boost 1.56.0 编译及使用

    boost的编译和使用,经过搜集资料和总结,记录成文.感谢文后所列参考资料的作者. 1 下载 地址:http://sourceforge.net/projects/boost/files/boost/ ...

  9. saiku之行速度优化(三)

    经历了前两轮优化之后,saiku由不可使用,优化到可以使用,不过在分析大量日志数据的时候,还有顿卡的感觉!继续观察背后执行的Sql,决定将注意力关注到索引上面! 日志的主要使用场景是:固定日期维度的数 ...

  10. Versions 出现 SVN Working Copy xxx locked

    Versions处于选中状态,Finder的导航栏就是Versions的导航栏,如下图,Action - Cleanup...,就可以解锁了