国内最大的酷站演示中心!
哈尔滨工业大学计算机学院数据库研究中心 [email protected]如何在sql server 2000中显示查询计划。 使用 sql 查询分析器以文本方式显示执行计划showplan_text and showplan_allthe two set options showplan_text and showplan_all let you see the estimated query plan without actually executing the query. both options also automatically enable the set noexec option, so you don't see any results from your query—you see only the way that sql server has determined is the best method for processing the query. turning noexec on can be a good thing while tuning a query. for example, if you have a query that takes 20 minutes to execute, you might try to create an index that will allow it to run faster. however, immediately after creating a new index, you might just want to know whether the query optimizer will even choose to use that index. if you were actually executing the query every time you looked at its plan, it would take you 20 minutes for every tuning attempt. setting noexec on along with the show plan option will allow you to see the plan without actually executing all the statements. warning since turning on showplan_text or showplan_all implies that noexec is also on, you must set the showplan option to off before you do anything else. for example, you must set showplan_text to off before setting showplan_all to on. 使用 sql 查询分析器以图形方式显示执行计划 如何使用dbcc page命令来查看数据页格式例子:use testgoselect * from stugo结果: 首先来看sysindexes关系的内容。数据库中的每个索引和表在表中各占一行。该表存储在每个数据库中。列名数据类型描述idint表 id(如果 indid = 0 或 255)。否则为索引所属表的 id。statusint内部系统状态信息。firstbinary(6)指向第一页或根页的指针。indidsmallint索引 id: 0 = 该表为新表,对应的表是一个堆1 = 聚集索引>1 = 非聚集255 = 具有 text 或 image 数据的表条目rootbinary(6)如果 indid >= 1 和 1 重复。如果 indid = 255,rows 设置为 0。提供该列是为了向后兼容。select id ,first,indid from sysindexes where id =object_id('stu') and indid in (0,1)dbcc的格式为:dbcc page ({dbid | dbname},filenum,pagenum[,printopt])为了获得filenum,filenum,执行下面的语句: select id ,first,indid from sysindexes where id =object_id('stu') and indid in (0,1)id first indid4535766540x1900000001000然后运行 dbcc dbcc traceon (3604)godbcc page('test',1,25,1)godbcc 执行完毕。如果 dbcc 输出了错误信息,请与系统管理员联系。page: (1:25)------------buffer:-------buf @0x18ed5a00---------------bpage = 0x19620000 bhash = 0x00000000 bpageno = (1:25)bdbid = 7 breferences = 24 bstat = 0x9bspin = 0 bnext = 0x00000000 page header:------------page @0x19620000----------------m_pageid = (1:25) m_headerversion = 1 m_type = 1m_typeflagbits = 0x0 m_level = 0 m_flagbits = 0x8008m_objid = 453576654 m_indexid = 0 m_prevpage = (0:0)m_nextpage = (0:0) pminlen = 8 m_slotcnt = 8m_freecnt = 7938 m_freedata = 1316 m_reservedcnt = 0m_lsn = (6:166:2) m_xactreserved = 0 m_xdesid = (0:0)m_ghostreccnt = 0 m_tornbits = 2 allocation status-----------------gam (1:2) = allocated sgam (1:3) = not allocatedpfs (1:1) = 0x61 mixed_ext allocated 50_pct_full diff (1:6) = changedml (1:7) = not min_logged data:-----slot 0, offset 0x60-------------------record type = primary_record record attributes = null_bitmap variable_columns 19620060: 00080030 0000000c 01000002 7a001300 0..............z19620070: 6f6168 haoslot 1, offset 0x73-------------------record type = primary_record record attributes = null_bitmap variable_columns 19620073: 00080030 00000002 01000002 6b001200 0..............k19620083: 6961 aislot 2, offset 0x85-------------------record type = primary_record record attributes = null_bitmap variable_columns 19620085: 00080030 0000002d 01000002 77001300 0...-..........w19620095: 676e61 angslot 3, offset 0x98-------------------record type = primary_record record attributes = null_bitmap variable_columns 19620098: 00080030 0000000f 01000002 66001300 0..............f196200a8: 676e65 engslot 4, offset 0xab-------------------record type = primary_record record attributes = null_bitmap variable_columns 196200ab: 00080030 0000004c 01000002 78001100 0...l..........x196200bb: 78 x slot 5, offset 0xbc-------------------record type = primary_record record attributes = null_bitmap variable_columns 196200bc: 00080030 0000005a 01000002 79001100 0...z..........y196200cc: 79 y slot 6, offset 0xcd-------------------record type = primary_record record attributes = null_bitmap 196200cd: 00080010 000000c8 020002 ...........slot 7, offset 0x50e--------------------record type = primary_record record attributes = null_bitmap variable_columns 1962050e: 00080030 000004d2 01000002 7a001600 0..............z1962051e: 6b6f6168 6961 haokaioffset table:-------------row - offset 7 (0x7) - 1294 (0x50e) 6 (0x6) - 205 (0xcd) 5 (0x5) - 188 (0xbc) 4 (0x4) - 171 (0xab) 3 (0x3) - 152 (0x98) 2 (0x2) - 133 (0x85) 1 (0x1) - 115 (0x73) 0 (0x0) - 96 (0x60) dbcc 执行完毕。如果 dbcc 输出了错误信息,请与系统管理员联系。 分析:数据行的格式19620060: 00080030 0000000c 01000002 7a001300 0..............z19620070: 6f6168 haoid = 12 name = zhao 30 00 0800 0c000000 0200 00 0100 1300 7a68616f300008000c000000020000010013007a68616f状态位未使用找到第一 列的位置第八个字节开始定长第一列的值为 c=12总列数为2null标志均可以为非空变长列数为1列第一个变长结束的位置在0x13=19结束第一个变长列zhao object_id返回数据库对象标识号。语法object_id ( 'object' )参数'object'要使用的对象。object 的数据类型为 char 或 nchar。如果 object 的数据类型是 char,那么隐性将其转换成 nchar。返回类型int dbcc extentinfo利用dbcc extentinfo得到属于一个对象的所有盘区的列表。其语法为:dbcc extentinfo(dbname,tablename,indexid) sql 会把聚集索引的数据页用链表连接起来,但对于堆不需要。例如有 以及 其数据行如下(选例):19918060: 00080030 0a0a0001 01000004 4e002b00 0............+.n19918070: 48207765 20657269 6f4a202d 6f6e2062 ew hire - job no19918080: 70732074 66696365 646569 t specifiedjob_id = 1 job_desc = new hire - job not specifiedmin_lvl = 10 max_lvl = 10 00080030 0a0a0001 01000004 4e002b00 48207765 20657269 6f4a202d 6f6e2062 70732074 66696365 6465693000080001000a列数开始的位置small int 第一列数据tiny int第三列数据0a04000001002btiny int第四列数据列数为4均可空变长列数为1第一个变长列结束的位置剩下的为字符串 replicate以指定的次数重复字符表达式。语法replicate ( character_expression , integer_expression ) 参数character_expression由字符数据组成的字母数字表达式。character_expression 可以是常量或变量,也可以是字符列或二进制数据列。integer_expression是正整数。如果 integer_expression 为负,则返回空字符串。返回类型varcharcharacter_expression 必须为可隐性转换为 varchar 的数据类型。否则,使用 cast 函数显式转换 character_expression。for example:replicate(cast(1 as varchar(1)),250) or replicate(‘1’,250) and they are the same. 如果是cast(1 as char)则,类型转化后的1所占的空间为32个字节如果是cast(1 as varchar(1)),则1所占的空间为1个字节。 primary key unique在sql中primary key 和unique的处理是一样的。系统会自动在primary key 和unique所约束的列上建立聚集索引。查询优化器基于位于索引的存在而不是基于一个烈被声明为主码的事实来作出决策。例如: 在建立it表后,系统自动为it建立聚集索引,同时上图表明了sysobjects,syscolumns和sysindexes的关系。待插入两行后,结果如下: 当我们插入insert into it values(3,null)分析文件格式1990408d: 00080010 00000003 020002 ...........id = 3 name = [null] 10 00 0800 03000000 0200 021000080003000000020002第一列数据在第八个字节处一个列数据为3共有两列null = 10 第二列为空,结束 建立索引是如不指名是clustered或nonclustered,则系统默认为非簇集的,即indid > 1 syslogins中保留了所有登陆用户的信息 sp_dboption显示或更改数据库选项。不能在 master 或 tempdb 数据库上使用 sp_dboption。向后兼容性支持 sp_dboption。使用 alter database 设置数据库选项。语法sp_dboption [ [ @dbname = ] 'database' ] [ , [ @optname = ] 'option_name' ] [ , [ @optvalue = ] 'value' ]参数[@dbname =] 'database'在其中设置指定选项的数据库的名称。database 的数据类型为 sysname,默认值为 null。[@optname =] 'option_name'要设置的选项的名称。没有必要输入完整的选项名称。microsoft? sql server? 可识别名称中任何独有的部分。如果选项名称包含空格或者关键字,请将选项名称用引号引起来。如果省略此参数,sp_dboption 将列出处于打开状态的选项。option_name 的数据类型为 varchar(35),默认值为 null。 [@value =] 'value'option_name 的新设置。如果省略此参数,sp_dboption 将返回当前设置。value 可以是 true、false、on 或 off。value 的数据类型为 varchar(10),默认值为 null。返回代码值0(成功)或 1(失败) 可以用sp_dboption 来设置批量insertselect into/bulkcopy当为 true 时,允许使用 select into 语句和快速大容量复制。 累积求和问题-t-sql系列累积求和问题 有一表: col1 value running_tot 1 10 0 (10) 2 15 0 (25) 3 50 0 (75)若要将value列累积求和的结果放到running_tot 列,可以这样:declare @i intset @i=0update tableset @[email protected]+value或者declare @i intset @i=0update tableset @[email protected]+valuesfrom table