DBCC CheckDB command 用法

DBCC CheckDB command 的使用分为三步

1,verify AUTO_UPDATE_STATISTICS_ASYNC option 关闭

--verify the AUTO_UPDATE_STATISTICS_ASYNC option is set to OFF
select db.name, db.is_auto_update_stats_async_on
from sys.databases db

2 设置DB 处于single user 访问模式

--set single user can access database
alter database db_study
set single_user with ROLLBACK IMMEDIATE

Syntax

ALTER DATABASE  database_name
SET <db_user_access_option>
[ WITH <termination> ] 

<db_user_access_option> ::=
    { SINGLE_USER | RESTRICTED_USER | MULTI_USER }
<termination>  ::=
{
    ROLLBACK AFTER integer [ SECONDS ]
  | ROLLBACK IMMEDIATE
  | NO_WAIT
}

<db_user_access_option> ::=

Controls user access to the database.

SINGLE_USER

Specifies that only one user at a time can access the database. If SINGLE_USER is specified and there are other users connected to the database the ALTER DATABASE statement will be blocked until all users disconnect from the specified database. To override this behavior, see the WITH <termination> clause.

The database remains in SINGLE_USER mode even if the user that set the option logs off. At that point, a different user, but only one, can connect to the database.

Before you set the database to SINGLE_USER, verify the AUTO_UPDATE_STATISTICS_ASYNC option is set to OFF. When set to ON, the background thread used to update statistics takes a connection against the database, and you will be unable to access the database in single-user mode. To view the status of this option, query the is_auto_update_stats_async_on column in the sys.databases catalog view. If the option is set to ON, perform the following tasks:

  1. Set AUTO_UPDATE_STATISTICS_ASYNC to OFF.
  2. Check for active asynchronous statistics jobs by querying the sys.dm_exec_background_job_queue dynamic management view.

If there are active jobs, either allow the jobs to complete or manually terminate them by using KILL STATS JOB.

RESTRICTED_USER

RESTRICTED_USER allows for only members of the db_owner fixed database role and dbcreator and sysadmin fixed server roles to connect to the database, but does not limit their number. All connections to the database are disconnected in the timeframe specified by the termination clause of the ALTER DATABASE statement. After the database has transitioned to the RESTRICTED_USER state, connection attempts by unqualified users are refused.

MULTI_USER

All users that have the appropriate permissions to connect to the database are allowed.

The status of this option can be determined by examining the user_access column in the sys.databases catalog view or the UserAccess property of the DATABASEPROPERTYEX function.

WITH <termination> ::=

Specifies when to roll back incomplete transactions when the database is transitioned from one state to another. If the termination clause is omitted, the ALTER DATABASE statement waits indefinitely if there is any lock on the database. Only one termination clause can be specified, and it follows the SET clauses.

ROLLBACK AFTER integer [SECONDS] | ROLLBACK IMMEDIATE


Specifies whether to roll back after the specified number of seconds or immediately.

NO_WAIT

Specifies that if the requested database state or option change cannot complete immediately without waiting for transactions to commit or roll back on their own, the request will fail.

3,exec dbcc checkdb command

The Repair_rebuild option attempts to repair errors only when losing data is not possible.

--exec dbcc checkdb
dbcc checkdb(‘db_study‘,repair_rebuild)

The Repair_Allow_Data_Loss option attempts to repair all errors, including those in which data will likely be lost(the option name was carefully chosen).

--exec dbcc checkdb command
dbcc checkdb(‘db_study‘,repair_allow_data_loss)

Syntax

DBCC CHECKDB
[
    [ ( database_name | database_id | 0
        [ , NOINDEX
        | , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]
    ) ]
    [ WITH
        {
            [ ALL_ERRORMSGS ]
            [ , EXTENDED_LOGICAL_CHECKS ]
            [ , NO_INFOMSGS ]
            [ , TABLOCK ]
            [ , ESTIMATEONLY ]
            [ , { PHYSICAL_ONLY | DATA_PURITY } ]
        }
    ]
]

MSDN URL:https://msdn.microsoft.com/zh-cn/library/ms176064.aspx

4,将db的access mode 切换到multiple user

-- switch to multiple user access mode
alter database [SIS_DataWarehouseV2_SIS]
set MULTI_USER with ROLLBACK IMMEDIATE

5, 查看 msdb.dbo.suspect_pages

url:https://msdn.microsoft.com/en-us/library/ms191301(v=sql.110).aspx

参考文档:

https://msdn.microsoft.com/en-us/library/bb522682.aspx

时间: 2025-01-02 13:38:17

DBCC CheckDB command 用法的相关文章

SQL Server 修复数据库 相关 脚本 之 DBCC CHECKDB 用法 来自同事分享

DBCC CHECKDB 用法详解, 手工修复数据库 1. 快速修复 DBCC CHECKDB ('数据库名',REPAIR_FAST) 2.重建索引并修复 DBCC CHECKDB ('数据库名',REPAIR_REBUILD) 3.如果必要允许丢失数据库修复 DBCC CHECKDB ('数据库名',REPAIR_ALLOW_DATA_LOSS) 如果出现错误: 未处理修复语句,数据库需要处于单用户模式下. 可以先启用单用户模式, 方法如下执行存储过程: Use master go sp_d

DBCC CHECKDB用法 手工修复数据库

快速修复DBCC CHECKDB ('数据库名', REPAIR_FAST)      重建索引并修复DBCC CHECKDB ('数据库名', REPAIR_REBUILD)如果必要允许丢失数据修复DBCC CHECKDB ('数据库名'', REPAIR_ALLOW_DATA_LOSS) 如果出现错误:未处理修复语句.数据库需处于单用户模式下. 可以先启用单用户模式,方法如下执行存储过程: Use mastergosp_dboption 数据库名, single, true --更改成单用户

DBCC CHECKDB

DBCC CHECKDB 算是管理员们最常用的命令也是必须要知道的命令了.定期的检查及问题的修复都是比较重要的!!下面介绍一下 DBCC CHECKDB 的一些基本用法. DBCC CHECKDB 完成两项任务: 检查数据库里有没有损坏发生. 尽力修复数据库损坏,使数据库能够被重新正常访问. DBCC CHECK 做了些什么: 检查一些关键的系统表 对数据库运行DBCC CHECKALLOC 对数据库运行DBCC CHECKCATALOG 验证数据库中每个索引视图的内容 验证数据库中servic

MS SqlServer DBCC CHECKDB 数据库/表修复

MS Sql Server 提供了很多数据库修复的命令,当数据库质疑或是有的无法完成读取时可以尝试这些修复命令. 1. DBCC CHECKDB  重启服务器后,在没有进行任何操作的情况下,在SQL查询分析器中执行以下SQL进行数据库的修复,修复数据库存在的一致性错误与分配错误. use master declare @databasename varchar(255) set @databasename='需要修复的数据库实体的名称' exec sp_dboption @databasenam

MS Sql Server 数据库或表修复(DBCC CHECKDB)

MS Sql Server 提供了很多数据库修复的命令,当数据库质疑或是有的无法完成读取时可以尝试这些修复命令.  1. DBCC CHECKDB  重启服务器后,在没有进行任何操作的情况下,在SQL查询分析器中执行以下SQL进行数据库的修复,修复数据库存在的一致性错误与分配错误. use master declare @databasename varchar(255) set @databasename='需要修复的数据库实体的名称' exec sp_dboption @databasena

【2017-05-22】WebForm内置对象:Application和ViewState、Repeater的Command用法

一.内置对象 1.Application 存贮在服务器端,占用服务器内存生命周期:永久 所有人访问的都是这一个对象 传值:传的是object类型可以传对象. string s =TextBox1.Text; Application["aaa"]=s; 取值: if(Application["aaa"]!=null) { Label1.Text=Application["aaa"].toString(); } 2.ViewState ViewSta

SQL Server 影响dbcc checkdb的 8 种因素

第一种: 数据库的大小. 第二种: IO系统的速度. 第三种: 当前CPU的负荷. 第四种: 当前数据库的并发修改量.因为并发修改量越大维护数据库快照的成本就越高,dbcc 的过程中要创建快照,所以. 第五种: 存放tempdb数据库硬盘的速度.dbcc 的过程中会有一些中间结果,而这些结果全放在内存里是不合适也是不可能的.所以有 用到tempdb的空间. 第六种: 数据库中对象的类型,不同类型的对象检查时要的时间也是不一样的.费时的有非聚集索引,计算列,off_row_lob,Server_b

SQL Server dbcc checkdb 修复

默认dbcc checkdb 只做数据库的检测数据库是否完好.不会主动做数据库的修复,要修复数据库,需要数据库设单用模式. 1.repair_allow_data_loss  可能导致数据丢失. 2.Repair_fast  未执行任何修复操作. 3.repair_rebuild  快速修复. 1.repaire_allow_data_loss修复的三个过程: 第一步.将标记为不可访问的页面重新标记为可以访问,就如同这些错误从来都没有发生过一样. 第二步.用常规的日志恢复技术恢复数据库. 第三步

SQL Server 对dbcc checkdb的优化

方法 1. 在运行dbcc checkdb前对数据库进行快照(事务是一致的),dbcc 对快照进行检测,dbcc完成后删除快照. 做快照的目的是为了不要让dbcc 申请太多的锁,从这里可以看出dbcc 可以在多用户模式下进. 方法 2. dbcc还可通知多线程技术,为运行加速.这样做的副作用就是,在这个时间段里正常的连接可能拿不到足够的资源. 方法 3. 可以用physical_only选项,这个选项可以较小的开销检查数据库的物理一致性.并检查出危及数据安全的残缺页,硬件故障.