通过sql server动态管理试图查询性能数据
2010-09-07 14:41
483 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-520092929 1073786111 9 0 415 0;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:modern;
mso-font-pitch:fixed;
mso-font-signature:-520092929 1073806591 9 0 415 0;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:1627400839 -2147483648 8 0 66047 0;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:宋体;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
font-family:"Calibri","sans-serif";
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page WordSection1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.WordSection1
{page:WordSection1;}
-->
1 Buffer Cache Hit Ratio:
SELECT
(CAST(SUM(CASE
LTRIM(RTRIM(counter_name)) WHEN 'Buffer cache hit ratio' THEN
CAST(cntr_value AS
INTEGER) ELSE NULL END) AS FLOAT) /
CAST(SUM(CASE
LTRIM(RTRIM(counter_name)) WHEN 'Buffer cache hit ratio base'
THEN
CAST(cntr_value AS INTEGER) ELSE NULL END) AS FLOAT)) * 100
AS
BufferCacheHitRatio
FROM
sys.dm_os_performance_counters
WHERE
LTRIM(RTRIM([object_name]))
LIKE '%:Buffer Manager' AND
[counter_name] LIKE
'Buffer Cache Hit Ratio%'
2 SQLSERVER CPU
占有率
DECLARE @ts_now
BIGINT
SELECT @ts_now = cpu_ticks /
CONVERT(FLOAT, ms_ticks) FROM
sys.dm_os_sys_info
SELECT record_id,
DATEADD(ms, -1 * (@ts_now -
[timestamp]), GETDATE()) AS
EventTime,
SQLProcessUtilization,
SystemIdle,
100 - SystemIdle -
SQLProcessUtilization AS
OtherProcessUtilization
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 '% %')
AS x
) AS y
ORDER BY record_id DESC
3
Memory Manager
declare
@physical_memory_kb
int
SELECT
@physical_memory_kb
=
total_physical_memory_kb
FROM
sys
.
dm_os_sys_memory
select
*,
convert
(
decimal
,
cntr_value
)/
convert
(
decimal
,
@physical_memory_kb
)
as
'P'
FROM
sys
.
dm_os_performance_counters
where
object_name
like
'%Memory Manager %'
and
counter_name
like
'%server memory%'
Buffer
Manager
Free
pages
SELECT
*
FROM
sys
.
dm_os_performance_counters
WHERE
counter_name
like
'%free pages%'
and
object_name
like
'%Buffer Manager%'
Buffer
Manager
: Page
life
expectancy
SELECT
*
FROM
sys
.
dm_os_performance_counters
WHERE
counter_name
like
'%expectancy%'
and
object_name
like
'%Buffer Manager%'
查找表占用磁盘
sp_spaceused
查询各个磁盘分区的剩余空间:
Exec
master.dbo.xp_fixeddrives
查询数据库服务器各数据库日志文件的大小及利用率
DBCC SQLPERF(LOGSPACE)
开启
xp_cmdshell
--
允许配置高级选项
EXEC
sp_configure
'show advanced options'
,
1
GO
--
重新配置
RECONFIGURE
GO
--
启用
xp_cmdshell
EXEC
sp_configure
'xp_cmdshell'
,
1
GO
--
重新配置
RECONFIGURE
GO
获取磁盘空间的:
A
EXEC
XP_CMDSHELL
'fsutil volume diskfree e: '
需要开启
--
允许配置高级选项
EXEC
sp_configure
'show advanced options'
,
1
GO
--
重新配置
RECONFIGURE
GO
--
启用
xp_cmdshell
EXEC
sp_configure
'xp_cmdshell'
,
1
GO
--
重新配置
RECONFIGURE
GO
B
获取一个磁盘总容量
CREATE FUNCTION
dbo.GetDriveSize
(@driveletter
CHAR(1))
RETURNS NUMERIC(20)
BEGIN
DECLARE
@rs INTEGER, @fso INTEGER, @getdrive VARCHAR(13), @drv
INTEGER,
@drivesize
VARCHAR(20)
SET @getdrive = 'GetDrive("' +
@driveletter + '")'
EXEC @rs = sp_OACreate
'Scripting.FileSystemObject', @fso OUTPUT
IF @rs = 0
EXEC @rs = sp_OAMethod @fso, @getdrive, @drv
OUTPUT
IF @rs = 0
EXEC @rs = sp_OAGetProperty
@drv,'TotalSize', @drivesize OUTPUT
IF @rs<> 0
SET @drivesize = NULL
EXEC sp_OADestroy @drv
EXEC sp_OADestroy @fso
RETURN @drivesize
END
GO
SELECT
dbo.GetDriveSize('C')
获取每个磁盘空间情况
CREATE
PROCEDURE
sp_diskspace
AS
SET
NOCOUNT
ON
DECLARE
@hr
int
DECLARE
@fso
int
DECLARE
@drive
char
(
1)
DECLARE
@odrive
int
DECLARE
@TotalSize
varchar
(
20)
DECLARE
@MB
bigint
;
SET
@MB
=
1048576
CREATE
TABLE
#drives
(
ServerName
varchar
(
15),
drive
char
(
1)
PRIMARY
KEY
,
FreeSpace
int
NULL,
TotalSize
int
NULL,
FreespaceTimestamp
DATETIME
NULL)
INSERT
#drives
(
drive
,
FreeSpace
)
EXEC
master
.
dbo
.
xp_fixeddrives
EXEC
@hr
=
sp_OACreate
'Scripting.FileSystemObject'
,
@fso
OUT
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
DECLARE
dcur
CURSOR
LOCAL
FAST_FORWARD
FOR
SELECT
drive
from
#drives
ORDER
by
drive
OPEN
dcur
FETCH
NEXT
FROM
dcur
INTO
@drive
WHILE
@@FETCH_STATUS
=
0
BEGIN
EXEC
@hr
=
sp_OAMethod
@fso
,
'GetDrive'
,
@odrive
OUT
,
@drive
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
EXEC
@hr
=
sp_OAGetProperty
@odrive
,
'TotalSize'
,
@TotalSize
OUT
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@odrive
UPDATE
#drives
SET
TotalSize
=
@TotalSize
/
@MB
,
ServerName
=
host_name
(),
FreespaceTimestamp
=
(
GETDATE
())
WHERE
drive
=
@drive
FETCH
NEXT
FROM
dcur
INTO
@drive
END
CLOSE
dcur
DEALLOCATE
dcur
EXEC
@hr
=
sp_OADestroy
@fso
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
SELECT
ServerName
,
drive
,
TotalSize
as
'Total(MB)'
,
FreeSpace
as
'Free(MB)'
,
CAST
((
FreeSpace
/(
TotalSize
*
1.0))*
100.0 as
int
)
as
'Free(%)'
,
FreespaceTimestamp
FROM
#drives
ORDER
BY
drive
DROP
TABLE
#drives
RETURN
GO
用户名称,密码:
select
p
.
name
,
p
.
type_desc
,
p
.
default_database_name
,
s
.
status
,
s
.
name
,
s
.
password
,
s
.
dbname
,
s
.*
from
sys
.
server_principals
p
join
sys
.
syslogins
s
ON
p
.
sid
=
s
.
sid
where
p
.
type_desc
=
'SQL_LOGIN'
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-520092929 1073786111 9 0 415 0;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:modern;
mso-font-pitch:fixed;
mso-font-signature:-520092929 1073806591 9 0 415 0;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:1627400839 -2147483648 8 0 66047 0;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:宋体;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
font-family:"Calibri","sans-serif";
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page WordSection1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.WordSection1
{page:WordSection1;}
-->
1 Buffer Cache Hit Ratio:
SELECT
(CAST(SUM(CASE
LTRIM(RTRIM(counter_name)) WHEN 'Buffer cache hit ratio' THEN
CAST(cntr_value AS
INTEGER) ELSE NULL END) AS FLOAT) /
CAST(SUM(CASE
LTRIM(RTRIM(counter_name)) WHEN 'Buffer cache hit ratio base'
THEN
CAST(cntr_value AS INTEGER) ELSE NULL END) AS FLOAT)) * 100
AS
BufferCacheHitRatio
FROM
sys.dm_os_performance_counters
WHERE
LTRIM(RTRIM([object_name]))
LIKE '%:Buffer Manager' AND
[counter_name] LIKE
'Buffer Cache Hit Ratio%'
2 SQLSERVER CPU
占有率
DECLARE @ts_now
BIGINT
SELECT @ts_now = cpu_ticks /
CONVERT(FLOAT, ms_ticks) FROM
sys.dm_os_sys_info
SELECT record_id,
DATEADD(ms, -1 * (@ts_now -
[timestamp]), GETDATE()) AS
EventTime,
SQLProcessUtilization,
SystemIdle,
100 - SystemIdle -
SQLProcessUtilization AS
OtherProcessUtilization
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 '% %')
AS x
) AS y
ORDER BY record_id DESC
3
Memory Manager
declare
@physical_memory_kb
int
SELECT
@physical_memory_kb
=
total_physical_memory_kb
FROM
sys
.
dm_os_sys_memory
select
*,
convert
(
decimal
,
cntr_value
)/
convert
(
decimal
,
@physical_memory_kb
)
as
'P'
FROM
sys
.
dm_os_performance_counters
where
object_name
like
'%Memory Manager %'
and
counter_name
like
'%server memory%'
Buffer
Manager
Free
pages
SELECT
*
FROM
sys
.
dm_os_performance_counters
WHERE
counter_name
like
'%free pages%'
and
object_name
like
'%Buffer Manager%'
Buffer
Manager
: Page
life
expectancy
SELECT
*
FROM
sys
.
dm_os_performance_counters
WHERE
counter_name
like
'%expectancy%'
and
object_name
like
'%Buffer Manager%'
查找表占用磁盘
sp_spaceused
查询各个磁盘分区的剩余空间:
Exec
master.dbo.xp_fixeddrives
查询数据库服务器各数据库日志文件的大小及利用率
DBCC SQLPERF(LOGSPACE)
开启
xp_cmdshell
--
允许配置高级选项
EXEC
sp_configure
'show advanced options'
,
1
GO
--
重新配置
RECONFIGURE
GO
--
启用
xp_cmdshell
EXEC
sp_configure
'xp_cmdshell'
,
1
GO
--
重新配置
RECONFIGURE
GO
获取磁盘空间的:
A
EXEC
XP_CMDSHELL
'fsutil volume diskfree e: '
需要开启
--
允许配置高级选项
EXEC
sp_configure
'show advanced options'
,
1
GO
--
重新配置
RECONFIGURE
GO
--
启用
xp_cmdshell
EXEC
sp_configure
'xp_cmdshell'
,
1
GO
--
重新配置
RECONFIGURE
GO
B
获取一个磁盘总容量
CREATE FUNCTION
dbo.GetDriveSize
(@driveletter
CHAR(1))
RETURNS NUMERIC(20)
BEGIN
DECLARE
@rs INTEGER, @fso INTEGER, @getdrive VARCHAR(13), @drv
INTEGER,
@drivesize
VARCHAR(20)
SET @getdrive = 'GetDrive("' +
@driveletter + '")'
EXEC @rs = sp_OACreate
'Scripting.FileSystemObject', @fso OUTPUT
IF @rs = 0
EXEC @rs = sp_OAMethod @fso, @getdrive, @drv
OUTPUT
IF @rs = 0
EXEC @rs = sp_OAGetProperty
@drv,'TotalSize', @drivesize OUTPUT
IF @rs<> 0
SET @drivesize = NULL
EXEC sp_OADestroy @drv
EXEC sp_OADestroy @fso
RETURN @drivesize
END
GO
SELECT
dbo.GetDriveSize('C')
获取每个磁盘空间情况
CREATE
PROCEDURE
sp_diskspace
AS
SET
NOCOUNT
ON
DECLARE
@hr
int
DECLARE
@fso
int
DECLARE
@drive
char
(
1)
DECLARE
@odrive
int
DECLARE
@TotalSize
varchar
(
20)
DECLARE
@MB
bigint
;
SET
@MB
=
1048576
CREATE
TABLE
#drives
(
ServerName
varchar
(
15),
drive
char
(
1)
PRIMARY
KEY
,
FreeSpace
int
NULL,
TotalSize
int
NULL,
FreespaceTimestamp
DATETIME
NULL)
INSERT
#drives
(
drive
,
FreeSpace
)
EXEC
master
.
dbo
.
xp_fixeddrives
EXEC
@hr
=
sp_OACreate
'Scripting.FileSystemObject'
,
@fso
OUT
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
DECLARE
dcur
CURSOR
LOCAL
FAST_FORWARD
FOR
SELECT
drive
from
#drives
ORDER
by
drive
OPEN
dcur
FETCH
NEXT
FROM
dcur
INTO
@drive
WHILE
@@FETCH_STATUS
=
0
BEGIN
EXEC
@hr
=
sp_OAMethod
@fso
,
'GetDrive'
,
@odrive
OUT
,
@drive
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
EXEC
@hr
=
sp_OAGetProperty
@odrive
,
'TotalSize'
,
@TotalSize
OUT
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@odrive
UPDATE
#drives
SET
TotalSize
=
@TotalSize
/
@MB
,
ServerName
=
host_name
(),
FreespaceTimestamp
=
(
GETDATE
())
WHERE
drive
=
@drive
FETCH
NEXT
FROM
dcur
INTO
@drive
END
CLOSE
dcur
DEALLOCATE
dcur
EXEC
@hr
=
sp_OADestroy
@fso
IF
@hr
<>
0 EXEC
sp_OAGetErrorInfo
@fso
SELECT
ServerName
,
drive
,
TotalSize
as
'Total(MB)'
,
FreeSpace
as
'Free(MB)'
,
CAST
((
FreeSpace
/(
TotalSize
*
1.0))*
100.0 as
int
)
as
'Free(%)'
,
FreespaceTimestamp
FROM
#drives
ORDER
BY
drive
DROP
TABLE
#drives
RETURN
GO
用户名称,密码:
select
p
.
name
,
p
.
type_desc
,
p
.
default_database_name
,
s
.
status
,
s
.
name
,
s
.
password
,
s
.
dbname
,
s
.*
from
sys
.
server_principals
p
join
sys
.
syslogins
s
ON
p
.
sid
=
s
.
sid
where
p
.
type_desc
=
'SQL_LOGIN'
相关文章推荐
- 庭审管理中动态显示今天起7日内的数据,动态查询,并根据已有数据判断新增和修改时的时间冲突问题(2)
- SQL Server动态管理视图,性能调校语句
- oracle 数据字典和动态性能视图数据 与用户权限角色查询
- 【Sql Server学习】Sql Server数据查询、数据更新、视图、权限管理
- 记一次 SQL SERVER 数据库查询性能优化 千万级数据
- SQL Server查询数据性能
- 通过loadrunner 11常规通用的C语言API类型的Vuser 方式,测试验证MySQL数据库插入、查询、修改、删除数据性能脚本实例
- Hibernate中通过HQL/JPQL查询的方式实现动态数据获取
- 优化SQL Server数据逻辑 提高查询性能
- SQL Server 通过JOB来定期获取数据库相关性能数据—JOB介绍
- 通过DMVS采集并存储SQL Server性能计数器数据
- SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?
- 数据库管理之数据字典和动态性能视图(常用视图)
- 【转】SQL Server 2008数据仓库查询性能
- 解决web程序获取不到his接口数据(通过查询his SQL server 视图)
- SQL SERVER 动态SQL实现 多条件查询数据
- 【Java EE 学习 77 上】【数据采集系统第九天】【通过AOP实现日志管理】【通过Spring石英调度动态生成日志表】【日志分表和查询】
- 庭审管理中动态显示今天起7日内的数据,动态查询,并根据已有数据判断新增和修改时的时间冲突问题
- 11G Concept 第六章翻译 Data Dictionary and Dynamic Performance Views(数据字典和动态性能试图)
- 优化SQL Server数据逻辑 提高查询性能