This is all begins from a mail.

...

Dear sir:

This is liulei.

Thanks for your help about last PM for data migration to SSD storage. However, it is still important for monitor SQL server inefficient
SQL statements.

I can do nothing before without thrid part database monitor software. Does it means we have no idear to fix it ?

The answer is NO. This mail is the answer.

First, I would like to introduce some script.

checkCPU -- check CPU status every 6 minutes about 255 records

getperformancetest -- the original script is copied from yours, I made it as a procedure and conver xml data to nvarchar(max)
-- CAST(qp.query_plan AS nvarchar(max)) , you will know why I did like that.

linkserver_LN_N4TEST --make link server from empty server which getting and collecting all information from clients.
-- if we decide save record table at each client. That is not necessary.

checkperformance -- check and get performacnce top 10 data local version, store result tables at client database

checkperformancetest -- check and get performacnce top 10 data remote version, store result tables at dedicated server

showdelperftable -- collect performance tables

checkoldtable2 -- check and drop performacd tables any days before , like oracle AWR.

mssql_cpu_status_check_test.xlsm -- we can get checkCPU result from this excel and we can check this performance table result if

I need to check SQL information result.

we can set checkperformance or checkperformancetest as agent job evert 6 minutes.

That is all about simple way to monitor SQL server SQL performance.

============================
checkCPU:

CREATE PROCEDURE [dbo].[checkCPU]
AS
BEGIN
DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info);
SELECT TOP(500) SQLProcessUtilization AS [SQL Server Process CPU Utilization],
SystemIdle AS [System Idle Process],
100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization],
DATEADD(ms, -6 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time]
FROM (
SELECT record.value('(./Record/@id)[1]', 'int') AS record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int')
AS [SystemIdle],
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]',
'int')
AS [SQLProcessUtilization], [timestamp]
FROM (
SELECT [timestamp], CONVERT(xml, record) AS [record]
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
AND record LIKE '%<SystemHealth>%') AS x
) AS y
ORDER BY record_id DESC;
END
GO

checkoldtable2

CREATE PROCEDURE [dbo].[checkoldtable2] @days int
AS
BEGIN
declare @m int
declare @n int
declare @tmp nvarchar(MAX)

/*
if object_id('tempdb..#droptableList') is not null
drop table #droptableList
else
CREATE TABLE #droptableList(id int IDENTITY(1,1) NOT NULL,contents VARCHAR(35) NOT NULL)
SET NOCOUNT ON

INSERT #droptableList (contents) exec showdelperftable @days
set @m=(select count(*) from #droptableList)
--select * from #droptableList -- where id=1
set @n=1
while @n<=@m
begin

set @tmp=(select contents from #droptableList where id=@n)
--exec (@tmp)
print (@tmp+CHAR(5))

end
set @n=@n+1
*/

declare @droptableList TABLE (id int IDENTITY(1,1) NOT NULL,contents VARCHAR(35) NOT NULL)
SET NOCOUNT ON
INSERT @droptableList (contents) exec showdelperftable @days
set @m=(select count(*) from @droptableList)
set @n=1
while @n<=@m
begin

set @tmp=(select contents from @droptableList where id=@n)
exec (@tmp)
--print (@tmp+CHAR(5))
set @n=@n+1

end
end
GO

checkperfprmance

CREATE PROCEDURE [dbo].[checkperformance]
AS
BEGIN

DECLARE @SQL2 VARCHAR(MAX)
DECLARE @createtable VARCHAR(MAX)
DECLARE @inserttable VARCHAR(MAX)
DECLARE @inserttable2 VARCHAR(MAX)
DECLARE @@tablename2 VARCHAR(MAX)

SELECT @SQL2 = (select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':',''))
--set @createtable='create table perf_'+@SQL2+' (dbname varchar(20),logSizeMB float,logSpaceUsedPct float,Status int);'

set @createtable='create table perf_'+@SQL2+' ([SQL_TEXT] [nvarchar](max),[execution_count] [numeric](18, 0),[avg_worker_time] [numeric](18, 0),[avg_elapsed_time] [numeric](18, 0),[avg_physical_reads] [numeric](18, 0),[avg_logical_reads] [numeric](18, 0),[query_plan] [xml]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];'

exec(@createtable)

--set @inserttable='insert into perf_'+@SQL2+' EXEC ("DBCC SQLPERF(LOGSPACE)")'

set @inserttable='insert into perf_'+@SQL2+' EXEC [dbo].[getperformance]'

--set @inserttable2=(select replace(@inserttable,'"',''''))

set @inserttable2=(select replace(@inserttable,'"',''''))

exec(@inserttable2)

END
GO

checkperformancetest

CREATE PROCEDURE [dbo].[checkperformancetest]
AS
BEGIN

DECLARE @SQL2 VARCHAR(MAX)
DECLARE @createtable VARCHAR(MAX)
DECLARE @inserttable VARCHAR(MAX)
DECLARE @inserttable2 VARCHAR(MAX)
DECLARE @@tablename2 VARCHAR(MAX)

SELECT @SQL2 = (select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':',''))
--set @createtable='create table perf_'+@SQL2+' (dbname varchar(20),logSizeMB float,logSpaceUsedPct float,Status int);'

set @createtable='create table perf_'+@SQL2+' ([SQL_TEXT] [nvarchar](max),[execution_count] [numeric](18, 0),[avg_worker_time] [numeric](18, 0),[avg_elapsed_time] [numeric](18, 0),[avg_physical_reads] [numeric](18, 0),[avg_logical_reads] [numeric](18, 0),[query_plan] [xml]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];'

exec(@createtable)
--set @inserttable='insert into perf_'+@SQL2+' EXEC ("DBCC SQLPERF(LOGSPACE)")'
-----set @inserttable='insert into perf_'+@SQL2+' EXEC [dbo].[getperformance]'

set @inserttable='insert into perf_'+@SQL2+' SELECT sql_text,execution_count,avg_worker_time,avg_elapsed_time,avg_physical_reads,avg_logical_reads, CAST(query_plan AS xml) query_plan FROM openquery([LN_N4TEST],''exec [dbo].[getperformancetest]'')'

--set @inserttable2=(select replace(@inserttable,'"',''''))
set @inserttable2=(select replace(@inserttable,'"',''''))
exec(@inserttable2)
END
GO

getperformancetest

CREATE PROCEDURE [dbo].[getperformancetest]
AS
BEGIN

SELECT TOP 10
Left(SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1
, ((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1), 200) AS "SQL_TEXT"
, qs.execution_count
, qs.total_worker_time /qs.execution_count/1000 as avg_worker_time
, qs.total_elapsed_time /qs.execution_count /1000 as avg_elapsed_time
, qs.total_physical_reads /qs.execution_count as avg_physical_reads
, qs.total_logical_reads /qs.execution_count as avg_logical_reads
--, qp.query_plan
, CAST(qp.query_plan AS nvarchar(max)) query_plan

FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE qs.last_execution_time > dateadd(minute, -1, getdate())
ORDER BY qs.total_elapsed_time/qs.execution_count DESC

END
GO

showdelperftable

CREATE PROCEDURE [dbo].[showdelperftable] @days int
AS
BEGIN

select 'drop table dbo.'+name from sys.tables where name like 'perf_%' and create_date < dateadd(day, @days, getdate())
order by name asc,create_date asc

END

GO

A simple way to monitor SQL server SQL performance.的更多相关文章

  1. SQL Server SQL分页查询

    SQL Server SQL分页查询的几种方式 目录 0.    序言 1.    TOP…NOT IN… 2.    ROW_NUMBER() 3.    OFFSET…FETCH 4.    执行 ...

  2. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports

    http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...

  3. 对SQL Server SQL语句进行优化的10个原则

    1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大 ...

  4. .NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)

    原文:.NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1) 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数 ...

  5. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  6. 13Microsoft SQL Server SQL 高级事务,锁,游标,分区

    Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...

  7. [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]列名 'user1' 无效

    唉,还是自己对php执行sql语句运用不熟练.... 我的错误代码是这样的,(解决办法在最后) $re=sqlsrv_query($conn, "select * from visitor ...

  8. [ERROR]pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问

    环境: Windows 2012 R2 SQL Server 2014 通过MSSQL查询数据库服务器时间,报错如下: pyodbc.ProgrammingError: (', '[42000] [M ...

  9. SQL Server SQL性能优化之--数据库在“简单”参数化模式下,自动参数化SQL带来的问题

    数据库参数化的模式 数据库的参数化有两种方式,简单(simple)和强制(forced),默认的参数化默认是“简单”,简单模式下,如果每次发过来的SQL,除非完全一样,否则就重编译它(特殊情况会自动参 ...

随机推荐

  1. 27.openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽

    openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽 from openpyxl import Workbook,load_workbook from openpyxl.dra ...

  2. Java环境准备

    电脑重装系统了,所以需要重新配置环境变量. 首先必备工具:jak.eclipse.maven.tomcat 首先配置Java运行环境. 在系统环境变量中新建变量JAVA_HOME:jdk所在的路径,P ...

  3. mysql-5.7.9-winx64遇坑记

    昨天在mysql5.0上导入sql文件时,一直卡在一个地方报错,也没仔细分析,认为应该是mysql版本太低不支持这个语法而已.遂决心下载一个最新版本的mysql,却浑然不知前面无数的坑已经埋伏好了在等 ...

  4. Keil uVision4的简单使用

    1. 项目创建 打开安装好的Keil uVision4可以看到如下界面 选择New uVision Project来创建一个新项目 选择项目存放的位置并为项目文件命名 选择要进行模拟仿真的设备(此处以 ...

  5. 基于JGraphT实现的路径探寻

    基于JGraphT实现的路径探寻 业务中提出基于内存,探寻的两点间的有向以及无向路径,多点间的最小子图等需求,以下记录使用JGraphT的实现过程. GraphT是免费的Java类库,提供数学图论对象 ...

  6. c++ beep 演奏一次质量不高的天空之城

    beep函数用法: beep(HZ,time); hz是发出多少赫兹声音,time是发声时间(ms) 话不多说,上代码 #include <cstdio> #include <win ...

  7. VS2015发布WEB项目

    第一步:在打开的VS2015中,右击项目,在弹出的对话框中,点击“发布”. 第二步:配置发布的WEB项目. 1.为要发布的项目起个名称,一般和项目名称相同. 2.选择以文件系统发布. 3.设置相关配置 ...

  8. 基于 HTML5 WebGL + WebVR 的 3D 虚实现实可视化系统

    前言 2019 年 VR, AR, XR, 5G, 工业互联网等名词频繁出现在我们的视野中,信息的分享与虚实的结合已经成为大势所趋,5G 是新一代信息通信技术升级的重要方向,工业互联网是制造业转型升级 ...

  9. Ubuntu18.04 一次性升级Python所有库

    pip是什么 pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 升级pip版本 默认Ubuntu自带的pip (pip 9.0.1)是基于Pytho ...

  10. AVR单片机教程——小结

    本文隶属于AVR单片机教程系列.   第一期挺让我失望的,是我太菜,没有把想讲的都讲出来.经常写了很多,然后一点一点删掉,最后就没多少了. 而且感觉难度不合适,处于很尴尬的位置.讲得简单,难的丢给库, ...