您的位置:首页 > 数据库

通过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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐