[杂] 一些常用的SQL归类之一

整理了一大坨的常用SQL语句,以方便自己需要用的时候查找。

查看锁

SELECT  [request_session_id] ,

        c.[program_name] ,

        DB_NAME(c.[dbid]) AS dbname ,

        [resource_type] ,

        [request_status] ,

        [request_mode] ,

        [resource_description] ,

        OBJECT_NAME(p.[object_id]) AS objectname ,

        p.[index_id]

FROM    sys.[dm_tran_locks] AS a

        LEFT JOIN sys.[partitions] AS p ON a.[resource_associated_entity_id] = p.[hobt_id]

        LEFT JOIN sys.[sysprocesses] AS c ON a.[request_session_id] = c.[spid]

WHERE   c.[dbid] = 19

ORDER BY [request_session_id] ,

        [resource_type]

查看Page上的数据

DBCC PAGE([dbname|dbid],1,14464,3)

 

读取错误日志

sp_readerrorlog

查看请求,查看连接,查看Session执行的语句

SELECT  *

FROM    sys.dm_exec_requests



SELECT  *

FROM    sys.dm_exec_connections

ORDER BY net_transport



DBCC INPUTBUFFER(69)





SELECT  [request_session_id] ,

        c.[program_name] ,

        DB_NAME(c.[dbid]) AS dbname ,

        [resource_type] ,

        [request_status] ,

        [request_mode] ,

        [resource_description] ,

        OBJECT_NAME(p.[object_id]) AS objectname ,

        p.[index_id]

FROM    sys.[dm_tran_locks] AS a

        LEFT JOIN sys.[partitions] AS p ON a.[resource_associated_entity_id] = p.[hobt_id]

        LEFT JOIN sys.[sysprocesses] AS c ON a.[request_session_id] = c.[spid]

WHERE   c.[dbid] = 5

ORDER BY [request_session_id] ,

        [resource_type]

查看Block

SELECT  *

FROM    sys.sysprocesses WITH ( NOLOCK )

WHERE   blocked <> 0

最耗CPU的语句查询

SELECT TOP ( 10 )

        qs.last_execution_time ,

        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 ,

        execution_count ,

        total_worker_time / 1000 AS total_worker_time_ms ,

        ( total_worker_time / 1000 ) / execution_count AS avg_worker_time_ms ,

        total_logical_reads ,

        total_logical_reads / execution_count AS avg_logical_reads ,

        total_elapsed_time / 1000 AS total_elapsed_time_ms ,

        ( total_elapsed_time / 1000 ) / execution_count AS avg_elapsed_time_ms ,

        qp.query_plan

FROM    sys.dm_exec_query_stats qs

        CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st

        CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp

WHERE   qs.last_worker_time > 3000

ORDER BY qs.last_execution_time DESC

参考Blog:

关于Block http://www.cnblogs.com/fygh/archive/2012/01/17/2324926.html

关于性能 http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html

常用语句 http://www.cnblogs.com/kerrycode/p/3153321.html

未完待续……

你可能感兴趣的:(sql)