文章出處

什么?有個 SQL 執行了 8 秒!

哪里出了問題?臣妾不知道啊,得找 DBA 啊。

DBA 人呢?離職了!!擦!!!

程序員在無處尋求幫助時,就得想辦法自救,努力讓自己變成 "偽 DBA"。

索引

  1. SQL Server 安裝的是什么版本
  2. Windows 操作系統是什么版本
  3. SQL Server 是什么時候安裝的
  4. 服務器主機名是什么
  5. 硬件服務器是誰制造的
  6. 服務器硬件是什么配置
  7. 服務器的 CPU 有幾個核
  8. 服務器的 CPU 是什么型號
  9. SQL Server 啟動了哪些服務
  10. 查看指定服務運行狀態
  11. SQL Server 的 IP 地址信息
  12. SQL Server 監聽了哪些 TCP 端口
  13. SQL Server 配置項信息
  14. 當前連接的 Session 有多少
  15. 每個數據庫上的 Session 數量是多少
  16. 按主機查詢 Session 數量
  17. 哪個 IP 地址上建立的連接最多
  18. 查看 Table 的基本屬性
  19. 查看 Table 的數據行數

SQL Server 安裝的是什么版本

SELECT @@VERSION AS [SQL Server Version];

版本號說明:

 Version 

 Product 

 Release Date 

 10.0.1600 

 SQL Server 2008 RTM 

 August 7, 2008 

 10.0.2531

 SQL Server 2008 SP1 RTM 

 April 7, 2009

 10.0.4000 

 SQL Server 2008 SP2 RTM

 September 29, 2010 

 10.0.5500

 SQL Server 2008 SP3 RTM

 October 6, 2011

 10.0.6000

 SQL Server 2008 SP4 RTM

 September 30, 2014

 10.50.1600 

 SQL Server 2008 R2 RTM

 April 21, 2010

 10.50.2500

 SQL Server 2008 R2 SP1 RTM 

 July 11, 2011

 10.50.4000

 SQL Server 2008 R2 SP2 RTM

 July 26, 2012

 10.50.6000

 SQL Server 2008 R2 SP3 RTM

 September 26, 2014

 11.0.2100

 SQL Server 2012 RTM

 March 6, 2012

 11.0.3000

 SQL Server 2012 SP1 RTM

 November 6, 2012

 11.0.5058

 SQL Server 2012 SP2 RTM

 June 10, 2014

 12.0.2000

 SQL Server 2014 RTM

 April 1, 2014

縮略語說明:

 Version 

 Description 

 CTP 

 Community Technology Preview (Beta Release)  

 RC

 Release Candidate 

 RTM 

 Released To Manufacturing

 CU

 Cumulative Update

 SP

 Service Pack

 GDR

 General Distribution Release

 QFE

 Quick Fix Engineering

比如,下面查到的版本號:

  • Build Version : 11.00.3153
  • File Version : 2011.110.3153.0

可以對應到 2977326 MS14-044: Description of the security update for SQL Server 2012 Service Pack 1 (GDR) August 12, 2014

Windows 操作系統是什么版本

SELECT windows_release
    ,windows_service_pack_level
    ,windows_sku
    ,os_language_version
FROM sys.dm_os_windows_info WITH (NOLOCK)
OPTION (RECOMPILE);

其中 windows_release 中的版本號代表著:

 Version 

 Windows 

 6.3 

 Windows 8.1 or Windows Server 2012 R2  

 6.2

 Windows 8 or Windows Server 2012 

 6.1 

 Windows 7 or Windows Server 2008 R2

 6.0

 Windows Vista or Windows Server 2008

 5.2

 Windows XP or Windows Server 2003

其中 windows_sku 代表著:

 SKU Code 

 Edition 

 4 

 Enterprise Edition  

 7

 Standard Edition 

 48 

 Professional Edition

SQL Server 2008 中沒有 sys.dm_os_windows_info,可以使用 @@VERSION 來看了。

SELECT @@VERSION AS [SQL Server Version];

SQL Server 是什么時候安裝的

SELECT @@SERVERNAME AS [Server Name]
    ,create_date AS [SQL Server Install Date]
FROM sys.server_principals WITH (NOLOCK)
WHERE NAME = N'NT AUTHORITY\SYSTEM'
    OR NAME = N'NT AUTHORITY\NETWORK SERVICE'
OPTION (RECOMPILE);

服務器主機名是什么

SELECT SERVERPROPERTY('MachineName') AS [MachineName]
    ,SERVERPROPERTY('ServerName') AS [ServerName]
    ,SERVERPROPERTY('InstanceName') AS [Instance]
    ,SERVERPROPERTY('IsClustered') AS [IsClustered]
    ,SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS [ComputerNamePhysicalNetBIOS]
    ,SERVERPROPERTY('Edition') AS [Edition]
    ,SERVERPROPERTY('ProductLevel') AS [ProductLevel]
    ,SERVERPROPERTY('ProductVersion') AS [ProductVersion]
    ,SERVERPROPERTY('ProcessID') AS [ProcessID]
    ,SERVERPROPERTY('Collation') AS [Collation]
    ,SERVERPROPERTY('IsFullTextInstalled') AS [IsFullTextInstalled]
    ,SERVERPROPERTY('IsIntegratedSecurityOnly') AS [IsIntegratedSecurityOnly];

硬件服務器是誰制造的

EXEC xp_readerrorlog 0, 1, N'Manufacturer'; 

服務器硬件是什么配置

SELECT cpu_count AS [Logical CPU Count]
    ,scheduler_count
    ,hyperthread_ratio AS [Hyperthread Ratio]
    ,cpu_count / hyperthread_ratio AS [Physical CPU Count]
    ,physical_memory_kb / 1024 AS [Physical Memory (MB)]
    ,committed_kb / 1024 AS [Committed Memory (MB)]
    ,committed_target_kb / 1024 AS [Committed Target Memory (MB)]
    ,max_workers_count AS [Max Workers Count]
    ,affinity_type_desc AS [Affinity Type]
    ,sqlserver_start_time AS [SQL Server Start Time]
    ,virtual_machine_type_desc AS [Virtual Machine Type]
FROM sys.dm_os_sys_info WITH (NOLOCK)
OPTION (RECOMPILE);

SQL Server 2008 R2 可以使用:

SELECT cpu_count AS [Logical CPU Count]
    ,hyperthread_ratio AS [Hyperthread Ratio]
    ,cpu_count / hyperthread_ratio AS [Physical CPU Count]
    ,physical_memory_in_bytes / 1048576 AS [Physical Memory (MB)]
    ,sqlserver_start_time
    ,affinity_type_desc
FROM sys.dm_os_sys_info WITH (NOLOCK)
OPTION (RECOMPILE);

服務器的 CPU 有幾個核

EXEC sys.xp_readerrorlog 0
    ,1
    ,N'detected'
    ,N'socket';

服務器的 CPU 是什么型號

EXEC xp_instance_regread N'HKEY_LOCAL_MACHINE'
    ,N'HARDWARE\DESCRIPTION\System\CentralProcessor\0'
    ,N'ProcessorNameString';

SQL Server 啟動了哪些服務

SELECT servicename
    ,process_id
    ,startup_type_desc
    ,status_desc
    ,last_startup_time
    ,service_account
    ,is_clustered
    ,cluster_nodename
    ,[filename]
FROM sys.dm_server_services WITH (NOLOCK)
OPTION (RECOMPILE);

查看指定服務運行狀態

EXEC master.dbo.xp_servicecontrol 'QUERYSTATE'
    ,'MSSQLServer'

EXEC master.dbo.xp_servicecontrol 'QUERYSTATE'
    ,'SQLServerAgent'

EXEC master.dbo.xp_servicecontrol 'QUERYSTATE'
    ,'SQLBrowser'

SQL Server 的 IP 地址信息

SELECT CONNECTIONPROPERTY('net_transport') AS net_transport
    ,CONNECTIONPROPERTY('protocol_type') AS protocol_type
    ,CONNECTIONPROPERTY('auth_scheme') AS auth_scheme
    ,CONNECTIONPROPERTY('local_net_address') AS local_net_address
    ,CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port
    ,CONNECTIONPROPERTY('client_net_address') AS client_net_address;

SELECT SERVERPROPERTY(N'MachineName') AS MachineName;

SQL Server 監聽了哪些 TCP 端口

SELECT listener_id
    ,ip_address
    ,is_ipv4
    ,port
    ,type_desc
    ,state_desc
    ,start_time
FROM sys.dm_tcp_listener_states WITH (NOLOCK)
ORDER BY listener_id
OPTION (RECOMPILE);

SQL Server 配置項信息

SELECT [name]
    ,value
    ,value_in_use
    ,minimum
    ,maximum
    ,[description]
    ,is_dynamic
    ,is_advanced
FROM sys.configurations WITH (NOLOCK)
ORDER BY [name]
OPTION (RECOMPILE);

通常會關注:

  • backup compression default :通常為 1;
  • cost threshold for parallelism 
  • clr enabled 
  • lightweight pooling :通常為 0;
  • max degree of parallelism
  • max server memory (MB)
  • optimize for ad hoc workloads :通常為 1;
  • priority boost :通常為 0;

當前連接的 Session 有多少

SELECT login_name
    ,[program_name]
    ,COUNT(session_id) AS [session_count]
FROM sys.dm_exec_sessions WITH (NOLOCK)
GROUP BY login_name
    ,[program_name]
ORDER BY COUNT(session_id) DESC
OPTION (RECOMPILE);

每個數據庫上的 Session 數量是多少

SELECT DB_NAME(dbid) AS DBName
    ,COUNT(dbid) AS NumberOfConnections
    ,loginame AS LoginName
FROM sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid
    ,loginame
ORDER BY 1, 2, 3;

按主機查詢 Session 數量

CREATE TABLE #tbl (
    spid INT
    ,ecid INT
    ,[status] VARCHAR(50)
    ,loginame VARCHAR(255)
    ,hostname VARCHAR(255)
    ,blk VARCHAR(50)
    ,dbname VARCHAR(255)
    ,cmd VARCHAR(255)
    ,request_id VARCHAR(255)
    )
GO

INSERT INTO #tbl
EXEC sp_who;

SELECT COUNT(0) AS CountByHostName
    ,hostname
FROM #tbl
GROUP BY hostname;

SELECT COUNT(0) AS CountByDBName
    ,dbname
FROM #tbl
GROUP BY dbname;

DROP TABLE #tbl
GO

哪個 IP 地址上建立的連接最多

SELECT ec.client_net_address
    ,es.[program_name]
    ,es.[host_name]
    ,es.login_name
    ,COUNT(ec.session_id) AS [connection count]
FROM sys.dm_exec_sessions AS es WITH (NOLOCK)
INNER JOIN sys.dm_exec_connections AS ec WITH (NOLOCK) ON es.session_id = ec.session_id
GROUP BY ec.client_net_address
    ,es.[program_name]
    ,es.[host_name]
    ,es.login_name
ORDER BY ec.client_net_address
    ,es.[program_name]
OPTION (RECOMPILE);

查看 Table 的基本屬性

SELECT [name]
    ,create_date
    ,lock_on_bulk_load
    ,is_replicated
    ,has_replication_filter
    ,is_tracked_by_cdc
    ,lock_escalation_desc
FROM sys.tables WITH (NOLOCK)
ORDER BY [name]
OPTION (RECOMPILE);

查看 Table 的數據行數

SELECT OBJECT_NAME(object_id) AS [ObjectName]
    ,SUM(Rows) AS [RowCount]
    ,data_compression_desc AS [CompressionType]
FROM sys.partitions WITH (NOLOCK)
WHERE index_id < 2 --ignore the partitions from the non-clustered index if any
    AND OBJECT_NAME(object_id) NOT LIKE N'sys%'
    AND OBJECT_NAME(object_id) NOT LIKE N'queue_%'
    AND OBJECT_NAME(object_id) NOT LIKE N'filestream_tombstone%'
    AND OBJECT_NAME(object_id) NOT LIKE N'fulltext%'
    AND OBJECT_NAME(object_id) NOT LIKE N'ifts_comp_fragment%'
    AND OBJECT_NAME(object_id) NOT LIKE N'filetable_updates%'
    AND OBJECT_NAME(object_id) NOT LIKE N'xml_index_nodes%'
GROUP BY object_id
    ,data_compression_desc
ORDER BY SUM(Rows) DESC
OPTION (RECOMPILE);

 

《人人都是 DBA》系列文章索引:

 序號 

 名稱 

1

 人人都是 DBA(I)SQL Server 體系結構

2

 人人都是 DBA(II)SQL Server 元數據

3

 人人都是 DBA(III)SQL Server 調度器

4

 人人都是 DBA(IV)SQL Server 內存管理

5

 人人都是 DBA(V)SQL Server 數據庫文件

6

 人人都是 DBA(VI)SQL Server 事務日志

7

 人人都是 DBA(VII)B 樹和 B+ 樹

8

 人人都是 DBA(VIII)SQL Server 頁存儲結構

9

 人人都是 DBA(IX)服務器信息收集腳本匯編

10

 人人都是 DBA(X)資源信息收集腳本匯編

11

 人人都是 DBA(XI)I/O 信息收集腳本匯編

12

 人人都是 DBA(XII)查詢信息收集腳本匯編

13

 人人都是 DBA(XIII)索引信息收集腳本匯編

14

 人人都是 DBA(XIV)存儲過程信息收集腳本匯編 

15

 人人都是 DBA(XV)鎖信息收集腳本匯編

本系列文章《人人都是 DBA》由 Dennis Gao 發表自博客園,未經作者本人同意禁止任何形式的轉載,任何自動或人為的爬蟲轉載行為均為耍流氓。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()