我是想对我开发的程序进行优化。因为有很多。谢谢了!
QQ:472231001
9 个解决方案
#1
比如在本机执行SQL。
SELECT * FROM TABLE1;SELECT * FROM TABLE2 ; EXECUTE PRO1 'XXX';
我得到的信息是:
1,SELECT * FROM TABLE1 -- 1.2S
2,SELECT * FROM TABLE2 -- 0.12S
3, EXECUTE PRO1 'XXX' --4S
DETAILS: SELECT * FROM TABLE3 -- 1S
SELECT * FROM TABLE4 -- 3S
比较详细的执行代码和时间。
SELECT * FROM TABLE1;SELECT * FROM TABLE2 ; EXECUTE PRO1 'XXX';
我得到的信息是:
1,SELECT * FROM TABLE1 -- 1.2S
2,SELECT * FROM TABLE2 -- 0.12S
3, EXECUTE PRO1 'XXX' --4S
DETAILS: SELECT * FROM TABLE3 -- 1S
SELECT * FROM TABLE4 -- 3S
比较详细的执行代码和时间。
#2
给你几段代码,2005以后适用,不用开发程序:
1、查找目前SQL Server所执行的SQL语法,并展示资源情况:
2、展示耗时查询:
3、当前进程及其语句:
4、存储过程执行情况:
5、开销较大的查询:
1、查找目前SQL Server所执行的SQL语法,并展示资源情况:
SELECT s2.dbid ,
DB_NAME(s2.dbid) AS [数据库名] ,
--s1.sql_handle ,
( SELECT TOP 1
SUBSTRING(s2.text, statement_start_offset / 2 + 1,
( ( CASE WHEN statement_end_offset = -1
THEN ( LEN(CONVERT(NVARCHAR(MAX), s2.text))
* 2 )
ELSE statement_end_offset
END ) - statement_start_offset ) / 2 + 1)
) AS [语句] ,
execution_count AS [执行次数] ,
last_execution_time AS [上次开始执行计划的时间] ,
total_worker_time AS [自编译以来执行所用的 CPU 时间总量(微秒)] ,
last_worker_time AS [上次执行计划所用的 CPU 时间(微秒)] ,
min_worker_time AS [单次执行期间曾占用的最小 CPU 时间(微秒)] ,
max_worker_time AS [单次执行期间曾占用的最大 CPU 时间(微秒)] ,
total_logical_reads AS [总逻辑读] ,
last_logical_reads AS [上次逻辑读] ,
min_logical_reads AS [最少逻辑读] ,
max_logical_reads AS [最大逻辑读] ,
total_logical_writes AS [总逻辑写] ,
last_logical_writes AS [上次逻辑写] ,
min_logical_writes AS [最小逻辑写] ,
max_logical_writes AS [最大逻辑写]
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
WHERE s2.objectid IS NULL
ORDER BY last_worker_time DESC
2、展示耗时查询:
-- List expensive queries
DECLARE @MinExecutions int;
SET @MinExecutions = 5
SELECT EQS.total_worker_time AS TotalWorkerTime
,EQS.total_logical_reads + EQS.total_logical_writes AS TotalLogicalIO
,EQS.execution_count As ExeCnt
,EQS.last_execution_time AS LastUsage
,EQS.total_worker_time / EQS.execution_count as AvgCPUTimeMiS
,(EQS.total_logical_reads + EQS.total_logical_writes) / EQS.execution_count
AS AvgLogicalIO
,DB.name AS DatabaseName
,SUBSTRING(EST.text
,1 + EQS.statement_start_offset / 2
,(CASE WHEN EQS.statement_end_offset = -1
THEN LEN(convert(nvarchar(max), EST.text)) * 2
ELSE EQS.statement_end_offset END
- EQS.statement_start_offset) / 2
) AS SqlStatement
-- Optional with Query plan; remove comment to show, but then the query takes !!much longer time!!
--,EQP.[query_plan] AS [QueryPlan]
FROM sys.dm_exec_query_stats AS EQS
CROSS APPLY sys.dm_exec_sql_text(EQS.sql_handle) AS EST
CROSS APPLY sys.dm_exec_query_plan(EQS.plan_handle) AS EQP
LEFT JOIN sys.databases AS DB
ON EST.dbid = DB.database_id
WHERE EQS.execution_count > @MinExecutions
AND EQS.last_execution_time > DATEDIFF(MONTH, -1, GETDATE())
ORDER BY AvgLogicalIo DESC
,AvgCPUTimeMiS DESC
3、当前进程及其语句:
-- Current processes and their SQL statements
SELECT PRO.loginame AS LoginName
,DB.name AS DatabaseName
,PRO.[status] as ProcessStatus
,PRO.cmd AS Command
,PRO.last_batch AS LastBatch
,PRO.cpu AS Cpu
,PRO.physical_io AS PhysicalIo
,SES.row_count AS [RowCount]
,STM.[text] AS SQLStatement
FROM sys.sysprocesses AS PRO
INNER JOIN sys.databases AS DB
ON PRO.dbid = DB.database_id
INNER JOIN sys.dm_exec_sessions AS SES
ON PRO.spid = SES.session_id
CROSS APPLY sys.dm_exec_sql_text(PRO.sql_handle) AS STM
WHERE PRO.spid >= 50 -- Exclude system processes
ORDER BY PRO.physical_io DESC
,PRO.cpu DESC;
4、存储过程执行情况:
-- Stored Procedure Execution Statistics
SELECT ISNULL(DBS.name, '') AS DatabaseName
,OBJECT_NAME(EPS.object_id, EPS.database_id) AS ObjectName
,EPS.cached_time AS CachedTime
,EPS.last_elapsed_time AS LastElapsedTime
,EPS.execution_count AS ExecutionCount
,EPS.total_worker_time / EPS.execution_count AS AvgWorkerTime
,EPS.total_elapsed_time / EPS.execution_count AS AvgElapsedTime
,(EPS.total_logical_reads + EPS.total_logical_writes)
/ EPS.execution_count AS AvgLogicalIO
FROM sys.dm_exec_procedure_stats AS EPS
LEFT JOIN sys.databases AS DBS
ON EPS.database_id = DBS.database_id
ORDER BY AvgWorkerTime DESC;
5、开销较大的查询:
/*
开销较大的查询
*/
SELECT ss.SUM_execution_count ,
t.text ,
ss.SUM_total_elapsed_time ,
ss.sum_total_worker_time ,
ss.sum_total_logical_reads ,
ss.sum_total_logical_writes
FROM ( SELECT S.plan_handle ,
SUM(s.execution_count) SUM_Execution_count ,
SUM(s.total_elapsed_time) SUM_total_elapsed_time ,
SUM(s.total_worker_time) SUM_total_worker_time ,
SUM(s.total_logical_reads) SUM_total_logical_reads ,
SUM(s.total_logical_writes) SUM_total_logical_writes
FROM sys.dm_exec_query_stats s
GROUP BY S.plan_handle
) AS ss
CROSS APPLY sys.dm_exec_sql_text(ss.plan_handle) t
ORDER BY sum_total_logical_reads DESC
#3
#4
这个怎么实时监控呢。就是像它自带的那个样子大概一样。
只是我要多它的执行时间和存储过程之类的的详细代码。
只是我要多它的执行时间和存储过程之类的的详细代码。
#5
只要不是代码非常长,这些部分代码里面是会显式所执行的代码的。只要不重启SQLServer,这些代码就可以监控到从SQLServer启动到现在所耗用的资源。DMV、data colleter这些都是监控SQLServer性能的新工具。
#6
我希望要的是时监控,不是到SQL里去查询什么记录。
就是现在SQL的运行状态。大概可以和SQL PROFILE一样的功能。只是需求简单点。
代码和运行时间。时实的监控。谢谢。
就是现在SQL的运行状态。大概可以和SQL PROFILE一样的功能。只是需求简单点。
代码和运行时间。时实的监控。谢谢。
#7
晕,算了,估计你还没懂我说什么,让别人跟你说吧
#8
++
#9
不是有现成工具嘛 干嘛要自己写,sql有跟踪功能
#1
比如在本机执行SQL。
SELECT * FROM TABLE1;SELECT * FROM TABLE2 ; EXECUTE PRO1 'XXX';
我得到的信息是:
1,SELECT * FROM TABLE1 -- 1.2S
2,SELECT * FROM TABLE2 -- 0.12S
3, EXECUTE PRO1 'XXX' --4S
DETAILS: SELECT * FROM TABLE3 -- 1S
SELECT * FROM TABLE4 -- 3S
比较详细的执行代码和时间。
SELECT * FROM TABLE1;SELECT * FROM TABLE2 ; EXECUTE PRO1 'XXX';
我得到的信息是:
1,SELECT * FROM TABLE1 -- 1.2S
2,SELECT * FROM TABLE2 -- 0.12S
3, EXECUTE PRO1 'XXX' --4S
DETAILS: SELECT * FROM TABLE3 -- 1S
SELECT * FROM TABLE4 -- 3S
比较详细的执行代码和时间。
#2
给你几段代码,2005以后适用,不用开发程序:
1、查找目前SQL Server所执行的SQL语法,并展示资源情况:
2、展示耗时查询:
3、当前进程及其语句:
4、存储过程执行情况:
5、开销较大的查询:
1、查找目前SQL Server所执行的SQL语法,并展示资源情况:
SELECT s2.dbid ,
DB_NAME(s2.dbid) AS [数据库名] ,
--s1.sql_handle ,
( SELECT TOP 1
SUBSTRING(s2.text, statement_start_offset / 2 + 1,
( ( CASE WHEN statement_end_offset = -1
THEN ( LEN(CONVERT(NVARCHAR(MAX), s2.text))
* 2 )
ELSE statement_end_offset
END ) - statement_start_offset ) / 2 + 1)
) AS [语句] ,
execution_count AS [执行次数] ,
last_execution_time AS [上次开始执行计划的时间] ,
total_worker_time AS [自编译以来执行所用的 CPU 时间总量(微秒)] ,
last_worker_time AS [上次执行计划所用的 CPU 时间(微秒)] ,
min_worker_time AS [单次执行期间曾占用的最小 CPU 时间(微秒)] ,
max_worker_time AS [单次执行期间曾占用的最大 CPU 时间(微秒)] ,
total_logical_reads AS [总逻辑读] ,
last_logical_reads AS [上次逻辑读] ,
min_logical_reads AS [最少逻辑读] ,
max_logical_reads AS [最大逻辑读] ,
total_logical_writes AS [总逻辑写] ,
last_logical_writes AS [上次逻辑写] ,
min_logical_writes AS [最小逻辑写] ,
max_logical_writes AS [最大逻辑写]
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
WHERE s2.objectid IS NULL
ORDER BY last_worker_time DESC
2、展示耗时查询:
-- List expensive queries
DECLARE @MinExecutions int;
SET @MinExecutions = 5
SELECT EQS.total_worker_time AS TotalWorkerTime
,EQS.total_logical_reads + EQS.total_logical_writes AS TotalLogicalIO
,EQS.execution_count As ExeCnt
,EQS.last_execution_time AS LastUsage
,EQS.total_worker_time / EQS.execution_count as AvgCPUTimeMiS
,(EQS.total_logical_reads + EQS.total_logical_writes) / EQS.execution_count
AS AvgLogicalIO
,DB.name AS DatabaseName
,SUBSTRING(EST.text
,1 + EQS.statement_start_offset / 2
,(CASE WHEN EQS.statement_end_offset = -1
THEN LEN(convert(nvarchar(max), EST.text)) * 2
ELSE EQS.statement_end_offset END
- EQS.statement_start_offset) / 2
) AS SqlStatement
-- Optional with Query plan; remove comment to show, but then the query takes !!much longer time!!
--,EQP.[query_plan] AS [QueryPlan]
FROM sys.dm_exec_query_stats AS EQS
CROSS APPLY sys.dm_exec_sql_text(EQS.sql_handle) AS EST
CROSS APPLY sys.dm_exec_query_plan(EQS.plan_handle) AS EQP
LEFT JOIN sys.databases AS DB
ON EST.dbid = DB.database_id
WHERE EQS.execution_count > @MinExecutions
AND EQS.last_execution_time > DATEDIFF(MONTH, -1, GETDATE())
ORDER BY AvgLogicalIo DESC
,AvgCPUTimeMiS DESC
3、当前进程及其语句:
-- Current processes and their SQL statements
SELECT PRO.loginame AS LoginName
,DB.name AS DatabaseName
,PRO.[status] as ProcessStatus
,PRO.cmd AS Command
,PRO.last_batch AS LastBatch
,PRO.cpu AS Cpu
,PRO.physical_io AS PhysicalIo
,SES.row_count AS [RowCount]
,STM.[text] AS SQLStatement
FROM sys.sysprocesses AS PRO
INNER JOIN sys.databases AS DB
ON PRO.dbid = DB.database_id
INNER JOIN sys.dm_exec_sessions AS SES
ON PRO.spid = SES.session_id
CROSS APPLY sys.dm_exec_sql_text(PRO.sql_handle) AS STM
WHERE PRO.spid >= 50 -- Exclude system processes
ORDER BY PRO.physical_io DESC
,PRO.cpu DESC;
4、存储过程执行情况:
-- Stored Procedure Execution Statistics
SELECT ISNULL(DBS.name, '') AS DatabaseName
,OBJECT_NAME(EPS.object_id, EPS.database_id) AS ObjectName
,EPS.cached_time AS CachedTime
,EPS.last_elapsed_time AS LastElapsedTime
,EPS.execution_count AS ExecutionCount
,EPS.total_worker_time / EPS.execution_count AS AvgWorkerTime
,EPS.total_elapsed_time / EPS.execution_count AS AvgElapsedTime
,(EPS.total_logical_reads + EPS.total_logical_writes)
/ EPS.execution_count AS AvgLogicalIO
FROM sys.dm_exec_procedure_stats AS EPS
LEFT JOIN sys.databases AS DBS
ON EPS.database_id = DBS.database_id
ORDER BY AvgWorkerTime DESC;
5、开销较大的查询:
/*
开销较大的查询
*/
SELECT ss.SUM_execution_count ,
t.text ,
ss.SUM_total_elapsed_time ,
ss.sum_total_worker_time ,
ss.sum_total_logical_reads ,
ss.sum_total_logical_writes
FROM ( SELECT S.plan_handle ,
SUM(s.execution_count) SUM_Execution_count ,
SUM(s.total_elapsed_time) SUM_total_elapsed_time ,
SUM(s.total_worker_time) SUM_total_worker_time ,
SUM(s.total_logical_reads) SUM_total_logical_reads ,
SUM(s.total_logical_writes) SUM_total_logical_writes
FROM sys.dm_exec_query_stats s
GROUP BY S.plan_handle
) AS ss
CROSS APPLY sys.dm_exec_sql_text(ss.plan_handle) t
ORDER BY sum_total_logical_reads DESC
#3
#4
这个怎么实时监控呢。就是像它自带的那个样子大概一样。
只是我要多它的执行时间和存储过程之类的的详细代码。
只是我要多它的执行时间和存储过程之类的的详细代码。
#5
只要不是代码非常长,这些部分代码里面是会显式所执行的代码的。只要不重启SQLServer,这些代码就可以监控到从SQLServer启动到现在所耗用的资源。DMV、data colleter这些都是监控SQLServer性能的新工具。
#6
我希望要的是时监控,不是到SQL里去查询什么记录。
就是现在SQL的运行状态。大概可以和SQL PROFILE一样的功能。只是需求简单点。
代码和运行时间。时实的监控。谢谢。
就是现在SQL的运行状态。大概可以和SQL PROFILE一样的功能。只是需求简单点。
代码和运行时间。时实的监控。谢谢。
#7
晕,算了,估计你还没懂我说什么,让别人跟你说吧
#8
++
#9
不是有现成工具嘛 干嘛要自己写,sql有跟踪功能