一、常见操作
1、TRUNCATE TABLE 语句
删除表数据
TRUNCATE TABLE语句比DELET删除表中的所有行更快。从逻辑上讲,TRUNCATE TABLE它类似于DELETE没有WHERE子句的语句。
TRUNCATE TABLE语句从表中删除所有行,但表结构及其列,约束,索引等保持不变。要删除表及其数据,可以使用该DROP TABLE语句,但是需谨慎操作。
语法
TRUNCATE TABLE的基本语法:
use dbname
TRUNCATE TABLE table_name;
delete 的循环删除的基本语法:
declare @onecount int set @onecount = 100000print getdate()while 1=1
begindelete top (@onecount ) from [eifiredataNewTemp].[dbo].[所有工程当前报警信息]if(@@ROWCOUNT < @onecount) break;
end
TRUNCATE TABLE vs DELETE
尽管DELETE和TRUNCATE TABLE似乎具有相同的效果,但是它们的工作方式不同。这是这两个语句之间的一些主要区别:
- TRUNCATE TABLE语句删除并重新创建表,并使任何自动增量值都重置为其初始值(通常为1);
- DELETE可让您根据可选WHERE子句过滤要删除的行,TRUNCATE TABLE而不支持WHERE子句则仅删除所有行;
- TRUNCATE TABLE与DELETE相比,它更快并且使用的系统资源更少,因为DELETE扫描表以生成受影响的行数,然后逐行删除行,并为每个删除的行在数据库日志中记录一个条目,而TRUNCATE TABLE只删除所有行而不提供任何其他信息。
**提示:**如果你只是想删除所有的行,并重新创建整个表,使用TRUNCATE TABLE。如果你想删除基于特定条件的行的数量有限,或者您不想要重置自动递增值,则使用DELETE。
2、查询数据表数据大小
语法格式:
USE [eifiredataNewTemp];
GO
-- 数据库空间使用情况
EXEC sp_spaceused;-- 查下文件空间使用情况
SELECT file_id, name,[文件大小(MB)] = size / 128.,[未使用空间(MB)] = (size - FILEPROPERTY(name, N'SpaceUsed')) / 128.
FROM sys.database_files-- 表空间使用情况
DECLARE @tb_size TABLE(name sysname,rows int,size varchar(100),data_size varchar(100),INDEX_size varchar(100),unused_size varchar(100)
);
INSERT @tb_size
EXEC sp_msforeachtable '
sp_spaceused ''?''
'
SELECT * FROM @tb_size order by rows desc, data_size desc
查询效果:
3、占用CPU过高的SQL
3.1 查看当前的数据库用户连接有多少
USE masterGO--如果要指定数据库就把注释去掉SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 AND DB_NAME([dbid])='eifireBigData' --and status = 'suspended'--SELECT COUNT(*) FROM [sys].[dm_exec_sessions] WHERE [session_id]>50
查询效果:
3.2 查看各项指标是否正常,是否有阻塞,选取了前10个最耗CPU时间的会话
SELECT TOP 10
[session_id],
[request_id],
[start_time] AS '开始时间',
[status] AS '状态',
[command] AS '命令',
dest.[text] AS 'sql语句',
DB_NAME([database_id]) AS '数据库名',
[blocking_session_id] AS '正在阻塞其他会话的会话ID',
[wait_type] AS '等待资源类型',
[wait_time] AS '等待时间',
[wait_resource] AS '等待的资源',
[reads] AS '物理读次数',
[writes] AS '写次数',
[logical_reads] AS '逻辑读次数',
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND DB_NAME(der.[database_id])='gposdb'
ORDER BY [cpu_time] DESC
查询结果:
3.3 查看具体的SQL语句,需要在SSMS里选择以文本格式显示结果
--在SSMS里选择以文本格式显示结果
SELECT TOP 10
dest.[text] AS 'sql语句'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50
ORDER BY [cpu_time] DESC
查询结果:
3.4 查看CPU数和user scheduler数和最大工作线程数,检查worker是否用完也可以排查CPU占用情况
--查看CPU数和user scheduler数目SELECT cpu_count,scheduler_count FROM sys.dm_os_sys_info--查看最大工作线程数SELECT max_workers_count FROM sys.dm_os_sys_info
查询结果:
3.5 查看worker是否用完,如果达到最大线程数的时候需要检查blocking
SELECT
scheduler_address,
scheduler_id,
cpu_id,
status,
current_tasks_count,
current_workers_count,active_workers_count
FROM sys.dm_os_schedulers
查询结果:
对照表:各种CPU和SQLSERVER版本组合自动配置的最大工作线程数
CPU数 32位计算机 64位计算机 <=4 256 512 8 288 576 16 352 704 32 480 960
3.6 查看会话中有多少个worker在等待
SELECT TOP 10[session_id],[request_id],[start_time] AS '开始时间',[status] AS '状态',[command] AS '命令',dest.[text] AS 'sql语句', DB_NAME([database_id]) AS '数据库名',[blocking_session_id] AS '正在阻塞其他会话的会话ID',der.[wait_type] AS '等待资源类型',[wait_time] AS '等待时间',[wait_resource] AS '等待的资源',[dows].[waiting_tasks_count] AS '当前正在进行等待的任务数',[reads] AS '物理读次数',[writes] AS '写次数',[logical_reads] AS '逻辑读次数',[row_count] AS '返回结果行数'FROM sys.[dm_exec_requests] AS der INNER JOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type]=[dows].[wait_type]CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDER BY [cpu_time] DESC
查询结果:
3.7 查看ASYNC_NETWORK_IO等待
(注:比如我当前执行了查询SalesOrderDetail_test表100次,由于表数据非常多,所以SSMS需要把SQLSERVER执行的结果慢慢的取走,造成了ASYNC_NETWORK_IO等待)
3.8 查询CPU占用高的语句
SELECT TOP 10total_worker_time/execution_count AS avg_cpu_cost, plan_handle,execution_count,(SELECT SUBSTRING(text, statement_start_offset/2 + 1,(CASE WHEN statement_end_offset = -1THEN LEN(CONVERT(nvarchar(max), text)) * 2ELSE statement_end_offsetEND - statement_start_offset)/2)FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY [avg_cpu_cost] DESC
查询结果:
3.9 查询缺失索引
SELECT DatabaseName = DB_NAME(database_id),[Number Indexes Missing] = count(*)
FROM sys.dm_db_missing_index_details
GROUP BY DB_NAME(database_id)
ORDER BY 2 DESC;
SELECT TOP 10 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) , avg_user_impact, TableName = statement, [EqualityUsage] = equality_columns , [InequalityUsage] = inequality_columns, [Include Cloumns] = included_columns
FROM sys.dm_db_missing_index_groups g
INNER JOIN sys.dm_db_missing_index_group_stats s ON s.group_handle = g.index_group_handle
INNER JOIN sys.dm_db_missing_index_details d ON d.index_handle = g.index_handle
ORDER BY [Total Cost] DESC;