SQL Server 日常巡检 1
时间:2021-07-01 10:21:17
帮助过:41人阅读
- dbcc checkdb(test)
通过加tablock提高速度
[sql] view plain
copy
- dbcc checkdb(test) with tablock
2、数据库重命名、修改恢复模式、修改用户模式
[sql] view plain
copy
- ALTER DATABASE WC
- MODIFY NAME = test
-
-
- alter database test
- set recovery full
-
-
- alter database test
- set single_user
- with rollback after 10 seconds
-
-
- alter database wc
- set restricted_user
- with rollback immediate
-
-
- alter database wc
- set multi_user
- with no_wait
3、扩展数据库:增加文件组、增加文件、修改文件大小、修改文件的逻辑名称
[sql] view plain
copy
- ALTER DATABASE test
- ADD FILEGROUP WC_FG8
-
-
- ALTER DATABASE test
- ADD FILE
- (
- NAME = WC_FG8,
- FILENAME = ‘D:\WC_FG8.ndf‘,
- SIZE = 1mb,
- MAXSIZE = 10mb,
- FILEGROWTH = 1mb
- )
- TO FILEGROUP WC_FG8
-
-
- ALTER DATABASE test
- ADD LOG FILE
- (
- NAME = WC_LOG3,
- FILENAME = ‘D:\WC_FG3.LDF‘,
- SIZE = 1MB,
- MAXSIZE = 10MB,
- FILEGROWTH = 100KB
- )
-
-
- ALTER DATABASE test
- MODIFY FILE
- (
- NAME = ‘WC_FG8‘,
- SIZE = 2MB,
- MAXSIZE= 8MB,
- FILEGROWTH = 10%
- )
-
-
- ALTER DATABASE test
- MODIFY FILE
- (
- NAME = WC_LOG3,
- NEWNAME = WC_FG33
- )
4、移动文件
[sql] view plain
copy
- checkpoint
- go
-
- ALTER DATABASE WC
- SET OFFLINE
- go
-
-
- ALTER DATABASE WC
- MODIFY FILE
- (
- NAME = WC_fg8,
- FILENAME = ‘D:\WC\WC_FG8.NDF‘
- )
- go
-
-
-
-
- ALTER DATABASE WC
- SET ONLINE
5、设置默认文件组、只读文件组
[sql] view plain
copy
- ALTER DATABASE WC
- MODIFY FILEGROUP WC_FG8 DEFAULT
-
-
- ALTER DATABASE WC
- MODIFY FILEGROUP WC_FG8 READ_WRITE
6、收缩数据库、收缩文件
[sql] view plain
copy
- DBCC SHRINKDATABASE(‘test‘,
- 10
- )
-
-
- DBCC SHRINKDATABASE(‘test‘,
- 10,
- NOTRUNCATE
- )
-
-
- DBCC SHRINKDATABASE(‘test‘,
- 10,
- TRUNCATEONLY
- )
-
-
- DBCC SHRINKFILE(wc_fg8,
- 7
- )
-
- DBCC SHRINKFILE(wc_fg8,
- EMPTYFILE
- )
7、删除文件、删除文件组
[sql] view plain
copy
- DBCC SHRINKFILE(WC_FG8,EMPTYFILE)
-
-
- ALTER DATABASE test
- REMOVE FILE WC_FG8
-
-
-
-
- ALTER DATABASE test
- REMOVE FILEGROUP WC_FG8
-
- /*
- drop database www
- go
-
- create database www
- on primary
- (
- name = ‘www_data01‘,
- filename = ‘c:\www_data01.mdf‘
- ),
- (
- name = ‘www_data02‘,
- filename =‘c:\www_data02.ndf‘
- )
-
- log on
- (
- name = ‘www_log‘,
- filename = ‘c:\www_log.ldf‘
- )
- go
-
- use www
- go
-
- create table a(id int ,v varchar(10)) on [primary]
- go
-
- insert into a
- select OBJECT_ID,left(name,10) from sys.objects
- go
-
-
- insert into a
- select * from a
- go 10
-
-
- DBCC SHRINKFILE(www_data02,EMPTYFILE)
- go
-
- ALTER DATABASE www
- REMOVE FILE www_data02
- */
8、重新组织索引
[sql] view plain
copy
- ALTER INDEX [idx_temp_lock_id] ON [dbo].[temp_lock]
- REORGANIZE
- WITH ( LOB_COMPACTION = ON )
批量生成重组索引的语句
[sql] view plain
copy
- use test
- go
-
- select ‘DBCC INDEXDEFRAG(‘+db_name()+‘,‘+o.name+‘,‘+i.name + ‘);‘
-
-
-
-
-
- from sysindexes i
- inner join sysobjects o
- on i.id = o.id
- where o.xtype = ‘U‘
- and i.indid >0
- and charindex(‘WA_Sys‘,i.name) = 0
9、重新生成索引
[sql] view plain
copy
- ALTER INDEX [idx_temp_lock_id] ON [dbo].[temp_lock]
- REBUILD PARTITION = ALL
- WITH ( PAD_INDEX = OFF,
- STATISTICS_NORECOMPUTE = OFF,
- ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON,
- ONLINE = OFF,
- SORT_IN_TEMPDB = OFF )
10、更新统计信息
[sql] view plain
copy
- update statistics temp_lock(_WA_Sys_00000001_07020F21)
-
-
- update statistics temp_lock(_WA_Sys_00000001_07020F21)
- with sample 50 percent
-
-
- update statistics temp_lock(_WA_Sys_00000001_07020F21)
- with resample,
- norecompute
-
-
-
- update statistics temp_lock(idx_temp_lock_id)
- with fullscan
-
-
- update statistics txt
- with all
11、执行SQL Server代理作业
[sql] view plain
copy
- exec msdb.dbo.sp_start_job
- @job_name =N‘job_update_sql‘;
12、备份数据库(完整、差异、日志备份),这个在其他文章中已有详细描述,这里不再赘述。
SQL Server 日常巡检 1
标签:max 文件组 恢复 默认 size 相同属性 alt toc csdn