zoukankan      html  css  js  c++  java
  • SQL SERVER DBCC命令参考

    【资源来源网络和Sql Server联机丛书】

    网络主要资源来源:http://boyi55.blog.51cto.com/4345/27300

    每次从Sql Server联机丛书不能一目了然的看到DBCC命令的概要,特整理DBCC命令

    【资料】

    1、

    DBCC TRACEON

    DBCC TRACEOFF

    分别意思:启用、禁用指定的跟踪标记

    对于数据库死锁,通常可以通过TRACE FLAG 1204、1205、1206,检查ERRORLOG里面的输出,和分析SQLTRACE的执行上下文判断死锁问题的来由。
    TRACEON函数的第三个参数设置为-1,表示不单单针对当前connection,
    而是针对所有包括未来建立的connection。这样,才够完全,否则只是监视当前已经建立的数据库连接了。
    执行下面的话可以把死锁记录到Errorlog中:
    DBCC TRACEON (1204, 3605, -1)
    GO
    DBCC TRACESTATUS(-1)
    GO
     
    说明:
    260:打印关于扩展存储过程动态链接库的版本信息
    444:停止auto-parameterization
    1200:输出锁信息
    1204:传回参与死锁的SQL SERVER相关程序之运行数据
    1211:停止lock escalation(锁升级)
    1252:显示动态选择锁的相关信息


    2528:通过  DBCC  CHECKDB、DBCC  CHECKFILEGROUP  和  DBCC  CHECKTABLE  禁用对象的并行检查。

    默认情况下,并行度由查询处理器自动确定。最大并行度的配置方式与并行查询相同。
    有关更多信息,请参见  max  degree  of  parallelism  选项。  
    通常情况下,应将并行  DBCC  保留为启用状态。执行  DBCC  CHECKDB  时,
    查询处理器重新评估和自动调整并行度,并检查每个表或一批表。
    有时,检查可能在服务器处于实际空闲状态时进行。

    如果管理员知道在检查结束前负荷将加大, 可能希望手工减小或禁用并行度。
    但是,禁用并行检查会导致数据库的总体性能降低。降低并行度将增加必须扫描的事务日志量。
    这反过来增加了对  tempdb  空间的需求,并导致  dbcc  完成检查所需的时间非线性增加。
    如果运行  DBCC  时启用了  TABLOCK  功能并关闭了并行度,则表可能被锁定更长时间。 


    3205:默认情况下,如果磁带驱动器支持硬件压缩,则  DUMP  或  BACKUP  语句会使用该功能。
    利用此跟踪标记,可以禁用磁带驱动程序的硬件压缩。
    本项在要与不支持压缩的其它站点或磁带驱动器交换磁带时有用。


    3604:将trace结果输出到前端
    3605:要求DBCC的输出放到SQL server ERROR LOG
    8602:停止索引提示功能
    8722:停止join group等最优化提示功能
    8755:停止锁提示功能
    8780:停止最优化超时配置,强制做完整的最优化动作

    2、

    DBCC Page

    例子:
    dbcc traceon(3604)
    dbcc page(northwind,1,100,1)
    /*查询northwind 的数据的第101个页面的信息*/


    /*

    DBCC Page ({dbid|dbname},filenum,pagenum[,printopt])
    ?
    具体参数描述如下:
    dbid: 包含页面的数据库ID
    dbname:包含页面的数据库的名称
    filenum:包含页面的文件编号
    pagenum:文件内的页面
    printopt:可选的输出选项;选用其中一个值:
    0:默认值,输出缓冲区的标题和页面标题
    1:输出缓冲区的标题、页面标题(分别输出每一行),以及行偏移量表
    2:输出缓冲区的标题、页面标题(整体输出页面),以及行偏移量表
    3:输出缓冲区的标题、页面标题(分别输出每一行),以及行偏移量表;每一行后跟分别列出的它的列值
    */

    3、

    DBCC checkalloc

    /*

    检查指定数据库的系统表内和表间的一致性
    checkalloc是检查指定数据库,看其所有正确分配的页和尚未分配的页的情况。
    若未指定数据库名,则checkalloc检查当前数据库。checkalloc会返回已分配的和使用的空间数量。
    checkalloc的缺省模式为nofix,要使用fix选项,必须把数据库置于单用户模式。

    */

    DBCC CHECKALLOC
        ( 'database_name'
                
    [ , NOINDEX
                    |
                    { REPAIR_ALLOW_DATA_LOSS
                        | REPAIR_FAST
                        | REPAIR_REBUILD
                    } ]
        )    [ WITH { [ ALL_ERRORMSGS | NO_INFOMSGS ]
                        [ , [ TABLOCK ] ]
                        [ , [ ESTIMATEONLY ] ]
                    }
            ]

    例子:
    DBCC checkalloc(northwind)

    4、

    DBCC checkcatalog

    /*
    检查批定数据库的系统表内和系统表间的一致性
    */

    DBCC CHECKCATALOG
        ( 'database_name'
        )
        [ WITH NO_INFOMSGS ]

    例子:

    DBCC checkcatalog(northwind)

    5、

    DBCC checkconstraints
    /*
    检查指定表上的指定约束或所有约束的完整性
    DBCC CHECKCONSTRAINTS
         [('table_name'|'constraint_name'
    )]
         [WITH {ALL_ERRORMSGS|ALL_CONSTRAINTS}]
    DBCC CHECKCONSTRAINTS在某个数据库中,检测某些特定的约束或者全部约束的一致性。
    DBCC CHECKCONSTRAINTS总是在当前数据库的上下文环境中执行。
      注意,DBCC CHECKCONSTRAINTS并不进行磁盘或者文件级别的一致性检测;
    它只是确保外键定义的一致性,同时检测约束——仅仅是确认数据有效。
    如果你希望检测磁盘上表和索引的一致性,
    你应该执行DBCC CHECKDB或者在所有的表上执行DBCC CHECKALLOC和 DBCC CHECKTABLE的组合。
    */

    例子:

    DBCC checkconstraints(products)

    6、

    DBCC checkdb
    /*
    检查数据库中的所有对象的分配和结构完整性
    DBCC CHECKDB
        ( 'database_name'
                
    [ , NOINDEX
                    | { REPAIR_ALLOW_DATA_LOSS
                        | REPAIR_FAST
                        | REPAIR_REBUILD
                        } ]
        )    [ WITH { [ ALL_ERRORMSGS ]
                        [ , [ NO_INFOMSGS ] ]
                        [ , [ TABLOCK ] ]
                        [ , [ ESTIMATEONLY ] ]
                        [ , [ PHYSICAL_ONLY ] ]
                        }
            ]
    */

    7、

    DBCC cleantable
    /*
    回收alter table drop column语句 删除可变长度列或text列后的存储空间
    DBCC CLEANTABLE
        ( { 'database_name' | database_id }
            , { 'table_name' | table_id | 'view_name' | view_id }
            [ , batch_size ]
        )
    */

    8、

    DBCC dbreindex
    /*
    重建指定数据库的一个或多个索引
    dbreindex ('table_name' [, index_name [, fillfactor ]]) [WITH NO_INFOMSGS]
    */

    应用:

    http://www.cnblogs.com/rickie/articles/90498.html

    9、

    DBCC indexdefrag
    /*
    对表或视图上的索引和非聚集索引进行碎片整理
    DBCC INDEXDEFRAG
        ( { database_name | database_id | 0 }
            , { table_name | table_id | 'view_name' | view_id }
            , { index_name | index_id }
        )    [ WITH NO_INFOMSGS ]
    */

    DBCC INDEXDEFRAG: In SQL Server 2000, Microsoft introduced DBCC INDEXDEFRAG to help reduce logical disk fragmentation. When this command runs, it reduces fragmentation and does not lock tables, allowing users to access the table when the defragmentation process is running. Unfortunately, this command doesn't do a great job of logical defragmentation.

    The only way to truly reduce logical fragmentation is to rebuild your table's indexes. While this will remove all fragmentation, unfortunately it will lock the table, preventing users from accessing it during this process. This means that you will need to find a time when this will not present a problem to your users.

    Of course, if you are unable to find a time to reindex your indexes, then running DBCC INDEXDEFRAG is better than doing nothing.

    Example:

    DBCC INDEXDEFRAG (Database_Name, Table_Name, Index_Name)

    10、

    DBCC pintable/DBCC unpintable
    --将表数据驻留在内存中或撤销驻留 在内存中的数据
    --pintable (database_id, table_id)

    11、

    DBCC shrinkdatabase
    --收缩指定数据库的数据文件和日志文件大小
    --shrinkdatabase ({dbid | 'dbname'}, [freespace_percentage [, {NOTRUNCATE | TRUNCATEONLY}]])

    12、

    DBCC shrinkfile
    --收缩相关数据库的指定数据文件和日志文件大小
    --shrinkfile ({fileid | 'filename'}, [compress_size [, {NOTRUNCATE | TRUNCATEONLY | EMPTYFILE}]])

    13、

    DBCC dllname(free)
    --在内存中制裁指定的扩展想念过程动态链接库(DLL)
    sp_helpextended proc
    --查询当前内存中的扩展存储过程动态链接库

    14、

    DBCC dropcleanbuffers
    --从缓冲池中删除所有缓冲区
    /*
    使用 DBCC DROPCLEANBUFFERS 测试带有冷高速缓存的查询,而不用关闭和重新启动服务器
    */

    DBCC DROPCLEANBUFFERS: Use this command to remove all the data from SQL Server's data cache (buffer) between performance tests to ensure fair testing. Keep in mind that this command only removes clean buffers, not dirty buffers. Because of this, before running the DBCC DROPCLEANBUFFERS command, you may first want to run the CHECKPOINT command first. Running CHECKPOINT will write all dirty buffers to disk. And then when you run DBCC DROPCLEANBUFFERS, you can be assured that all data buffers are cleaned out, not just the clean ones.

    15、

    DBCC freeproccache
    --从过程缓冲区删除所有元素
    --清理所有数据库的过程高速缓存

    16、

    DBCC inputButter
    --显示从客户机发送到服务器的最后一个语句

    /*

    DBCC INPUTBUFFER (spid)

    参数

    spid

    sp_who 系统存储过程的输出中所显示的用户连接系统进程 ID (SPID)。

    */

    17、

    DBCC opentran
    --查询某个数据库执行时间最久的事务,由哪个程序拥有

    DBCC OPENTRAN
        (    { 'database_name' | database_id} )
            
    [ WITH TABLERESULTS
                [ , NO_INFOMSGS ]
            ]

    例子:

    DBCC OPENTRAN('pubs')

    18、

    DBCC show_statistics
    --显示指定表上的指定目前的当前分布统计信息

    DBCC SHOW_STATISTICS ( table_or_indexed_view_name , target )
    [ WITH [ NO_INFOMSGS ] < option > [ , n ] ]
    < option > :: =
        STAT_HEADER | DENSITY_VECTOR | HISTOGRAM | STATS_STREAM

    --资源参见msdn在线

    例子:

    Dbcc show_statistics ("pubs.dbo.authors" ,aunmind);

    19、

    DBCC showcontig
    --显示指定表的数据和索引的碎片信息

    DBCC SHOWCONTIG
        [    ( { table_name | table_id | view_name | view_id }
                [ , index_name | index_id ]
            )
        ]
        [ WITH { ALL_INDEXES
                    | FAST [ , ALL_INDEXES ]
                    | TABLERESULTS [ , { ALL_INDEXES } ]
                    [ , { FAST | ALL_LEVELS } ]
                }
        ]

    通过分析这些结果可以知道该表的索引是否需要重构。如下描述了每一行的意义:

    信息                                           描述

    Pages Scanned                    表或索引中的长页数

    Extents Scanned                 表或索引中的长区页数

    Extent Switches                  DBCC遍历页时从一个区域到另一个区域的次数

    Avg. Pages per Extent         相关区域中的页数

    Scan Density[Best Count:Actual Count]       

    Best Count是连续链接时的理想区域改变数,Actual Count是实际区域改变数,Scan Density为100%表示没有分块。

    Logical Scan Fragmentation   扫描索引页中失序页的百分比

    Extent Scan Fragmentation    不实际相邻和包含链路中所有链接页的区域数

    Avg. Bytes Free per Page       扫描页面中平均自由字节数

    Avg. Page Density (full)         平均页密度,表示页有多满

    建议:Scan Density在90%以上应当没有必要重整Index

    例子:

    Use pubs;

    DBCC ShowContig (authors) with all_indexes;

    20、

    DBCC sqlperf
    --可用参数logspace,iostats,threads
    --返回多种有用的统计信息

    DBCC SQLPERF
    (    [ LOGSPACE ]
         |
         [ "sys.dm_os_latch_stats" , CLEAR ]
         |
         [ "sys.dm_os_wait_stats" , CLEAR ]
    )
         [WITH NO_INFOMSGS ]

    --msdn

    21、

    DBCC cachestats
    --显示SQL SERVER内存的统计信息

    Here's what some of the key statistics from this command mean:

        * Hit Ratio: Displays the percentage of time that this particular object was found in SQL Server's cache. The bigger this number, the better.
        * Object Count: Displays the total number of objects of the specified type that are cached.
        * Avg. Cost: A value used by SQL Server that measures how long it takes to compile a plan, along with the amount of memory needed by the plan. This value is used by SQL Server to determine if the plan should be cached or not.
        * Avg. Pages: Measures the total number of 8K pages used, on average, for cached objects.
        * LW Object Count, LW Avg Cost, WL Avg Stay, LW Ave Use: All these columns indicate how many of the specified objects have been removed from the cache by the Lazy Writer. The lower the figure, the better.

    22、

    DBCC cursorstats
    --显示SQL SERVER游标的统计信息

    23、

    DBCC sqlmgrstats
    --显示缓冲中先读和预先准备的SQL语句

    24、

    DBCC errorlog
    --初始化SQL SERVER错误日志文件

    DBCC ERRORLOG: If you rarely restart the mssqlserver service, you may find that your server log gets very large and takes a long time to load and view. You can truncate (essentially create a new log) the Current Server log by running DBCC ERRORLOG. You might want to consider scheduling a regular job that runs this command once a week to automatically truncate the server log. As a rule, I do this for all of my SQL Servers on a weekly basis. Also, you can accomplish the same thing using this stored procedure: sp_cycle_errorlog.

    25、

    DBCC flushprocindb
    --清除SQL SERVER服务器内存中某个数据库的存储过程缓存内容

    DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a specific database on a SQL Server, not the entire SQL Server. The database ID number to be affected must be entered as part of the command.

    You may want to use this command before testing to ensure that previous stored procedure plans won't negatively affect testing results.

    Example:

    DECLARE @intDBID INTEGER SET @intDBID = (SELECT dbid FROM master.dbo.sysdatabases WHERE name = 'database_name')
    DBCC FLUSHPROCINDB (@intDBID)

    26、

    DBCC Buffer
    --显示缓冲区的扇信息和页面信息

    27、

    DBCC DBinfo
    --显示数据库结构信息

    28、

    DBCC DBtable
    --显示管理数据的表信息

    29、

    DBCC IND
    --查看某个索引使用的页面信息

    --(databasename,tablename,-1)用来查看数据表的物理结构

    来查看此表的物理存储页面

    Index为 null并且pagetype 为10的为IAM 页

    Index为1并且pagetype 为2的为clustered索引页

    Index为1并且pagetype为1的为数据页

    Index为2并且pagetype为2的为non clustered索引页

    例子:

    DBCC IND('taoisTong',taoisTong,-1) 

    30、

    DBCC REbuild_log
    --重建SQL SERVER事务日志文件

    31、

    DBCC log
    --查看某个数据库使用的事务日志信息


    32、

    DBCC procbuf
    --显示过程缓冲池中的缓冲区头和存储过程头

    33、

    DBCC prtipage
    --查看某个索引页面的每行指向的页面号

    34、

    DBCC pss
    --显示当前连接到SQL SERVER的进程信息

    35、

    DBCC resource
    --显示服务器当前使用的资源情况

    36、

    DBCC tab
    --查看数据页面的结构

    --语法:
    DBCC TAB (dbid,objectid[,?])

    --参数:
       dbid --数据库ID
       object_id --对象ID

       ,? --估计后面还有参数,未知

    测试效果:

    select db_Id('pubs')--5

    select object_id('titles')--2121058592 

    DBCC tab (5,2121058592 ,0)

    dbcc ind (pubs ,titles ,-1)

    后两个数据结果一致,在一定程度上可以使用dbcc ind

  • 相关阅读:
    JS4
    JS3
    JS2
    JS1
    Dos命令
    面向对象的复习
    9.14Css
    9.13列表的用法
    9.12Css
    9.11Css
  • 原文地址:https://www.cnblogs.com/GoGoagg/p/1802192.html
Copyright © 2011-2022 走看看