2009-10-07 42 views

回答

1

SQL Server Express中是否存在動態管理視圖?嘗試使用sys.dm_exec_query_stats來查看是否返回任何結果。如果是這樣的話,我可以執行一些存儲過程來優化性能。我給你一個程序的例子,如果你願意,我可以發佈一些其他程序。

CREATE PROCEDURE [ADMIN].[spExecutionTimeStats] 
       @DBName     AS SYSNAME 
    AS 
    -- +---------------------------------------------------------------------------------------------------------------- 
     -- ! O b j e c t   : ADMIN.spExecutionTimeStats 
     -- ! R e t u r n s  : NONE 
     -- ! P a r a m e t e r s : Name     DataType  Description 
     -- +      ======================= ============== ================================================== 
     -- !            @dbname         sysname 
     -- + --------------------------------------------------------------------------------------------------------------- 
     -- ! O b j e c t i v e : 
     -- + --------------------------------------------------------------------------------------------------------------- 
     -- ! S A M P L E S  : 
     -- !            EXEC ADMIN.spExecutionTimeStats 'DWH' 
     -- !            EXEC ADMIN.spExecutionTimeStats 'DWH_TOOLKIT' 
     -- !            EXEC ADMIN.spExecutionTimeStats 'DWH_HISTORY' 
     -- + --------------------------------------------------------------------------------------------------------------- 
     -- ! H i s t o r y  : 
     -- + --------------------------------------------------------------------------------------------------------------- 
     -- !      Date  Who What 
     -- +      ========== ===== ======================================================================== 
     -- !           2009-09-04 HAWI Initial version 
     -- +---------------------------------------------------------------------------------------------------------------- 

    --DECLARE @DBName AS SYSNAME='DWH' 
    DECLARE @SQL NVARCHAR(MAX) 

    SET @SQL='SELECT TOP 100 percent 
          ''' [email protected] +'''AS DBName, 
       OBJECT_SCHEMA_NAME(s.object_id, DB_ID(''' [email protected] +''')) AS Schema_name, 
       S.name, 
       D.execution_count, 
       D.total_physical_reads, 
       D.total_logical_reads, 
       d.total_logical_writes, 
       d.last_execution_time, 
       total_elapsed_time_s=convert(money,d.total_elapsed_time)/1000000, 
       max_elapsed_time_s=convert(money,D.max_elapsed_time)/1000000, 
       last_elapsed_time_s=convert(money,d.last_elapsed_time)/1000000, 
       min_elapsed_time_s=convert(money,D.min_elapsed_time)/1000000, 
       avg_time_s=(convert(money,d.total_elapsed_time)/d.execution_count)/1000000, 
       d.sql_handle as proc_handle, 
       stmnt.* 
    FROM ' + @dbname + '.sys.procedures s 
       INNER JOIN ' + @dbname + '.sys.dm_exec_procedure_stats d 
          ON s.object_id = d.object_id 
       LEFT JOIN 
           (
            SELECT TOP 100 PERCENT 
            QS.sql_handle,   
            ROW_NUMBER() OVER(PARTITION BY qs.sql_handle ORDER BY statement_start_offset) AS statement_no,  
            qs.execution_count, 
            qs.total_physical_reads, 
            qs.total_logical_reads, 
            qs.total_logical_writes, 
            qs.last_execution_time, 
            sql_total_elapsed_time_s=convert(money,qs.total_elapsed_time)/1000000, 
            sql_max_elapsed_time_s=convert(money,qs.max_elapsed_time)/1000000, 
            sql_last_elapsed_time_s=convert(money,qs.last_elapsed_time)/1000000, 
            sql_min_elapsed_time_s=convert(money,qs.min_elapsed_time)/1000000, 
            sql_avg_time_s=(convert(money,qs.total_elapsed_time)/qs.execution_count)/1000000,    
            SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1, 
             ((CASE statement_end_offset 
              WHEN -1 THEN DATALENGTH(st.text) 
              ELSE QS.statement_end_offset END 
              - QS.statement_start_offset)/2) + 1) AS statement_text 
            FROM ' + @dbname + '.sys.dm_exec_query_stats AS QS 
            CROSS APPLY ' + @dbname + '.sys.dm_exec_sql_text(QS.sql_handle) as ST 
           ) AS stmnt 
          ON d.sql_handle=stmnt.sql_handle 
    WHERE 
       convert(money,d.total_elapsed_time)/d.execution_count/1000000>10 
       OR 
       d.last_elapsed_time > (d.total_elapsed_time/d.execution_count)*1.15 
    ORDER BY avg_time_s DESC;' 

    PRINT @SQL; 
    EXEC sp_executeSQL @SQL; 
    GO 
+0

哈坎非常有趣,我想看看其他腳本,如果你有:) – Trapias 2012-11-27 11:44:24

相關問題