﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-The NoteBook of EricKong-随笔分类-数据库</title><link>http://www.blogjava.net/jjshcc/category/45278.html</link><description>桑巴葡语翻译工作室是广州市最专业葡萄牙语翻译机构-www.puyufanyi.com-欢迎寻找Java,As400,Mainframe的猎头eric_cc#qq.ccom(把#换成@)</description><language>zh-cn</language><lastBuildDate>Tue, 08 Oct 2013 20:30:07 GMT</lastBuildDate><pubDate>Tue, 08 Oct 2013 20:30:07 GMT</pubDate><ttl>60</ttl><item><title>Oracle 系统权限和对象权限管理</title><link>http://www.blogjava.net/jjshcc/archive/2013/10/08/404735.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Tue, 08 Oct 2013 03:51:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2013/10/08/404735.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/404735.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2013/10/08/404735.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/404735.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/404735.html</trackback:ping><description><![CDATA[<div><div>1.查询Oracle所有系统权限&nbsp;</div><div>&nbsp;</div><div>select * from system_privilege_map &nbsp;&nbsp;</div><div></div><div>2.查询Oracle所有对象权限&nbsp;</div><div>&nbsp;</div><div>select * from table_privilege_map &nbsp;&nbsp;</div><div></div><div>3.将'授予系统权限'这个权限给某用户--当然一般采用 dba 给其他用户授予&#8220;其他权限&#8221;,这个权限一般不需要给别人&nbsp;</div><div>&nbsp;</div><div>grant any privilege to TestUser 【with admin option】//表示 TestUser 用户有权利 给其他用户授予 系统权限 。后面 表示对这个权限的维护(能否再授予其他用户) &nbsp;&nbsp;</div><div></div><div>4.举例： 授予系统权限 create session (连接数据库的关键权限，connect角色中有这个权限，oracle角色 文章中有介绍)&nbsp;</div><div>&nbsp;</div><div>1.grant create session to TestUser //TestUser就可以连接数据库了 &nbsp;</div><div>2.grant create session to TestUser with option admin //TestUser就可以连接数据库了,并且 可以 将这个权限 给其他用户 &nbsp;</div><div></div><div>5.收回 系统权限 不会级联回收&nbsp;</div><div>&nbsp;</div><div>revoke create session from TestUser &nbsp;</div><div></div><div>6.关于 系统权限 的维护&nbsp;</div><div>&nbsp;</div><div>with admin option //表示继承后可以 授予其他用户 &nbsp;</div><div></div><div>7.关于 对象权限 的维护&nbsp;</div><div>&nbsp;</div><div>with grant option //表示继承后可以 授予其他用户 &nbsp;</div><div></div><div>8.举例： 授予对象权限 update,select,insert,delete,all [4种等于 all]&nbsp;</div><div>&nbsp;</div><div>grant select on scott.emp to TestUser 【with grant option】 //表示给TestUser 授予scott.emp的查询权限 &nbsp;</div><div>grant all on scott.emp to TestUser //表示给TestUser 授予scott.emp的select,update,insert,delete 权限 &nbsp;</div><div></div><div>9.举例： 授予 某几列 对象权限&nbsp;</div><div>&nbsp;</div><div>grant update on scott.emp(sal) to luob //指定修改某列 &nbsp;</div><div>grant select on emp(ename,sal) to luob //只能查询这指定字段 &nbsp;</div><div></div><div>10.举例：授予 alter 权限&nbsp;</div><div>&nbsp;</div><div>grant alter on scott.emp to luob //授予这个表结构的修改权限 &nbsp;</div><div></div><div>11.举例：授予 execute 权限 （用于用户执行其他方案的包、过程，函数）&nbsp;</div><div>&nbsp;</div><div>grant execute on dbms_transaction to luob; --执行 dbms_transaction 包的权限 &nbsp;</div><div></div><div>12.举例：授予 index 权限&nbsp;</div><div>&nbsp;</div><div>grant index on scott.emp to luob 【with grant option】 //授予创建索引的权限 &nbsp;</div><div></div><div>13.收回 对象权限 【cascade】会级联回收&nbsp;</div><div>&nbsp;</div><div>revoke select on scott.emp from TestUser【cascade】//撤销查询权限或者级联撤销 &nbsp;</div><div></div><div>14.举例：级联回收的&nbsp;</div><div>&nbsp;</div><div>grant select on emp to luobing with grant option; //授予权限 并维护 &nbsp;</div><div>conn luobin/m123@ORACLE； //登录后 给 xiaoming 权限 &nbsp;</div><div>grant select on emp to xiaoming; &nbsp;</div><div>conn scott/tiger@ORACLE //scott 撤销 luobing的权限 &nbsp;</div><div>revoke select on emp from luobing cascade &nbsp;</div><div>conn xiaoming/m123@ORACLE; &nbsp;</div><div>select * from scott.emp; //表和视图不存在 &nbsp;</div><div></div><div>15.查询当前用户具有的系统权限&nbsp;</div><div>&nbsp;</div><div>select * from dba_sys_privs where grantee='SYSTEM' &nbsp;</div><div></div><div>16.查询当前用户具有的对象权限&nbsp;</div><div>&nbsp;</div><div>select * from dba_tab_privs where grantee='SYSTEM'; &nbsp;</div><div></div><div>17.查询当前用户具有的列权限&nbsp;</div><div>&nbsp;</div><div>select * from dba_col_privs where grantee='SYSTEM'; &nbsp;</div><div></div><div>18.查询用户 SCOTT 所拥有的系统权限&nbsp;</div><div>&nbsp;</div><div>select * from dba_sys_privs where grantee in (select granted_role from dba_role_privs where grantee='SCOTT'); &nbsp;</div><div></div><div>19.数据库管理员 DBA的一些职责&nbsp;</div><div>&nbsp;</div><div>1.安装和升级 oracle 数据库 &nbsp;</div><div>2.建库建表建空间 视图，索引。。。。 &nbsp;</div><div>3.指定和并实施 备份和恢复计划 &nbsp;</div><div>4.数据库权限管理，调优，故障排除 &nbsp;</div><div>5.对高级的dba，要求能参加项目开发，会编写sql语句 ，存储过程，触发器，规则，约束，包 &nbsp;</div><div>6.管理员 还有管理初始化参数 &nbsp;</div><div>&nbsp;&nbsp; &nbsp; show &nbsp;parameter; 有 200多参数 有参数可以配置的 &nbsp;</div><div>7.管理数据库的用户主要是 sys (董事长)&gt; 和system &nbsp;(总经理) &nbsp;</div><div></div><div>20.sys,sysdba,sysoper的区别&nbsp;</div><div>&nbsp;</div><div>sys：拥有oracle的基表和视图，拥有 dba 数据库管理 &nbsp;</div><div>sysdba:系统管理员 &nbsp;</div><div>sysoper:系统操作员 &nbsp;的角色和权限 &nbsp;</div><div>system：主要存储次一级的数据 如：oracle的特性和工具的管理信息和 &nbsp;&nbsp;</div><div>dba，sysdba 角色权限 区别 &nbsp; &nbsp;&nbsp;</div><div>1.存储的数据重要性不一样 ,权限不一样 &nbsp;&nbsp;</div><div>2.sys 必须 利用 as sysdba 或者 as sysoper 不能用normal（标准）方式登录 &nbsp;</div><div>3.system 默认登录角色是 &nbsp;dba &nbsp; （conn system/manager） &nbsp;</div><div>4.如果用 &nbsp;conn system/manager as sysdba &nbsp;登录结果和 sys 登录一样 &nbsp;</div><div></div><div>21. sysdba 和 sysoper的权限区别&nbsp;</div><div></div><div></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; "><table style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; "><tbody><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">权限</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">sysdba</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">sysoper</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">能够 Startup/Shutdown 启动/关闭数据库</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">alter database open/mount/backup(设置数据库不同的状态)</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">改变字符集</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">n</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">采用 create/drop database(创建/删除数据库)</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">n</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">create spfile</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">alter database archivelog(归档日志)</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">alter database recover(完全和部分恢复数据库)</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">只能完全恢复，不能执行部分恢复</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">拥有 restricted session(会话限制)权限</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">可以让用户作为sys 用户连接</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">y</td></tr><tr><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">登录之后</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">sys</td><td style="font-size: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; ">public</td></tr></tbody></table></span></div><div></div></div><div></div><img src ="http://www.blogjava.net/jjshcc/aggbug/404735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2013-10-08 11:51 <a href="http://www.blogjava.net/jjshcc/archive/2013/10/08/404735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle用户权限表的管理方法</title><link>http://www.blogjava.net/jjshcc/archive/2013/10/08/404732.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Tue, 08 Oct 2013 03:44:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2013/10/08/404732.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/404732.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2013/10/08/404732.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/404732.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/404732.html</trackback:ping><description><![CDATA[<div><span style="font-family: 宋体, Verdana; line-height: 23px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">我们将从创建Oracle用户权限表开始谈起，然后讲解登陆等一般性动作，使大家对Oracle用户权限表有个深入的了解。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">一、创建</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sys;//系统管理员，拥有最高权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　system;//本地管理员，次高权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　scott;//普通用户，密码默认为tiger,默认未解锁</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sys;//系统管理员，拥有最高权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　system;//本地管理员，次高权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　scott;//普通用户，密码默认为tiger,默认未解锁</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">二、登陆</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus / as sysdba;//登陆sys帐户</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus sys as sysdba;//同上</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus scott/tiger;//登陆普通用户scott</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus / as sysdba;//登陆sys帐户</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus sys as sysdba;//同上</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　sqlplus scott/tiger;//登陆普通用户scott</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">三、管理用户</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　create user zhangsan;//在管理员帐户下，创建用户zhangsan</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　alert user scott identified by tiger;//修改密码</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　create user zhangsan;//在管理员帐户下，创建用户zhangsan</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　alert user scott identified by tiger;//修改密码</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">四，授予权限</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　1、默认的普通用户scott默认未解锁，不能进行那个使用，新建的用户也没有任何权限，必须授予权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*管理员授权*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant create session to zhangsan;//授予zhangsan用户创建session的权限，即登陆权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant unlimited session to zhangsan;//授予zhangsan用户使用表空间的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant create table to zhangsan;//授予创建表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grante drop table to zhangsan;//授予删除表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert table to zhangsan;//插入表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update table to zhangsan;//修改表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant all to public;//这条比较重要，授予所有权限(all)给所有用户(public)</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*管理员授权*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant create session to zhangsan;//授予zhangsan用户创建session的权限，即登陆权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant unlimited session to zhangsan;//授予zhangsan用户使用表空间的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant create table to zhangsan;//授予创建表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grante drop table to zhangsan;//授予删除表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert table to zhangsan;//插入表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update table to zhangsan;//修改表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant all to public;//这条比较重要，授予所有权限(all)给所有用户(public)</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　2、oralce对权限管理比较严谨，普通用户之间也是默认不能互相访问的，需要互相授权</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*oralce对权限管理比较严谨，普通用户之间也是默认不能互相访问的*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant select on tablename to zhangsan;//授予zhangsan用户查看指定表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant drop on tablename to zhangsan;//授予删除表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert on tablename to zhangsan;//授予插入的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update on tablename to zhangsan;//授予修改表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert(id) on tablename to zhangsan;</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update(id) on tablename to zhangsan;//授予对指定表特定字段的插入和修改权限，注意，只能是insert和update</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert all table to zhangsan;//授予zhangsan用户alert任意表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*oralce对权限管理比较严谨，普通用户之间也是默认不能互相访问的*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant select on tablename to zhangsan;//授予zhangsan用户查看指定表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant drop on tablename to zhangsan;//授予删除表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert on tablename to zhangsan;//授予插入的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update on tablename to zhangsan;//授予修改表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant insert(id) on tablename to zhangsan;</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant update(id) on tablename to zhangsan;//授予对指定表特定字段的插入和修改权限，注意，只能是insert和update</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert all table to zhangsan;//授予zhangsan用户alert任意表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">五、撤销权限</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　基本语法同grant,关键字为revoke</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　基本语法同grant,关键字为revoke</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">六、查看权限</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from user_sys_privs;//查看当前用户所有权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from user_tab_privs;//查看所用用户对表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from user_sys_privs;//查看当前用户所有权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from user_tab_privs;//查看所用用户对表的权限</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">七、操作表的用户的表</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*需要在表名前加上用户名，如下*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from zhangsan.tablename</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*需要在表名前加上用户名，如下*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　select * from zhangsan.tablename</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">八、权限传递</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　即用户A将权限授予B，B可以将操作的权限再授予C，命令如下：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert table on tablename to zhangsan with admin option;//关键字 with admin option</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert table on tablename to zhangsan with grant option;//关键字 with grant option效果和admin类似</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert table on tablename to zhangsan with admin option;//关键字 with admin option</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant alert table on tablename to zhangsan with grant option;//关键字 with grant option效果和admin类似</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　<strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">九、角色</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　角色即权限的集合，可以把一个角色授予给用户</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　create role myrole;//创建角色</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant create session to myrole;//将创建session的权限授予myrole</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　grant myrole to zhangsan;//授予zhangsan用户myrole的角色</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　drop role myrole;删除角色</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　/*但是有些权限是不能授予给角色的，比如unlimited tablespace和any关键字*/</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; ">　　Oracle用户权限表就介绍到这里。</p></span></div><img src ="http://www.blogjava.net/jjshcc/aggbug/404732.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2013-10-08 11:44 <a href="http://www.blogjava.net/jjshcc/archive/2013/10/08/404732.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQLServer性能计数器简要说明</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/04/331007.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Sat, 04 Sep 2010 02:53:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/04/331007.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/331007.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/04/331007.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/331007.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/331007.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 26px; ">下表对重要的性能计数器做一个简要的说明:<br />
</p>
<span  style="font-family: 宋体; font-size: 14px; line-height: 26px; ">&nbsp;</span><span  style="font-family: 宋体; font-size: 14px; line-height: 26px; ">&nbsp;</span><span  style="font-family: 宋体; font-size: 14px; line-height: 26px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<table border="1" frame="border" style="font-size: 12px; font-family: 宋体; line-height: 26px; ">
    <tbody>
        <tr>
            <td style="font-size: 12px; ">性能计数器:</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Performance Object</td>
            <td style="font-size: 12px; ">Counter</td>
            <td style="font-size: 12px; ">Description</td>
        </tr>
        <tr>
            <td rowspan="2" style="font-size: 12px; ">Processor</td>
            <td style="font-size: 12px; ">%processor Time</td>
            <td style="font-size: 12px; ">指处理器执行非闲置线程时间的百分比,测量处理器繁忙的时间 这个计数器设计成用来作为处理器活动的主要指示器,可以选择单个CPU实例,也可以选择Total</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Interrupts/sec</td>
            <td style="font-size: 12px; ">处理器正在处理的来自应用程序或硬件的中断的数量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="5" style="font-size: 12px; ">PhysicalDisk</td>
            <td style="font-size: 12px; ">% Disk Time</td>
            <td style="font-size: 12px; ">
            <div id="_mcePaste" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; ">计数器监视磁盘忙于读/写活动所用时间的百分比.在系统监视器中，PhysicalDisk: % Disk Time 计数器监视磁盘忙于读/写活动所用时间的百分比。如果 PhysicalDisk: % Disk Time 计数器的值较高（大于 90%），请检查 PhysicalDisk: Current Disk Queue Length 计数器了解等待进行磁盘访问的系统请求数量。等待 I/O 请求的数量应该保持在不超过组成物理磁盘的轴数的 1.5 到 2 倍。大多数磁盘只有一个轴，但独立磁盘冗余阵列 (RAID) 设&nbsp;备通常有多个轴。硬件 RAID 设备在系统监视器中显示为一个物理磁盘。通过软件创建的多个 RAID 设备在系统监视器中显示为多个实例。</div>
            <div id="_mcePaste" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; ">可以使用 Current Disk Queue Length 和 % Disk Time 计数器的值检测磁盘子系统中的瓶颈。如果 Current Disk Queue Length 和 % Disk Time 计数器的值一直很高，则考虑下列事项：</div>
            <div id="_mcePaste" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; ">1.使用速度更快的磁盘驱动器。</div>
            <div id="_mcePaste" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; ">2.将某些文件移至其他磁盘或服务器。</div>
            <div id="_mcePaste" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; ">3.如果正在使用一个 RAID 阵列，则在该阵列中添加磁盘。</div>
            计数器监视磁盘忙于读/写活动所用时间的百分比.在系统监视器中，PhysicalDisk: % Disk Time 计数器监视磁盘忙于读/写活动所用时间的百分比。<br />
            如果 PhysicalDisk: % Disk Time 计数器的值较高（大于 90%），请检查 PhysicalDisk: Current Disk Queue Length 计数器了解等待进行磁<br />
            盘访问的系统请求数量。等待 I/O 请求的数量应该保持在不超过组成物理磁盘的轴数的 1.5 到 2 倍。大多数磁盘只有一个轴，但独立磁盘冗余阵列&nbsp;<br />
            (RAID) 设备通常有多个轴。硬件 RAID 设备在系统监视器中显示为一个物理磁盘。通过软件创建的多个 RAID 设备在系统监视器中显示为多个实例。<br />
            可以使用 Current Disk Queue Length 和 % Disk Time 计数器的值检测磁盘子系统中的瓶颈。如果 Current Disk Queue Length 和 % Disk Time 计数器的值一直很高，则考虑下列事项：<br />
            1.使用速度更快的磁盘驱动器。<br />
            2.将某些文件移至其他磁盘或服务器。<br />
            3.如果正在使用一个 RAID 阵列，则在该阵列中添加磁盘。</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Avg.Disk Queue Length</td>
            <td style="font-size: 12px; ">指读取和写入请求(为所选磁盘在实例间隔中列队的)的平均数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Current Disk Queue Length</td>
            <td style="font-size: 12px; ">指示被挂起的磁盘 I/O 请求的数量。如果这个值始终高于 2，&nbsp;就表示产生了拥塞</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Avg.Disk Bytes/Transfer</td>
            <td style="font-size: 12px; ">写入或读取操作时向磁盘传送或从磁盘传出字节的平均数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Disk Bytes/sec</td>
            <td style="font-size: 12px; ">在读写操作中，从磁盘传出或传送到磁盘的字节速率</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="5" style="font-size: 12px; ">Memory</td>
            <td style="font-size: 12px; ">Pages/sec</td>
            <td style="font-size: 12px; ">被请求页面的数量.</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Available Bytes</td>
            <td style="font-size: 12px; ">可用物理内存的数量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Committed Bytes</td>
            <td style="font-size: 12px; ">已分配给物理 RAM 用于存储或分配给页面文件的虚拟内存</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Pool Nonpaged Bytes</td>
            <td style="font-size: 12px; ">未分页池系统内存区域中的 RAM 数量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Page Faults/sec</td>
            <td style="font-size: 12px; ">是每秒钟出错页面的平均数量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; ">Network Interface</td>
            <td style="font-size: 12px; ">Bytes Received/sec</td>
            <td style="font-size: 12px; ">使用本网络适配器接收的字节数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Bytes Sent/sec</td>
            <td style="font-size: 12px; ">使用本网络适配器发送的字节数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Bytes Total/sec</td>
            <td style="font-size: 12px; ">使用本网络适配器发送和接收的字节数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Server</td>
            <td style="font-size: 12px; ">Bytes Received/sec</td>
            <td style="font-size: 12px; ">把此计数器与网络适配器的总带宽相比较，确定网络连接是否产生瓶颈</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; ">SQL Server Access Methods</td>
            <td style="font-size: 12px; ">Page Splits/sec</td>
            <td style="font-size: 12px; ">每秒由于索引页溢出而发生的页拆分数.如果发现页分裂的次数很多,考虑提高Index的填充因子.数据页将会有更多的空间保留用于做数据的填充,从而减少页拆分</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Pages Allocated/sec</td>
            <td style="font-size: 12px; ">在此 SQL Server 实例的所有数据库中每秒分配的页数。这些页包括从混合区和统一区中分配的页</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Full Scans/sec</td>
            <td style="font-size: 12px; ">每秒不受限制的完全扫描数. 这些扫描可以是基表扫描，也可以是全文索引扫描</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; ">SQL Server: SQL Statistics</td>
            <td style="font-size: 12px; ">Batch Requests/Sec</td>
            <td style="font-size: 12px; ">每秒收到的 Transact-SQL 命令批数。这一统计信息受所有约束（如 I/O、用户数、高速缓存大小、请求的复杂程度等）影响。<br />
            批处理请求数值高意味着吞吐量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">SQL Compilations/Sec</td>
            <td style="font-size: 12px; ">每秒的编译数。表示编译代码路径被进入的次数。包括 SQL Server 中语句级重新编译导致的编译。当 SQL Server 用户活动稳定后，<br />
            该值将达到稳定状态</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Re-Compilations/Sec</td>
            <td style="font-size: 12px; ">每秒语句重新编译的次数。计算语句重新编译被触发的次数。一般来说，这个数最好较小,存储过程在理想情况下应该只编译一次，<br />
            然后执行计划被重复使用. 如果该计数器的值较高，或许需要换个方式编写存储过程，从而减少重编译的次数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; ">SQL Server: Databases</td>
            <td style="font-size: 12px; ">Log Flushes/sec</td>
            <td style="font-size: 12px; ">每秒日志刷新数目</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Active Transactions</td>
            <td style="font-size: 12px; ">数据库的活动事务数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Backup/Restore Throughput/sec</td>
            <td style="font-size: 12px; ">每秒数据库的备份和还原操作的读取/写入吞吐量。例如，并行使用多个备份设备或使用更快的设备时,可以测量数据库备份操作性能的变化情况。<br />
            数据库的备份或还原操作的吞吐量可以确定备份和还原操作的进程和性能</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; ">SQL Server General Statistics</td>
            <td style="font-size: 12px; ">User Connections</td>
            <td style="font-size: 12px; ">系统中活动的SQL连接数. 该计数器的信息可以用于找出系统的最大并发用户数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Temp Tables Creation Rate</td>
            <td style="font-size: 12px; ">每秒创建的临时表/表变量的数目</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Temp Tables For Destruction</td>
            <td style="font-size: 12px; ">等待被清除系统线程破坏的临时表/表变量数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="3" style="font-size: 12px; "><br />
            SQL Server Locks</td>
            <td style="font-size: 12px; ">Number of Deadlocks/sec</td>
            <td style="font-size: 12px; ">指每秒导致死锁的锁请求数. 死锁对于应用程序的可伸缩性非常有害, 并且会导致恶劣的用户体验. 该计数器必须为0</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Average Wait Time (ms)</td>
            <td style="font-size: 12px; ">每个导致等待的锁请求的平均等待时间</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Lock requests/sec</td>
            <td style="font-size: 12px; ">锁管理器每秒请求的新锁和锁转换数. 通过优化查询来减少读取次数, 可以减少该计数器的值</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="4" style="font-size: 12px; ">SQL Server:Memory Manager</td>
            <td style="font-size: 12px; ">Total Server Memory (KB)</td>
            <td style="font-size: 12px; ">从缓冲池提交的内存(这不是 SQL Server 使用的总内存)</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Target Server Memory (KB)</td>
            <td style="font-size: 12px; ">服务器能够使用的动态内存总量</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">SQL Cache Memory(KB)</td>
            <td style="font-size: 12px; ">服务器正在用于动态 SQL 高速缓存的动态内存总数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Memory Grants Pending</td>
            <td style="font-size: 12px; ">指每秒等待工作空间内存授权的进程数. 该计数器应该尽可能接近0,否则预示可能存在着内存瓶颈</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
        </tr>
        <tr>
            <td rowspan="6" style="font-size: 12px; ">SQL Server Buffer Manager</td>
            <td style="font-size: 12px; ">Buffer Cache Hit Ratio</td>
            <td style="font-size: 12px; ">缓存命中率,在缓冲区高速缓存中找到而不需要从磁盘中读取(物理I/O)的页的百分比. 如果该值较低则可能存在内存不足或不正确的索引</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Page Reads/sec</td>
            <td style="font-size: 12px; ">每秒发出的物理数据库页读取数。此统计信息显示的是所有数据库间的物理页读取总数。由于物理 I/O 的开销大，可以通过使用更大的数据缓存、智能索引、更有效的查询或更改数据库设计等方法，将开销降到最低</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Page Writes/sec</td>
            <td style="font-size: 12px; ">每秒执行的物理数据库页写入数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Page Life Expectancy</td>
            <td style="font-size: 12px; ">页若不被引用将在缓冲池中停留的秒数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Lazy Writes/Sec</td>
            <td style="font-size: 12px; ">每秒被缓冲区管理器的惰性编写器写入的缓冲区数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">Checkpoint Pages/Sec</td>
            <td style="font-size: 12px; ">由要求刷新所有脏页的检查点或其他操作每秒刷新到磁盘的页数</td>
        </tr>
        <tr>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;</td>
            <td style="font-size: 12px; ">&nbsp;<br />
            <br />
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/jjshcc/aggbug/331007.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-04 10:53 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/04/331007.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQl分区技术</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/03/330805.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Fri, 03 Sep 2010 01:38:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/03/330805.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330805.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/03/330805.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330805.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330805.html</trackback:ping><description><![CDATA[&nbsp;&nbsp; <span style="border-collapse: separate; color: #000000; font-family: Simsun; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: #323234; font-family: Arial; font-size: 12px;">你是否在千方百计优化SQL&nbsp;<a href="http://whatis.ctocio.com.cn/searchwhatis/497/5948997.shtml" target="_blank" style="color: #323234 ! important; border-width: 0px ! important; line-height: normal;">Server</a>&nbsp;数据库的性能?如果你的数据库中含有大量的表格，把这些表格分区放入独立的文件组可能会让你受益匪浅。SQL Server 2005引入的表分区技术，让用户能够把数据分散存放到不同的物理磁盘中，提高这些磁盘的并行处理性能以优化查询性能。
<p style="line-height: normal;">　　SQL Server数据库表分区操作过程由三个步骤组成：</p>
<p style="line-height: normal;">　　1. 创建分区函数</p>
<p style="line-height: normal;">　　2. 创建分区架构</p>
<p style="line-height: normal;">　　3. 对表进行分区</p>
<p style="line-height: normal;">　　下面将对每个步骤进行详细介绍。</p>
<p style="line-height: normal;">　　<strong style="line-height: normal;">步骤一：创建一个分区函数</strong></p>
<p style="line-height: normal;">　　此分区函数用于定义你希望SQL Server如何对数据进行分区的参数值(<u style="line-height: normal;">how</u>)。这个操作并不涉及任何表格，只是单纯的定义了一项技术来分割数据。</p>
<p style="line-height: normal;">　　我们可以通过指定每个分区的边界条件来定义分区。例如，假定我们有一份Customers表，其中包含了关于所有客户的信息，以一一对应的客户编号(从1到1,000,000)来区分。我们将通过以下的分区函数把这个表分为四个大小相同的分区：　　</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">CREATE&nbsp;<a href="http://whatis.ctocio.com.cn/searchwhatis/427/6025927.shtml" target="_blank" style="color: #323234 ! important; border-width: 0px ! important; line-height: normal;">PARTITION</a>&nbsp;FUNCTION customer_partfunc (<a href="http://whatis.ctocio.com.cn/searchwhatis/69/6025569.shtml" target="_blank" style="color: #323234 ! important; border-width: 0px ! important; line-height: normal;">int</a>)&nbsp;<br style="line-height: normal;" />
            AS RANGE RIGHT&nbsp;<br style="line-height: normal;" />
            FOR VALUES (250000, 500000, 750000)</td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　这些边界值定义了四个分区。第一个分区包括所有值小于250,000的数据<span style="color: red;">，第二个分区包括值在250,000到49,999之间的数据</span>。第三个分区包括值在500,000到7499,999之间的数据。所有值大于或等于750,000的数据被归入第四个分区。</p>
<p style="line-height: normal;">　　请注意，这里调用的"RANGE RIGHT"语句表明每个分区边界值是右界。类似的，<span style="color: red;">如果使用"RANGE LEFT"语句</span>，则上述第一个分区应该包括所有值小于或等于250,000的数据<span style="color: red;">，第二个分区的数据值在250,001到500,000之间，以此类推。</span></p>
<p style="line-height: normal;">　　<strong style="line-height: normal;">步骤二：创建一个分区架构</strong></p>
<p style="line-height: normal;">　　一旦给出描述如何分割数据的分区函数，接着就要创建一个分区架构，用来定义分区位置(<u style="line-height: normal;">where</u>)。创建过程非常直截了当，只要将分区连接到指定的文件组就行了。例如，<span style="color: red;">如果有四个文件组，组名从"fg1"到"fg4"</span>，那么以下的分区架构就能达到想要的效果：　　</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">CREATE PARTITION SCHEME customer_partscheme&nbsp;<br style="line-height: normal;" />
            AS PARTITION customer_partfunc&nbsp;<br style="line-height: normal;" />
            <span style="color: red;">TO (fg1, fg2, fg3, fg4)</span></td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　注意，这里将一个分区函数连接到了该分区架构，但并没有将分区架构连接到任何数据表。这就是可复用性起作用的地方了。无论有多少数据库表，我们都可以使用该分区架构(或仅仅是分区函数)。</p>
<p style="line-height: normal;">　　<strong style="line-height: normal;">步骤三：对一个表进行分区</strong></p>
<p style="line-height: normal;">　　定义好一个分区架构后，就可以着手创建一个分区表了。这是整个分区操作过程中最简单的一个步骤。只需要在表创建指令中添加一个"ON"语句，用来指定分区架构以及应用该架构的表列。因为分区架构已经识别了分区函数，所以不需要再指定分区函数了。</p>
<p style="line-height: normal;">　　例如，使用以上的分区架构创建一个客户表，可以调用以下的Transact-SQL指令：　　</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">CREATE TABLE customers (FirstName nvarchar(40), LastName nvarchar(40), CustomerNumber int)&nbsp;<br style="line-height: normal;" />
            <span style="color: red;">ON customer_partscheme </span>(<span style="color: red;">CustomerNumber</span>)</td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　关于SQL Server的表分区功能，你知道上述的相关知识就足够了。记住!编写能够用于多个表的一般的分区函数和分区架构就能够大大提高可复用性。</p>
<p style="line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数据库性能调优是每一个优秀SQL Server管理员最终的责任。虽然保证数据的安全和可用性是我们的最高的目标，但是假如数据库应用程序无法满足用户的要求，那么DBA们会因为性能低下的设计和实现而受到指责。SQL Server 2005在数据库性能方面得到了很多提高，尤其是表分区的技术。如果你还没不了解表分区的特征，那么请你花点时间读这篇文章。</p>
<p style="line-height: normal;">　　表分区的概念不是一个新的概念;只要你当过一段时间的SQL Server DBA，那么你可能已经对一些频繁访问的表进行过归档，当这个表中的历史数据变的不再经常被访问的时候。比如，假设你有一个打印时间报表的应用，你的报告很少会查询1995年的数据，因为绝大部分的预算规划会基于最近几年的数据。</p>
<p style="line-height: normal;">　　在SQL Server的早期版本中，你可以创建多个表。每一个表都具有相同的列结构，用来保存不同年份的数据。这样，当存在着对历史数据访问的必要的时候，你可以创建一个视图来对这些表进行查询处理。将数据保存在多个表中是很方便的，因为相对于查询时扫描整个大表，扫描小表会更快。但是这种好处只有在你预先知道哪些时间段的数据会被访问。同时，一旦数据过期，你还需要创建新表并且转移新产生的历史数据。</p>
<p style="line-height: normal;">　　SQL Server 7和SQL Server 2000支持分布式分区视图(distributed partitioned views，又称为物化视图，materialized views).分布式分区视图由分布于多台服务器上的、具有相同表结构的表构成，而且你还需要为每一个服务器增加链接服务器定义(linked server definitions)，最后在其中一台服务器上创建一个视图将每台服务器上返回的数据合并起来。这里的设计思想是数据库引擎可以利用多台服务器的处理能力来满足查询。</p>
<p style="line-height: normal;">　　但是，分布式分区视图(DPV)受到很多限制，你可以在SQL Server的在线帮助文档中阅读到。虽然DPV在一些情况下能够提供性能上的提高，但是这种技术不能被广泛的应用。已经被证明它们不能满足逐步增长的企业级应用的要求。何况，DPV的实现是一个费力的过程，需要DBA进行很多工作。</p>
<p style="line-height: normal;">　　SQL Server 2005开始支持表分区，这种技术允许所有的表分区都保存在同一台服务器上。每一个表分区都和在某个文件组(filegroup)中的单个文件关联。同样的一个文件/文件组可以容纳多个分区表。</p>
<p style="line-height: normal;">　　在这种设计架构下，数据库引擎能够判定查询过程中应该访问哪个分区，而不用扫描整个表。如果查询需要的数据行分散在多个分区中，SQL Server使用多个处理器对多个分区进行并行查询。你可以为在创建表的时候就定义分区的索引。 对小索引的搜索或者扫描要比扫描整个表或者一张大表上的索引要快很多。因此，当对大表进行查询，表分区可以产生相当大的性能提升。</p>
<p style="line-height: normal;">　　现在让我们通过一个简单的例子来了解表分区是如何发挥作用的。在这篇文章中，我不想深入到分区的语法细节当中，这些你可以在SQL Server的在线帮助文档中找到。下面的例子基于存储着一个时间报表系统的数据的数据仓库。除了默认的文件组，我另外创建了7个文件组，每一个文件组仅包含一个文件，这个文件将存储由分区函数定义的一部分数据。</p>
<p style="line-height: normal;">　　为了测试表分区的性能提升，我向这个分区表中插入了一千五百万行，同时向另外一个具有相同表结构、但是没有进行分区的表插入了同样的数据。对分区表执行的INSERT语句运行的更快一些。甚至在我的内存不到1G的笔记本电脑上，对分区表的INSERT语句比不分区的表的INSERT语句要快上三倍。当然，查询的执行时间依据硬件资源的差异而所有变化，但是你还是能够在你的环境中感到不同程度的提升。</p>
<p style="line-height: normal;">　　我将检查更深入了一步，通过分别检查同一条返回所有行的、简单SELECT语句在分区表和非分区表上的执行计划，返回的数据范围通过WHERE语句来指定。同一条语句在这两个不同的表上有不同的执行计划。对于分区表的查询显示出一个嵌套的循环和索引的扫描。从本质上来说，SQL Server将两个分区视为独立的表，因此使用一个嵌套循环将它们连接起来。对非分区的表的同一个查询则使用索引扫描来返回同样的列。当你使用同样的分区策略创建多个表，同时在查询中连接这些表，那么性能上的提升会更加明显</p>
<p style="line-height: normal;">你可以使用下面的查询来了解每一个分区中的行的个数：</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">　　SELECT $PARTITION.TimeEntryDateRangePFN(time_entry_date) AS Partition,&nbsp;<br style="line-height: normal;" />
            COUNT(*) AS [COUNT] FROM fact_time_entry&nbsp;<br style="line-height: normal;" />
            GROUP BY $PARTITION.TimeEntryDateRangePFN(time_entry_date)&nbsp;<br style="line-height: normal;" />
            ORDER BY Partition</td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　表分区对交易环境和数据仓库环境来说，都是一个重要的特征。数据仓库用户最主要的抱怨是移动事实表(fact table)会花费太多时间。当装载数据到事实表的时候，用户查询(立方体处理查询)的性能会明显下降，甚至是完全无法成功。因此，装载大量的数据到事实表的时候常常需要停机。如果使用表分区，就不再出现这样的情况——确切的讲，你一眨眼的工夫就可以移动事实表。为了演示这是如何生效的，我使用上面例子中相同的分区函数和表结构来创建一个新的表，这个表叫做fact_time_entry2。表的主键从五千万开始，这样fact_time_entry2就不会包含表fact_time_entry中已经有的数据。</p>
<p style="line-height: normal;">　　现在我把2007年的数据移动到这张fact_time_entry2中。同时让我们假设fact_time_entry表中包含着2007年之前的数据。在fact_time_entry2表完成数据的转移，我执行下面的语句：</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">　　ALTER TABLE fact_time_entry2&nbsp;<br style="line-height: normal;" />
            SWITCH PARTITION 8 TO fact_time_entry PARTITION 8</td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　这条语句将编号为8的分区，这个分区恰好包含着2007年的数据，从fact_time_entry2移动到了fact_time_entry表中，在我的笔记本电脑上，这个过程只花费了3毫秒。在这短短的3毫秒中，我的事实表就增加了五百万条记录!的确，我需要在交换分区之前，将数据移动到中间表，但是我的用户不需要担心——事实表随时都可以查询!在这幕后，实际上没有数据移动——只是两张表的元数据发生了变化。</p>
<p style="line-height: normal;">　　我可以使用类似的查询删除事实表中不在需要的数据。例如，假设我们决定我们不再关心2004年的记录。下面的语句可以将这些记录转移到我们创建的工作表中：</p>
<p style="line-height: normal;">
<table style="table-layout: fixed; line-height: normal; border: 1px dotted #cccccc;" width="95%" align="center" border="0" cellpadding="6" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="font-family: Verdana,Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;">
            <td style="font-family: Arial; word-wrap: break-word; visibility: visible ! important; filter: none; color: #323234 ! important; font-size: 12px; line-height: normal;" bgcolor="#f3f3f3">　　ALTER TABLE fact_time_entry&nbsp;<br style="line-height: normal;" />
            SWITCH PARTITION 2 TO fact_time_entry2 PARTITION 2</td>
        </tr>
    </tbody>
</table>
</p>
<p style="line-height: normal;">　　这样的语句依旧在毫秒级内完成了。现在，我可以删除fact_time_entry2或者将它移到其他的服务器上。我的事实表不会包含2004年的任何记录。这个分区还是需要在目的表中存在，而且它必须是空的。你不能将分区转移到一个包含重复数据的表中。源表和目的表的分区必须一致，同时被转移的数据必须在同一个文件组中。即使受到这么多的限制，转换分区和无需停机就可以移动数据表的功能必将让数据仓库的实现变的前所未有的轻松。</p>
<p style="line-height: normal;">SQL Server 表分区(partitioned table/Data Partitioning)<br style="line-height: normal;" />
Partitioned Table<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
可伸缩性性是数据库管理系统的一个很重要的方面，在SQL Server 2005中可伸缩性方面提供了表分区功能。<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
其实对于有关系弄数据库产品来说，对表、数据库和服务器进行数据分区的从而提供大数据量的支持并不是什么新鲜事，但 SQL Server 2005 提供了一个新的体系结构功能，用于对数据库中的文件组进行表分区。水平分区可根据分区架构，将一个表划分为几个较小的分组。表分区功能是针对超大型数据库（从数百吉字节到数千吉字节或更大）而设计的。超大型数据库 (VLDB) 查询性能通过分区得到了改善。通过对广大分区列值进行分区，可以对数据的子集进行管理，并将其快速、高效地重新分配给其他表。<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
设想一个大致的电子交易网站，有一个表存储了此网站的历史交易数据，这此数据量可能有上亿条，在以前的SQL Server版本中存储在一个表中不管对于查询性能还是维护都是件麻烦事，下面我们来看一下在SQL Server2005怎么提高性能和可管理性：<br style="line-height: normal;" />
-- 创建要使用的测试数据库，Demo<br style="line-height: normal;" />
USE [master]<br style="line-height: normal;" />
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'DEMO')<br style="line-height: normal;" />
DROP DATABASE [DEMO]<br style="line-height: normal;" />
CREATE DATABASE [DEMO]<br style="line-height: normal;" />
--由于表分区使用使用新的体系结构，使用文件组来进行表分区，所以我们创建将要用到的6个文件组，来存储6个时间段的交易数据[&lt;2000],[ 2001], [2002], [2003], [2004], [&gt;2005]<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG1;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG2;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG3;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG4;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG5;&nbsp;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILEGROUP YEARFG6;&nbsp;<br style="line-height: normal;" />
-- 下面为这些文件组添加文件来进行物理的数据存储<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF1', FILENAME = 'C:"ADVWORKSF1.NDF') TO FILEGROUP YEARFG1;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF2', FILENAME = 'C:"ADVWORKSF2.NDF') TO FILEGROUP YEARFG2;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF3', FILENAME = 'C:"ADVWORKSF3.NDF') TO FILEGROUP YEARFG3;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF4', FILENAME = 'C:"ADVWORKSF4.NDF') TO FILEGROUP YEARFG4;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF5', FILENAME = 'C:"ADVWORKSF5.NDF') TO FILEGROUP YEARFG5;<br style="line-height: normal;" />
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF6', FILENAME = 'C:"ADVWORKSF6.NDF') TO FILEGROUP YEARFG6;<br style="line-height: normal;" />
-- HERE WE ASSOCIATE THE PARTITION FUNCTION TO&nbsp;<br style="line-height: normal;" />
-- THE CREATED FILEGROUP VIA A PARTITIONING SCHEME<br style="line-height: normal;" />
USE DEMO;<br style="line-height: normal;" />
GO<br style="line-height: normal;" />
-------------------------------------------------------<br style="line-height: normal;" />
-- 创建分区函数<br style="line-height: normal;" />
-------------------------------------------------------<br style="line-height: normal;" />
CREATE PARTITION FUNCTION YEARPF(datetime)<br style="line-height: normal;" />
AS&nbsp;<br style="line-height: normal;" />
RANGE LEFT FOR VALUES ('01/01/2000'<br style="line-height: normal;" />
,'01/01/2001'<br style="line-height: normal;" />
,'01/01/2002'<br style="line-height: normal;" />
,'01/01/2003'<br style="line-height: normal;" />
,'01/01/2004')<br style="line-height: normal;" />
-------------------------------------------------------<br style="line-height: normal;" />
-- 创建分区架构<br style="line-height: normal;" />
-------------------------------------------------------<br style="line-height: normal;" />
CREATE PARTITION SCHEME YEARPS<br style="line-height: normal;" />
AS PARTITION YEARPF TO (YEARFG1, YEARFG2,YEARFG3,YEARFG4,YEARFG5,YEARFG6)<br style="line-height: normal;" />
-- 创建使用此Schema的表<br style="line-height: normal;" />
CREATE TABLE PARTITIONEDORDERS<br style="line-height: normal;" />
(<br style="line-height: normal;" />
ID INT NOT NULL IDENTITY(1,1),<br style="line-height: normal;" />
DUEDATE DATETIME NOT NULL,<br style="line-height: normal;" />
) ON YEARPS(DUEDATE)<br style="line-height: normal;" />
--为此表填充数据<br style="line-height: normal;" />
declare @DT datetime<br style="line-height: normal;" />
SELECT @DT = '1999-01-01'<br style="line-height: normal;" />
--start looping, stop at ending date<br style="line-height: normal;" />
WHILE (@DT &lt;= '2005-12-21')<br style="line-height: normal;" />
BEGIN<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;
INSERT INTO PARTITIONEDORDERS VALUES(@DT)&nbsp;<br style="line-height: normal;" />
&nbsp;&nbsp;
SET @DT=dateadd(yy,1,@DT)<br style="line-height: normal;" />
END<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-- 现在我们可以看一下我们刚才插入的行都分布在哪个Partition&nbsp;<br style="line-height: normal;" />
SELECT *, $PARTITION.YEARPF(DUEDATE) FROM PARTITIONEDORDERS<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--我们可以看一下我们现在PARTITIONEDORDERS表的数据存储在哪此partition中，以及在这些分区中数据量的分布<br style="line-height: normal;" />
SELECT * FROM SYS.PARTITIONS WHERE OBJECT_ID = OBJECT_ID('PARTITIONEDORDERS')<br style="line-height: normal;" />
--<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--现在我们设想一下，如果我们随着时间的流逝，现在已经到了2005年，按照我们先前的设定，我们想再想入一个分区，这时是不是重新创建表分区架构然后重新把数据导放到新的分区架构呢，答案是完全不用。下面我们就看如果新加一个分区。<br style="line-height: normal;" />
&nbsp; &nbsp; &nbsp;&nbsp;
--更改分区架构定义语言，让下一个分区使用和现在已经存在的分区YEARFG6分区中，这样此分区就存储了两段partition的数据。<br style="line-height: normal;" />
ALTER PARTITION SCHEME YEARPS<br style="line-height: normal;" />
NEXT USED YEARFG6;<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--更改分区函数<br style="line-height: normal;" />
ALTER PARTITION FUNCTION YEARPF()<br style="line-height: normal;" />
SPLIT RANGE ('01/01/2005')&nbsp;<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--现在我们可以看一下我们刚才插入的行都分布在哪个Partition？<br style="line-height: normal;" />
SELECT *, $PARTITION.YEARPF(DUEDATE) FROM PARTITIONEDORDERS<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--我们可以看一下我们现在PARTITIONEDORDERS表的数据存储在哪此partition中，以及在这些分区中</p>
</span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/330805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-03 09:38 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/03/330805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分享SQL2005分区实现教程</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/03/330803.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Fri, 03 Sep 2010 01:26:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/03/330803.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330803.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/03/330803.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330803.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330803.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 以前的一次技术例会内容，拿出来共享一下，大家有问题可以提出来，一起提高。技术会议－&nbsp;SQL Server PartitioningV2※高捷本月技术会议专题为数据库分区（&nbsp;SQL Server Partitioning&nbsp;），主要讲述为什么要分区，在什么情况下需要对数据进行分区，如何进行分区，分区表管理等内容。一、&nbsp;摘要◆&nbsp;&nb...&nbsp;&nbsp;<a href='http://www.blogjava.net/jjshcc/archive/2010/09/03/330803.html'>阅读全文</a><img src ="http://www.blogjava.net/jjshcc/aggbug/330803.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-03 09:26 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/03/330803.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQLServer海量数据库的查询优化及分页算法方案</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/02/330734.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 02 Sep 2010 07:31:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/02/330734.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330734.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/02/330734.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330734.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330734.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 随着&#8220;金盾工程&#8221;建设的逐步深入和公安信息化的高速发展，公安计算机应用系统被广泛应用在各警种、各部门。与此同时，应用系统体系的核心、系统数据的存放地――数据库也随着实际应用而急剧膨胀，一些大规模的系统，如人口系统的数据甚至超过了1000万条，可谓海量。那么，如何实现快速地从这些超大容量的数据库中提取数据（查询）、分析、统计以及提取数据后进行数据分页已成为各地系统管理员和数据库...&nbsp;&nbsp;<a href='http://www.blogjava.net/jjshcc/archive/2010/09/02/330734.html'>阅读全文</a><img src ="http://www.blogjava.net/jjshcc/aggbug/330734.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-02 15:31 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/02/330734.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQlserver2005清除日志的方法</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/02/330726.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 02 Sep 2010 06:41:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/02/330726.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330726.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/02/330726.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330726.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330726.html</trackback:ping><description><![CDATA[<p><font style="background-color: #cce8cf">清除日志：</font></p>
<p><font style="background-color: #cce8cf">&nbsp;<br />
DECLARE @LogicalFileName sysname,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @MaxMinutes INT,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @NewSize INT<br />
USE&nbsp;&nbsp;&nbsp;&nbsp; szwzcheck&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- 要操作的数据库名<br />
SELECT&nbsp; @LogicalFileName = 'szwzcheck_Log',&nbsp; -- 日志文件名<br />
@MaxMinutes = 10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Limit on time allowed to wrap log.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @NewSize = 20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- 你想设定的日志文件的大小(M)<br />
-- Setup / initialize<br />
DECLARE @OriginalSize int<br />
SELECT @OriginalSize = size <br />
&nbsp; FROM sysfiles<br />
&nbsp; WHERE name = @LogicalFileName<br />
SELECT 'Original Size of ' + db_name() + ' LOG is ' + <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' + <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'<br />
&nbsp; FROM sysfiles<br />
&nbsp; WHERE name = @LogicalFileName<br />
CREATE TABLE DummyTrans<br />
&nbsp; (DummyColumn char (8000) not null)<br />
DECLARE @Counter&nbsp;&nbsp; INT,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @StartTime DATETIME,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @TruncLog&nbsp; VARCHAR(255)<br />
SELECT&nbsp; @StartTime = GETDATE(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'<br />
DBCC SHRINKFILE (@LogicalFileName, @NewSize)<br />
EXEC (@TruncLog)<br />
-- Wrap the log if necessary.<br />
WHILE&nbsp;&nbsp;&nbsp;&nbsp; @MaxMinutes &gt; DATEDIFF (mi, @StartTime, GETDATE()) -- time <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = <br />
@LogicalFileName)&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND (@OriginalSize * 8 /1024) &gt; @NewSize&nbsp; <br />
&nbsp; BEGIN -- Outer loop.<br />
&nbsp;&nbsp;&nbsp; SELECT @Counter = 0<br />
&nbsp;&nbsp;&nbsp; WHILE&nbsp; ((@Counter &lt; @OriginalSize / 16) AND (@Counter &lt; 50000))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BEGIN -- update<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INSERT DummyTrans VALUES ('Fill Log')&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DELETE DummyTrans<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT @Counter = @Counter + 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; EXEC (@TruncLog)&nbsp; <br />
&nbsp; END&nbsp;&nbsp; <br />
SELECT 'Final Size of ' + db_name() + ' LOG is ' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONVERT(VARCHAR(30),size) + ' 8K pages or ' + <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'<br />
&nbsp; FROM sysfiles <br />
&nbsp; WHERE name = @LogicalFileName<br />
DROP TABLE DummyTrans<br />
SET NOCOUNT OFF<br />
&nbsp;<br />
把szwzcheck换成你数据库的名字即可，在查询分析器里面运行。 </font></p>
<img src ="http://www.blogjava.net/jjshcc/aggbug/330726.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-02 14:41 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/02/330726.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松识别重复索引</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/02/330708.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 02 Sep 2010 05:39:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/02/330708.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330708.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/02/330708.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330708.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330708.html</trackback:ping><description><![CDATA[<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 现在SQL2005提供了DTA的工具，大家在去优化一个语句时都有意无意的使用此工具所给出的一些优化建议。不过它始终是个工具，所给出的优化建议很多时候都是使用2005新的索引功能INCLUDE把查询列表统统包括在一个索引中。因此，每个开发人员所定义的索引就会存在重复或是很相似的地方。因为索引页的数据比较密集，因此在对包含有索引列的字段做修改操作时，都会去相应的修改包含此键值列的索引。理论上对一张表多加一个索引，修改数据的速度就会比原来慢1.2倍。因此，这会增加记录被锁定的时间，从而也就会影响到查询的性能。</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 但是，如果通过SQL2005提供的几个与索引相关的视图，我们不能很方便的观察出索引所包含的键值列和它的包含列是哪些。同时，如果表是分区表，通过sys.partitions查看总记录数时要累加各分区的行数。</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下面的脚本可以组合这些视图，查询出对象名称、对象类型（表或索引视图）、索引名称、索引编号、索引类型、是否主键、是否唯一、填充度、键值字段、包含字段、表的总记录数（取各分区中行的总数）、索引描述，如下图部分显示结果所示，这样就很方便的判断出哪些索引是重复或相似的：</font></p>
<font style="background-color: #cce8cf">
<p><img border="0" alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/110408_0836_1.png" width="701" height="93" /><br />
对取包含字段时用到了FOR XML PATH这个功能，可以方便的把包含列组织成A,B,C的形式。然后使用CROSS APPLY得出最终的结果。脚本定义如下：</p>
<p><br />
USE AdventureWorks;<br />
GO<br />
DROP INDEX IX_SalesOrderHeader_CustomerID ON Sales.SalesOrderHeader<br />
GO<br />
CREATE INDEX IX_SalesOrderHeader_CustomerID ON Sales.SalesOrderHeader(CustomerID)<br />
INCLUDE(ShipDate,Freight)<br />
GO</p>
<p>--sp_helpindex不能反应出包含字段<br />
EXEC sp_helpindex 'Sales.SalesOrderHeader'<br />
GO<br />
--SQL2005下用于诊断索引重复的脚本<br />
DECLARE @Result TABLE(<br />
&nbsp;&nbsp;&nbsp; objname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sysname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; objtype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char(2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; indexname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sysname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; index_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; indextype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tinyint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; is_primary_key&nbsp; bit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; is_unique&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; fill_factor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tinyint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; IndexKeys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nvarchar(2126)&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; Included&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nvarchar(max)&nbsp;&nbsp;&nbsp; NULL,<br />
&nbsp;&nbsp;&nbsp; rows&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bigint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; IndexDesc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar(210)&nbsp;&nbsp;&nbsp; NULL<br />
)</p>
<p><br />
CREATE TABLE #IndexInfo<br />
(<br />
&nbsp;&nbsp;&nbsp; IndexName&nbsp;&nbsp;&nbsp; sysname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; IndexDesc&nbsp;&nbsp;&nbsp; varchar(210)&nbsp;&nbsp;&nbsp;&nbsp; NULL,<br />
&nbsp;&nbsp;&nbsp; IndexKeys&nbsp;&nbsp;&nbsp; nvarchar(2126)&nbsp;&nbsp;&nbsp;&nbsp; NULL<br />
)<br />
DECLARE @objname&nbsp;&nbsp;&nbsp;&nbsp; sysname</p>
<p>DECLARE ObjectList CURSOR FAST_FORWARD FOR<br />
&nbsp;&nbsp;&nbsp; SELECT SCHEMA_NAME(o.schema_id)+'.'+o.name AS objname<br />
&nbsp;&nbsp;&nbsp; FROM sys.indexes i JOIN sys.objects o ON i.object_id=o.object_id<br />
&nbsp;&nbsp;&nbsp; WHERE o.type IN('U','V') AND i.index_id IN(0,1) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --AND o.object_id=OBJECT_ID(N'Sales.SalesOrderHeader')</p>
<p>OPEN ObjectList<br />
FETCH NEXT FROM ObjectList INTO @objname</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
&nbsp;&nbsp;&nbsp; INSERT INTO #IndexInfo EXEC sp_helpindex @objname--使用全名称，防止直接使用表名称时无法获取其它架构表的信息<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; INSERT INTO @Result<br />
&nbsp;&nbsp;&nbsp;&nbsp; SELECT SCHEMA_NAME(o.schema_id)+'.'+o.name AS objname, o.type AS objtype,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i.name AS indexname,i.index_id,i.type AS indextype,i.is_primary_key,i.is_unique,i.fill_factor,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.IndexKeys,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.name AS Included,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p.rows,t.IndexDesc<br />
&nbsp;&nbsp;&nbsp; FROM sys.indexes i<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INNER JOIN sys.objects o ON i.object_id=o.object_id<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INNER JOIN #IndexInfo t ON t.IndexName=i.name<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CROSS APPLY (SELECT SUM(rows) AS rows <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.partitions p<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE p.index_id = i.index_id AND p.object_id = i.object_id<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) p<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CROSS APPLY (SELECT name=STUFF((SELECT N',' + QUOTENAME(y) AS [text()]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM (SELECT c.name AS y<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.index_columns ic<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JOIN sys.columns c ON ic.column_id=c.column_id AND ic.object_id=c.object_id<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE ic.object_id=i.object_id AND ic.index_id=i.index_id&nbsp; AND ic.is_included_column=1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) AS Y<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY y FOR XML PATH('')), 1, 1, N'')<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) c<br />
&nbsp;&nbsp;&nbsp; WHERE o.object_id=OBJECT_ID(@objname)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; TRUNCATE TABLE #IndexInfo<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp; FETCH NEXT FROM ObjectList INTO @objname<br />
END</p>
<p>CLOSE ObjectList<br />
DEALLOCATE ObjectList</p>
<p>DROP TABLE #IndexInfo<br />
SELECT * FROM @Result ORDER BY objname,index_id<br />
用于SQL2000的脚本：</p>
<p>--SQL2000下用于诊断索引重复的脚本<br />
DECLARE @Result TABLE (<br />
&nbsp;&nbsp;&nbsp; [objname] [sysname] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [indexname] [sysname] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [indid] [smallint] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [IsUnique] [int] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [IndexKeys] [nvarchar] (2126) NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [rowcnt] [bigint] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [rowmodctr] [int] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [keycnt] [smallint] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [OrigFillFactor] [tinyint] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [dpages] [int] NOT NULL ,<br />
&nbsp;&nbsp;&nbsp; [IndexDesc] [varchar] (210) NULL <br />
)</p>
<p>CREATE TABLE #IndexInfo<br />
(<br />
&nbsp;&nbsp;&nbsp; IndexName&nbsp;&nbsp;&nbsp; sysname&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL,<br />
&nbsp;&nbsp;&nbsp; IndexDesc&nbsp;&nbsp;&nbsp; varchar(210)&nbsp;&nbsp;&nbsp;&nbsp; NULL,<br />
&nbsp;&nbsp;&nbsp; IndexKeys&nbsp;&nbsp;&nbsp; nvarchar(2126)&nbsp;&nbsp;&nbsp;&nbsp; NULL<br />
)<br />
DECLARE @objname&nbsp;&nbsp;&nbsp;&nbsp; sysname,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @objid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int</p>
<p>DECLARE ObjectList CURSOR FAST_FORWARD FOR <br />
&nbsp;&nbsp;&nbsp; SELECT USER_NAME(o.uid)+'.'+o.name AS objname,o.id AS objid<br />
&nbsp;&nbsp;&nbsp; FROM dbo.sysobjects o JOIN dbo.sysindexes i ON i.id = o.id<br />
&nbsp;&nbsp;&nbsp; WHERE o.type IN( 'U','V') AND i.indid IN(0,1) AND o.name&lt;&gt;'dtproperties'--用于保存关系图的系统表<br />
&nbsp;&nbsp;&nbsp; ORDER BY o.name,o.uid </p>
<p>OPEN ObjectList<br />
FETCH NEXT FROM ObjectList INTO @objname,@objid</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
&nbsp;&nbsp;&nbsp; INSERT INTO #IndexInfo EXEC sp_helpindex @objname--使用全名称，防止直接使用表名称时无法获取其它用户表的信息<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; INSERT INTO @Result<br />
&nbsp;&nbsp;&nbsp;&nbsp; SELECT USER_NAME(o.uid)+'.'+o.name AS objname, i.name AS indexname, i.indid, <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CASE WHEN t.IndexDesc LIKE '%unique%' THEN 1 ELSE 0 END AS IsUnique,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.IndexKeys, i.rowcnt, i.rowmodctr, i.keycnt, i.OrigFillFactor, i.dpages,t.IndexDesc<br />
&nbsp;&nbsp;&nbsp; FROM dbo.sysindexes i<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INNER JOIN dbo.sysobjects o ON i.id = o.id<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INNER JOIN #IndexInfo t ON t.IndexName=i.name<br />
&nbsp;&nbsp;&nbsp; WHERE o.id=@objid<br />
&nbsp;&nbsp;&nbsp; TRUNCATE TABLE #IndexInfo<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp; FETCH NEXT FROM ObjectList INTO @objname,@objid<br />
END</p>
<p>CLOSE ObjectList<br />
DEALLOCATE ObjectList</p>
<p>DROP TABLE #IndexInfo<br />
SELECT * FROM @Result ORDER BY objname,indid</font></p>
<img src ="http://www.blogjava.net/jjshcc/aggbug/330708.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-02 13:39 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/02/330708.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>提升SQL Server速度整理索引碎片</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/02/330685.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 02 Sep 2010 03:28:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/02/330685.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330685.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/02/330685.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330685.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330685.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="line-height: 22px; font-size: 14px" class="Apple-style-span">&nbsp;&nbsp;&nbsp; 数据库表A有十万条记录，查询速度本来还可以，但导入一千条数据后，问题出现了。当选择的数据在原十万条记录之间时，速度还是挺快的；但当选择的数据在这一千条数据之间时，速度变得奇慢。<span class="Apple-converted-space">&nbsp;</span><br />
&nbsp;&nbsp;&nbsp; 凭经验，这是索引碎片问题。检查索引碎片DBCC SHOWCONTIG(表)，得到如下结果：<span class="Apple-converted-space">&nbsp;</span><br />
DBCC SHOWCONTIG 正在扫描 'A' 表...<span class="Apple-converted-space">&nbsp;</span><br />
表: 'A'（884198200）；索引 ID: 1，数据库 ID: 13<span class="Apple-converted-space">&nbsp;</span><br />
已执行 TABLE 级别的扫描。<span class="Apple-converted-space">&nbsp;</span><br />
- 扫描页数.....................................: 3127<span class="Apple-converted-space">&nbsp;</span><br />
- 扫描扩展盘区数...............................: 403<span class="Apple-converted-space">&nbsp;</span><br />
- 扩展盘区开关数...............................: 1615<span class="Apple-converted-space">&nbsp;</span><br />
- 每个扩展盘区上的平均页数.....................: 7.8<span class="Apple-converted-space">&nbsp;</span><br />
- 扫描密度［最佳值:实际值］....................: 24.20%［391:1616］<span class="Apple-converted-space">&nbsp;</span><br />
- 逻辑扫描碎片.................................: 68.02%<span class="Apple-converted-space">&nbsp;</span><br />
- 扩展盘区扫描碎片.............................: 38.46%<span class="Apple-converted-space">&nbsp;</span><br />
- 每页上的平均可用字节数.......................: 2073.2<span class="Apple-converted-space">&nbsp;</span><br />
- 平均页密度（完整）...........................: 74.39%<span class="Apple-converted-space">&nbsp;</span><br />
DBCC 执行完毕。如果 DBCC 输出了错误信息，请与系统管理员联系。<span class="Apple-converted-space">&nbsp;</span><br />
&nbsp;&nbsp;&nbsp; 由上我们看出，逻辑扫描碎片和扩展盘区扫描碎片都非常大，果真需要对索引碎片进行处理了。<span class="Apple-converted-space">&nbsp;</span><br />
一般有两种方法解决，一是利用DBCC INDEXDEFRAG整理索引碎片，二是利用DBCC DBREINDEX重建索引。二者各有优缺点。调用微软的原话如下：<span class="Apple-converted-space">&nbsp;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; DBCC INDEXDEFRAG 命令是联机操作，所以索引只有在该命令正在运行时才可用。而且可以在不丢失已完成工作的情况下中断该操作。这种方法的缺点是在重新组织数据方面没有聚集索引的除去/重新创建操作有效。<span class="Apple-converted-space">&nbsp;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; 重新创建聚集索引将对数据进行重新组织，其结果是使数据页填满。填满程度可以使用 FILLFACTOR 选项进行配置。这种方法的缺点是索引在除去/重新创建周期内为脱机状态，并且操作属原子级。如果中断索引创建，则不会重新创建该索引。<span class="Apple-converted-space">&nbsp;</span><br />
也就是说，要想获得好的效果，还是得用重建索引，所以决定重建索引。<span class="Apple-converted-space">&nbsp;</span><br />
DBCC DBREINDEX(表，索引名，填充因子）<span class="Apple-converted-space">&nbsp;</span><br />
第一个参数，可以是表名，也可以是表ID。<span class="Apple-converted-space">&nbsp;</span><br />
第二个参数，如果是''，表示影响该表的所有索引。<span class="Apple-converted-space">&nbsp;</span><br />
第三个参数，<span style="color: red">填充因子，即索引页的数据填充程度。如果是100，表示每一个索引页都全部填满，此时select效率最高，但以后要插入索引时，就得移动后面的所有页，效率很低。如果是0，表示使用先前的填充因子值。<span class="Apple-converted-space">&nbsp;</span><br />
</span>DBCC DBREINDEX(A，''，100)<span class="Apple-converted-space">&nbsp;</span><br />
重新测试查询速度，飞快。</span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/330685.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-02 11:28 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/02/330685.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为SQLSERVER打好地基－硬盘碎片和索引碎片</title><link>http://www.blogjava.net/jjshcc/archive/2010/09/02/330676.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 02 Sep 2010 02:23:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/09/02/330676.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/330676.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/09/02/330676.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/330676.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/330676.html</trackback:ping><description><![CDATA[<div>数据文件的碎片</div>
<div>&nbsp;&nbsp; &nbsp;影响磁盘读取性能的两个主要因素：录道时间和轮询延迟。</div>
<div>&nbsp;&nbsp; &nbsp;我们在查询数据时，有两种磁盘的读取方式：顺序读和随机读。<span style="color: red; ">随机读发生在对表或索引的扫描时，顺序读发生在使用索引查找数据时</span>。<span style="color: red; ">当数据文件有大量碎片时，随机读不会受到太大的影响，因为SQLSERVER会根据表所占用到的数据页面，不管记录的逻辑顺序随机的读取出来，所谓的预读正是这种方式。而顺序读时，因为要按记录的逻辑顺序读取相应的记录，如果逻辑上相邻的数据页在物理分布上不连续，则会因为磁头的来回移动使性能大打折扣。这也就是为什么有时我们看到表扫描比索引查找效率更高的原因。</span></div>
<div>&nbsp;&nbsp; &nbsp;我们在创建数据库时，会为数据文件和日志文件分别指定一个初始大小和增量大小。<span style="color: red; ">如果这些文件都在独自的逻辑分区中，那么不会有磁盘碎片的产生</span>。但是，如果每个文件所在的分区中还有其它的数据库文件。则因为这些文件的自增长就会产生磁盘碎片了，如下图所示：</div>
<div>&nbsp;<img src="http://www.blogjava.net/images/blogjava_net/jjshcc/070808_1856_1.png" width="258" height="105" alt="" /></div>
<div>&nbsp;&nbsp; &nbsp;为了防止这些碎片的产生，我们应该每次把文件自增长的大小设置的更大些，以防止产生这么多小的碎片。但是，如果每次文件增长的过大，特别是在系统繁忙的时候，势必会影响数据库的性能。为了能快速的完全文件增长的工作，SQLSERVER借助WINDOWS的即时文件初始化功能来快速的完成此项任务。若要使用即时文件初始化，必须在 Windows 帐户下运行 MSSQLSERVER 服务帐户并为该 Windows 帐户分配 Windows SE_MANAGE_VOLUME_NAME 特权。此权限默认情况下分配给 Windows 管理员组。如果拥有系统管理员权限，您可以通过将 Windows 帐户添加到&#8220;执行卷维护任务&#8221;安全策略来分配此权限。默认MSSQLSERVER是在LocalSystem帐号启动的，但此帐号的SE_MANAGE_VOLUME_NAME 特权是被禁用的。详见http://msdn.microsoft.com/en-us/library/ms684190(VS.85).aspx</div>
<div>&nbsp;&nbsp; &nbsp;结论：定期执行磁盘碎片整理并为数据文件分配合适的初始大小。并制定任务计划，在系统空闲时根据现在数据的实际大小调整数据文件的大小，减少对系统繁忙时因为文件增长带来的开销。</div>
<div>日志文件的碎片</div>
<div>&nbsp;&nbsp; 不同于数据文件，日志文件不能使用即时文件初始化进行自增长。因此，在分配一个很大自增长量时就会很耗时。在这个操作期间，所有的inset、delete、update操作都会被阻塞。那么随后一断时间数据库的整体性能也会受到很大的影响。就像高速公路突然塞车被疏导之后一样。在系统内部，会把这些日志文件分成好多个虚拟的日志文件(VLF)，你可以使用DBCC LOGINFO来查看你当前的日志文件中有多少个VLF。如果返回的结果数很多，证明你应该对日志进行维护了。这就和数据文件的磁盘碎片一样，会对性能造成严重影响。这个数量是由日志文件的整体大小和扩张日志使用的增量在内部决定的，我们无法控制。</div>
<div>&nbsp;&nbsp; &nbsp;但是，因为日志是顺序写入的，真正的磁盘碎片对性能影响其实不是很大。如果你的增量设置过小，会因为频繁的调整日志文件而影响到VLF。如果你设置的增量过大，又会占用过长的文件分配时间。因此，最好的办法就是你控制你的事务尽可能的短。同时，定期的备份你的日志，以使日志可以截断。从而防止日志文件进行自增长而带来的性能开销。一直以来有种误解就是认为完整恢复模式的数据库不会自动截断事务日志。如果你从来没有对这个数据库做过完整备份，其实它也是可以对事务日志自动截断的。</div>
<div>结论：VLF越少越好，建议的数值是不超过5个。定时对事务日志进行备份，以最快截断以供后续使用。</div>
<div>索引的内部和外部碎片</div>
<div>&nbsp;&nbsp; &nbsp;这些碎片都是逻辑上的碎片。整天都在讨论索引碎片，相信这个大家应该都很清楚了。不再多罗嗦，概括如下：内部碎片受页面填充度的影响，如果碎片过多使表所占的实际页面数比无碎片时多出很多。因此在表扫描时会发生更多的I/O操作，但是索引查找时不会受到很大影响。外部碎片是因为页面的逻辑顺序和硬盘上的物理顺序不一致或是分区的不连续所造成的。这时，如果使用索引进行范围查找的话，因为要按照记录的逻辑顺序进行记取，会引起磁头来回移动。关于索引碎片的维护，请参见联机文档。</div>
<div>文件的目录存储及文件名要求</div>
<div>&nbsp;&nbsp; &nbsp;在目录中新建、访问、删除文件时，都会在目录的元数据中进行相应的搜索或执行Chkdsk.exe命令完成相应的任务。因此，如果文件过多或是目录层次太多，会花费更长的时间完成。建议文件数目不超过100,000，当然我们很多时候永远达不到这个数目。同时，Windwos NT之后的版本，为了提供向后兼容性，在你对目录中的任何文件修改之后，不符合8.3文件格式的长文件名都会生成一个8.3格式文件名。如果你的目录中有上百个长文件名的文件，这会带来一定的性能损失。因此，如果机器上没有运行16位的程序，可通过注册表把NtfsDisable8dot3NameCreation设置为1，禁止生成8.3文件名。注册表位置如下：HKEY_LOCAL_MACHINE"SYSTEM"CurrentControlSet"Control"FileSystem"NtfsDisable8dot3NameCreation。那么日志文件和数据文件是在什么时候才会被修改呢？如果你不怕葬你的硬盘，运行每个脚本之前创 建一个新的Northwind数据库。你可以运行一下下面的脚本，此例也正好演示一下insert into和select into的效率问题。</div>
<div>USE Northwind;</div>
<div>GO</div>
<div>select * into my_customers</div>
<div>from dbo.Customers where 1=0</div>
<div>GO</div>
<div>insert into my_customers</div>
<div>select c1.*</div>
<div>from dbo.Customers c1,dbo.Customers c2,dbo.Customers c3</div>
<div>--观察运行前后的数据文件和日志文件的增长</div>
<div>--insert into被完整记录于日志中，我们发现</div>
<div>--日志文件增长了很大，我的长到了500M多</div>
<div>--在新建Northwind数据库后，运行下面的脚本</div>
<div>--select into作为一个大批量操作，只记录了部分事务</div>
<div>--因此日志增长不是很大，我的长到了4M</div>
<div>--因此从性能上来说select into效率高于insert into</div>
<div>select c1.*</div>
<div>into my_customers</div>
<div>from dbo.Customers c1,dbo.Customers c2,dbo.Customers c3&nbsp;</div>
<div>硬盘格式化的簇大小设置</div>
<div>&nbsp;&nbsp; &nbsp; 客户给我们一台新的服务器，我们可以最大调整的就是硬盘。CPU、内存就摆在那了，客户说没有更好的机器了。同时，硬盘的I/O效率也是影响查询性能的关键因素。SQL2005对tempdb的要求越来越高，如果条件允许，一般把tempdb、数据文件、索引文件、全文目录都分别存放在独立的RAID5阵列中（有时MSFTESQL服务会因为磁盘I/O过高而暂停服务），日志文件则存放在RAID1+0或RAID1中，操作系统和SQLSERVER存放于RAID1中。硬盘的扇区大小默认是512个字节，那么我们在对新的硬盘进行格式化时，选择的簇的大小多少才是最合适的？阵列的条带容量大小应该设置为多少？</div>
<div>&nbsp;&nbsp; &nbsp;因为一个数据页面是8K，数据页面在内部由扩展分区进行管理。一个扩展分区包含了8个逻辑连续的页面。分区的管理是通过全局分配映射页面（GAM，只保存超过8个页面的表，统一分区）和共享全局分配映射页面（SGAM，保存小于8个数据页面的表，混合分区）来进行管理的，一个数据文件的第2个页面是GAM，第3个页面是SGAM。每个GAM和SGAM能管理的页面范围是4G，每4G都会增加一个GAM和SGAM。在你创建一个新的数据库是，使用DBCC PAGE命令来观察这两个页面，可以看到数据库已经分配了很多扩展分区，还保留了一些分区。在创建表时，新加记录后，如果表总共占用不到8个数据页面的话会被分配到SGAM中，超过8个页面时才会被分配到GAM分区中。前面我们提到过索引的外部碎片是因为页面的逻辑顺序和硬盘上的物理顺序不一致或是分区的不连续所造成的。因此，如果我们把簇的大小设置为64K时，正好和一个分区大小一样，那么这个分区一旦被某个表所使用后，就不能被另外的表所使用了。从而减少了数据页面的外部碎片，但是分区的不连续还是不能避免。那么把簇大小设为128K呢？因为读取数据时，磁盘是按簇的大小进行读取的。设置簇过大，会一次读取出很多无用的内容。即便你只读取一条记录，SQLSERVER还是会把记录所在的整个页读取出来。这时，实际的磁盘是读取出了64K。但是因为簇是连续的扇区，因此多读取的这一部分，对性能的影响基本是可以忽略的。因为磁盘主要受寻道和轮询延迟影响。</div>
<div>&nbsp;&nbsp; &nbsp;对于RAID中的条带容量设置，内部的工作机制我现在还不是很清楚。只是通过下面的文档得出的结论256K。但是网上很多介绍的都是说作为数据库应用时应该小于簇的大小，这和下面微软的文档描述不一致。更多内容参见：http://www.microsoft.com/whdc/archive/subsys_perf.mspx</div>
<div>为你的硬盘启动写入缓存</div>
<div>&nbsp;&nbsp; &nbsp;在没有专门缓存控制器时，这会提高磁盘的I/O效率，但是会增加数据丢失的风险。但是并不会造成数据的不一致。我们来看一下事务操作的过程，它采用预写事务日志(WAL)的方式来保证ACID。如图所示：</div>
<div><img src="http://www.blogjava.net/images/blogjava_net/jjshcc/070808_1856_2.png" width="281" height="210" alt="" /><br />
</div>
<div>&nbsp;&nbsp; &nbsp;事务提交后，修改先反应到事务日志中，这时可能会还存在于磁盘缓存中。如果这时突然断电，检查点操作还没有来得急把提交的事务写入数据文件。重启服务后日志文件中的并没有真正包含所提交的事务，redo操作失败了，你提交的事务丢失了。但是如果事务日志从缓存中写入了磁盘后断电，是不会丢失数据的。如果是日志文件保存在缓存中，而数据文件已从缓存中写入了磁盘。这时数据不会丢失，只是日志中看不到你提交的事务记录了。因为写入磁盘时是以8K写入的，也就是16个扇区的操作。如果只完成了部分扇区的写入后，断电了。这时我们就会收到824错误了，因为页面的校验和发生错误致使无法读取出此页了。数据库校验和设置在page_verity选项中，有三个选项可以设置：checksum、torn_page_detection、none。开销依次减少，安全性依次减弱。每次发生校验和错误时，都会在msdb.dbo.suspect_pages中得到一条记录。如果出现这样的错误，而你没有备份，你只能冒着丢失数据的风险执行DBCC命令来忽略掉这一页了。</div>
<div>以上各人见解，如有异议请指正！</div>
<img src ="http://www.blogjava.net/jjshcc/aggbug/330676.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-09-02 10:23 <a href="http://www.blogjava.net/jjshcc/archive/2010/09/02/330676.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>索引 與性能--如何让你的SQL运行得更快(轉)</title><link>http://www.blogjava.net/jjshcc/archive/2010/08/19/329391.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 19 Aug 2010 09:03:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/08/19/329391.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/329391.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/08/19/329391.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/329391.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/329391.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;
如何让你的SQL运行得更快 [推荐！！！] <br />
人们在使用SQL时往往会陷入一个误区，即太关注于所得的结果是否正确，而忽略了不同的实现方法之间可能存在的性能差异，这种性能差异在大型的或是复杂的数据库<br />
环境中（如联机事务处理OLTP或决策支持系统DSS）中表现得尤为明显。笔者在工作实践中发现，不良的SQL往往来自于不恰当的索引设计、不充份的连接条件和不可优化的where子句。在对它们进行适当的优化后，其运行速度有了明显地提高！下面我将从这三个<br />
<br />
方面分别进行总结：<br />
<br />
---- 为了更直观地说明问题，所有实例中的SQL运行时间均经过测试，不超过１秒的均<br />
<br />
表示为（&lt; 1秒）。<br />
<br />
---- 测试环境--<br />
<br />
---- 主机：HP LH II<br />
<br />
---- 主频：330MHZ<br />
<br />
---- 内存：128兆<br />
<br />
---- 操作系统：Operserver5.0.4<br />
<br />
----数据库：Sybase11.0.3<br />
<br />
一、不合理的索引设计<br />
<br />
----例：表record有620000行，试看在不同的索引下，下面几个 SQL的运行情况：<br />
<br />
---- 1.在date上建有一<span style="color: red;">非群集索引</span><br />
<br />
select count(*) from record where date &gt;'19991201' and date &lt; '19991214'and amount &gt;2000 (25秒)<br />
<br />
select date,sum(amount) from record group by date(55秒)<br />
<br />
select count(*) from record where date &gt;'19990901' and place in ('BJ','SH') (27秒)<br />
<br />
---- 分析：<br />
<br />
----date上有大量的重复值，<span style="color: red;">在非群集索引下，数据在物理上随机存放在数据页上，在范围查找时，必须执行一次表扫描才能找到这一范围内的全部行</span>。<br />
<br />
---- 2.在date上的一个<span style="color: red;">群集索引</span><br />
<br />
select count(*) from record where date &gt;'19991201' and date &lt; '19991214' and amount &gt;2000 （14秒）<br />
<br />
select date,sum(amount) from record group by date（28秒）<br />
<br />
select count(*) from record where date &gt;'19990901' and place in ('BJ','SH')（14秒）<br />
<br />
---- 分析：<br />
<br />
---- 在<span style="color: red;">群集索引下，数据在物理上按顺序在数据页上，重复值也排列在一起，因而在范围查找时，可以先找到这个范围的起末点，且只在这个范围内扫描数据页，避免了大范围扫描，提高了查询速度</span>。<br />
<br />
---- 3.在<span style="color: red;">place</span>，date，amount上的组合索引<br />
<br />
select count(*) from record where date &gt;'19991201' and date &lt; '19991214' and amount &gt;2000 （26秒）<br />
<br />
select date,sum(amount) from record group by date（27秒）<br />
<br />
select count(*) from record where date &gt;'19990901' and <span style="color: red;">place</span> in ('BJ', 'SH')（&lt; 1秒）<br />
<br />
---- 分析：<br />
<br />
---- 这是一个不很合理的组合索引，因为它的<span style="color: red;">前导列是place，</span>第一和第二条SQL没有引用place，因此也没有利用上索引；<span style="color: red;">第三个SQL使用了place，且引用的所有列都包含在组合索引中，形成了索引覆盖，所以它的速度是非常快的</span>。<br />
<br />
---- 4.在date，place，amount上的组合索引<br />
<br />
select count(*) from record where date &gt;'19991201' and date &lt; '19991214' and amount &gt;2000(&lt; 1秒)<br />
<br />
select date,sum(amount) from record group by date（11秒）<br />
<br />
select count(*) from record where date &gt;'19990901' and place in ('BJ','SH')（&lt; 1秒）<br />
<br />
---- 分析：<br />
<br />
---- 这是一个合理的组合索引。它将date作为前导列，使每个SQL都可以利用索引，并且在第一和第三个SQL中形成了索引覆盖，因而性能达到了最优。<br />
<br />
---- 5.总结：<br />
<br />
---- 缺省情况下建立的索引是非群集索引，但有时它并不是最佳的；<span style="color: red;">合理的索引设计要建立在对各种查询的分析和预测</span>上。一般来说：<br />
<br />
---- ①.有大量重复值、且经常有范围查询<br />
<br />
（between, &gt;,&lt; ，&gt;=,&lt; =）和order by、group by发生的列，可考虑建立群集索引；<br />
<br />
---- ②.经常同时存取多列，且每列都含有重复值可考虑建立组合索引；<br />
<br />
---- ③.组合索引要尽量使关键查询形成索引覆盖，其<span style="color: red;">前导列一定是使用最频繁的列</span>。<br />
<br />
<br />
<br />
二、不充份的连接条件：<br />
<br />
---- 例：表card有7896行，在<span style="color: red;">card_no上有一个非聚集索引</span>，表account有191122行，在a<span style="color: red;">ccount_no上有一个非聚集索引</span>，试看在不同的表连接条件下，两个SQL的执行情况：<br />
<br />
<br />
<br />
select sum(a.amount) from account a,card b where a.card_no = b.card_no（20秒）<br />
<br />
---- 将SQL改为：<br />
<br />
select sum(a.amount) from account a,card b where a.card_no = b.card_no and a.account_no=b.account_no（&lt; 1秒）<br />
<br />
---- 分析：<br />
<br />
---- 在第一个连接条件下，最佳查询方案是将account作外层表，card作内层表，利用card上的索引，其I/O次数可由以下公式估算为：<br />
<br />
---- 外层表account上的22541页+（外层表account的191122行*内层表card上对应外层表第一行所要查找的3页）=595907次I/O<br />
<br />
---- 在第二个连接条件下，最佳查询方案是将card作外层表，account作内层表，利用account上的索引，其I/O次数可由以下公式估算为：<br />
<br />
---- 外层表card上的1944页+（外层表card的7896行*内层表account上对应外层表每一行所要查找的4页）= 33528次I/O<br />
<br />
---- 可见，只有充份的连接条件，真正的最佳方案才会被执行。<br />
<br />
---- 总结：<br />
<br />
---- 1.多表操作在被实际执行前，查询优化器会根据连接条件，列出几组可能的连接方案并从中找出系统开销最小的最佳方案。连接条件要充份考虑带有索引的表、行数多的表；内外表的选择可由公式：外层表中的匹配行数*内层表中每一次查找的次数确定，乘积最小为最佳方案。<br />
<br />
---- 2.查看执行方案的方法-- 用set showplanon，打开showplan选项，就可以看到连接顺序、使用何种索引的信息；想看更详细的信息，需用sa角色执行dbcc(3604,310,302)。<br />
<br />
三、不可优化的where子句<br />
<br />
---- 1.例：下列SQL条件语句中的列都建有恰当的索引，但执行速度却非常慢：<br />
<br />
select * from record where substring(card_no,1,4)='5378'(13秒)<br />
<br />
select * from record where amount/30&lt; 1000（11秒）<br />
<br />
select * from record where convert(char(10),date,112)='19991201'（10秒）<br />
<br />
---- 分析：<br />
<br />
---- where子句中对列的任何操作结果都是在SQL运行时逐列计算得到的，因此它不得不进行表搜索，而没有使用该列上面的索引；如果这些结果在查询编译时就能得到，那么就可以被SQL优化器优化，使用索引，避免表搜索，因此将SQL重写成下面这样：<br />
<br />
select * from record where card_no like '5378%'（&lt; 1秒）<br />
<br />
select * from record where amount&lt; 1000*30（&lt; 1秒）<br />
<br />
select * from record where date= '1999/12/01'（&lt; 1秒）<br />
<br />
---- 你会发现SQL明显快起来！<br />
<br />
---- 2.例：表stuff有200000行，id_no上有非群集索引，请看下面这个SQL：<br />
<br />
select count(*) from stuff where id_no in('0','1')（23秒）<br />
<br />
---- 分析：<br />
<br />
----<span style="color: red;"> where条件中的'in'在逻辑上相当于'or'，所以语法分析器会将in ('0','1')转化为id_no ='0' or id_no='1'来执行。我们期望它会根据每个or子句分别查找，再将结果相加，这样可以利用id_no上的索引；但实际上（根据showplan）,它却采用了"OR策略"，即先取出满足每个or子句的行，存入临时数据库的工作表中，再建立唯一索引以去掉重复行，最后从这个临时表中计算结果。因此，实际过程没有利用id_no上索引，并且完成时间还要受tempdb数据库性能的影响</span>。<br />
<br />
---- <span style="color: red;">实践证明，表的行数越多，工作表的性能就越差，当stuff有620000行时，执行时间竟达到220秒！还不如将or子句分开</span>：<br />
<br />
select count(*) from stuff where id_no='0'<br />
<br />
select count(*) from stuff where id_no='1'<br />
<br />
---- 得到两个结果，再作一次加法合算。因为每句都使用了索引，执行时间只有3秒，在620000行下，时间也只有4秒。或者，用更好的方法，写一个简单的存储过程：<br />
<br />
create proc count_stuff as<br />
<br />
declare @a int<br />
<br />
declare @b int<br />
<br />
declare @c int<br />
<br />
declare @d char(10)<br />
<br />
begin<br />
<br />
select @a=count(*) from stuff where id_no='0'<br />
<br />
select @b=count(*) from stuff where id_no='1'<br />
<br />
end<br />
<br />
select @c=@a+@b<br />
<br />
select @d=convert(char(10),@c)<br />
<br />
print @d<br />
<br />
---- 直接算出结果，执行时间同上面一样快！<br />
<br />
---- 总结：<br />
<br />
---- 可见，所谓优化即where子句利用了索引，不可优化即发生了表扫描或额外开销。<br />
<br />
<br />
<br />
---- 1.任何对列的操作都将导致表扫描，它包括数据库函数、计算表达式等等，查询时要尽可能将操作移至等号右边。<br />
<br />
---- 2.in、or子句常会使用工作表，使索引失效；如果不产生大量重复值，可以考虑把子句拆开；拆开的子句中应该包含索引。<br />
<br />
---- 3.要善于使用存储过程，它使SQL变得更加灵活和高效。<br />
<br />
---- 从以上这些例子可以看出，SQL优化的实质就是在结果正确的前提下，用优化器可以识别的语句，充份利用索引，减少表扫描的I/O次数，尽量避免表搜索的发生。其实SQL的性能优化是一个复杂的过程，上述这些只是在应用层次的一种体现，深入研究还会<br />
<br />
涉及数据库层的资源配置、网络层的流量控制以及操作系统层的总体设计。<br />
<br />
1．合理使用索引 <br />
<br />
索引是数据库中重要的数据结构，它的根本目的就是为了提高查询效率。现在大多数的数据库产品都采用IBM最先提出的ISAM索引结构。索引的使用要恰到好处，其使用原则如下： <br />
<br />
●在经常进行连接，但是没有指定为外键的列上建立索引，而不经常连接的字段则由优化器自动生成索引。 <br />
<br />
●在频繁进行排序或分组（即进行group by或order by操作）的列上建立索引。 <br />
<br />
●在条件表达式中经常用到的不同值较多的列上建立检索，在不同值少的列上不要建立索引。<span style="color: red;">比如在雇员表的&#8220;性别&#8221;列上只有&#8220;男&#8221;与&#8220;女&#8221;两个不同值，因此就无必要建立索引。如果建立索引不但不会提高查询效率，反而会严重降低更新速度</span>。 <br />
<br />
●如果待排序的列有多个，可以在这些列上建立复合索引（compound index）。 <br />
<br />
●使用系统工具。如Informix数据库有一个tbcheck工具，可以在可疑的索引上进行检查。在一些数据库服务器上，索引可能失效或者因为频繁操作而使得读取效率降低，如果一个使用索引的查询不明不白地慢下来，可以试着用tbcheck工具检查索引的完整性，必要时进行修复。另外，当数据库表更新大量数据后，删除并重建索引可以提高查询速度。 <br />
<br />
<br />
<br />
2．避免或简化排序 <br />
<br />
应当简化或避免对大型表进行重复的排序。当能够利用索引自动以适当的次序产生输出时，优化器就避免了排序的步骤。以下是一些影响因素： <br />
<br />
●索引中不包括一个或几个待排序的列； <br />
<br />
●group by或order by子句中列的次序与索引的次序不一样； <br />
<br />
●排序的列来自不同的表。 <br />
<br />
&nbsp;&nbsp;&nbsp; 为了避免不必要的排序，就要正确地增建索引，合理地合并数据库表（尽管有时可能影响表的规范化，但相对于效率的提高是值得的）。如果排序不可避免，那么应当试图简化它，如缩小排序的列的范围等。 <br />
<br />
<br />
<br />
3．消除对大型表行数据的顺序存取 <br />
<br />
&nbsp;&nbsp;&nbsp;
在嵌套查询中，对表的顺序存取对查询效率可能产生致命的影响。比如采用顺序存取策略，一个嵌套3层的查询，如果每层都查询1000行，那么这个查询就要查询10亿行数据。避免这种情况的主要方法就是对连接的列进行索引。例如，两个表：学生表（学号、姓名、年龄&#8230;&#8230;）和选课表（学号、课程号、成绩）。如果两个表要做连接，就要在&#8220;学号&#8221;这个连接字段上建立索引。 <br />
<br />
&nbsp;&nbsp;&nbsp;
还可以使用并集来避免顺序存取。尽管在所有的检查列上都有索引，但某些形式的where子句强迫优化器使用顺序存取。下面的查询将强迫对orders表执行顺序操作： <br />
<br />
SELECT ＊ FROM orders WHERE (customer_num=104 AND order_num&gt;1001) OR order_num=1008 <br />
<br />
&nbsp;&nbsp;&nbsp;
虽然在customer_num和order_num上建有索引，但是在上面的语句中优化器还是使用顺序存取路径扫描整个表。因为这个语句要检索的是分离的行的集合，所以应该改为如下语句： <br />
<br />
SELECT ＊ FROM orders WHERE customer_num=104 AND order_num&gt;1001 <br />
<br />
UNION <br />
<br />
SELECT ＊ FROM orders WHERE order_num=1008 <br />
<br />
&nbsp;&nbsp;&nbsp;
这样就能利用索引路径处理查询。 <br />
<br />
<br />
4．避免相关子查询 <br />
<br />
&nbsp;&nbsp;&nbsp;
一个列的标签同时在主查询和where子句中的查询中出现，那么很可能当主查询中的列值改变之后，子查询必须重新查询一次。查询嵌套层次越多，效率越低，因此应当尽量避免子查询。如果子查询不可避免，那么要在子查询中过滤掉尽可能多的行。 <br />
<br />
<br />
<br />
5．避免困难的正规表达式 <br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;
MATCHES和LIKE关键字支持通配符匹配，技术上叫正规表达式。但这种匹配特别耗费时间。例如：SELECT ＊ FROM customer WHERE zipcode LIKE &#8220;98_ _ _&#8221; <br />
即使在zipcode字段上建立了索引，在这种情况下也还是采用顺序扫描的方式。如果把语句改为SELECT ＊ FROM customer WHERE zipcode &gt;&#8220;98000&#8221;，在执行查询时就会利用索引来查询，显然会大大提高速度。 <br />
&nbsp;&nbsp;&nbsp;
另外，还要避免非开始的子串。例如语句：SELECT ＊ FROM customer WHERE zipcode[2，3] &gt;&#8220;80&#8221;，在where子句中采用了非开始子串，因而这个语句也不会使用索引。<br />
<br />
<br />
<br />
6．使用临时表加速查询 <br />
<br />
&nbsp;&nbsp;&nbsp;
把表的一个子集进行排序并创建临时表，有时能加速查询。它有助于避免多重排序操作，而且在其他方面还能简化优化器的工作。例如： <br />
<br />
SELECT cust.name，rcvbles.balance，&#8230;&#8230;other columns <br />
<br />
FROM cust，rcvbles <br />
<br />
WHERE cust.customer_id = rcvlbes.customer_id <br />
<br />
AND rcvblls.balance&gt;0 <br />
<br />
AND cust.postcode&gt;&#8220;98000&#8221; <br />
<br />
ORDER BY cust.name <br />
<br />
&nbsp;&nbsp;&nbsp;
如果这个查询要被执行多次而不止一次，可以把所有未付款的客户找出来放在一个临时文件中，并按客户的名字进行排序： <br />
<br />
SELECT cust.name，rcvbles.balance，&#8230;&#8230;other columns<br />
<br />
FROM cust，rcvbles <br />
<br />
WHERE cust.customer_id = rcvlbes.customer_id <br />
<br />
AND rcvblls.balance&gt;0 <br />
<br />
ORDER BY cust.name <br />
<br />
INTO TEMP cust_with_balance <br />
<br />
然后以下面的方式在临时表中查询： <br />
<br />
SELECT ＊ FROM cust_with_balance <br />
<br />
WHERE postcode&gt;&#8220;98000&#8221; <br />
<br />
临时表中的行要比主表中的行少，而且物理顺序就是所要求的顺序，减少了磁盘I/O，所以查询工作量可以得到大幅减少。 <br />
<br />
注意：临时表创建后不会反映主表的修改。在主表中数据频繁修改的情况下，注意不要丢失数据。 <br />
<br />
<br />
<br />
7．用排序来取代非顺序存取 <br />
<br />
&nbsp;&nbsp;
非顺序磁盘存取是最慢的操作，表现在磁盘存取臂的来回移动。SQL语句隐藏了这一情况，使得我们在写应用程序时很容易写出要求存取大量非顺序页的查询。 <br />
<br />
有些时候，用数据库的排序能力来替代非顺序的存取能改进查询。 <br />
<br />
<br />
<br />
3.优化 tempdb 性能<br />
<br />
对 tempdb 数据库的物理位置和数据库选项设置的一般建议包括： <br />
<br />
使 tempdb 数据库得以按需自动扩展。这确保在执行完成前不终止查询，该查询所生成的存储在 tempdb 数据库内的中间结果集比预期大得多。将 tempdb 数据库文件的初始大小设置为合理的大小，以避免当需要更多空间时文件自动扩展。如果 tempdb 数据库扩展得过于频繁，性能会受不良影响。将文件增长增量百分比设置为合理的大小，以避免 tempdb 数据库文件按太小的值增长。如果文件增长幅度与写入 tempdb 数据库的数据量相比太小，则 tempdb 数据库可能需要始终扩展，因而将妨害性能。将 tempdb 数据库放在快速 I/O 子系统上以确保好的性能。在多个磁盘上条带化 tempdb 数据库以获得更好的性能。将 tempdb 数据库放在除用户数据库所使用的磁盘之外的磁盘上。有关更多信息，请参见扩充数据库。<br />
<br />
<br />
4.优化服务器:<br />
<br />
<br />
<br />
使用内存配置选项优化服务器性能<br />
<br />
&nbsp;&nbsp;&nbsp;
Microsoft&amp;reg; SQL Server&#8482; 2000 的内存管理组件消除了对 SQL Server 可用的内存进行手工管理的需要。SQL Server 在启动时根据操作系统和其它应用程序当前正在使用的内存量，动态确定应分配的内存量。当计算机和SQL Server 上的负荷更改时，分配的内存也随之更改。有关更多信息，请参见内存构架。<br />
<br />
<br />
<br />
下列服务器配置选项可用于配置内存使用并影响服务器性能： <br />
<br />
min server memory<br />
<br />
max server memory<br />
<br />
max worker threads<br />
<br />
index create memory<br />
<br />
<br />
<br />
min memory per query <br />
<br />
&nbsp;&nbsp;&nbsp;
min server memory 服务器配置选项可用于确保 SQL Server 在达到该值后不会释放内存。可以基于 SQL Server 的大小及活动将该配置选项设置为特定的值。如果选择设置此选项，必须为操作系统和其他程序留出足够的内存。如果操作系统没有足够的内存，会向 SQL Server 请求内存，从而导致影响 SQL Server 性能。<br />
&nbsp;&nbsp;&nbsp;
max server memory 服务器配置选项可用于：在 SQL Server 启动及运行时，指定 SQL Server 可以分配的最大内存量。如果知道有多个应用程序与 SQL Server 同时运行，而且想保障这些应用程序有足够的内存运行，可以将该配置选项设置为特定的值。如果这些其它应用程序（如 Web 服务器或电子邮件服务器）只根据需要请求内存，则 SQL Server 将根据需要给它们释放内存，因此不要设置 max server memory 服务器配置选项。然而，应用程序通常在启动时不假选择地使用可用内存，而如果需要更多内存也不请求。如果有这种行为方式的应用程序与 SQL Server 同时运行在相同的计算机上，则将 max server memory 服务器配置选项设置为特定的值，以保障应用程序所需的内存不由 SQL Server 分配出。<br />
&nbsp;&nbsp;
不要将 min server memory 和 max server memory 服务器配置选项设置为相同的值，这样做会使分配给 SQL Server 的内存量固定。动态内存分配可以随时间提供最佳的总体性能。有关更多信息，请参见服务器内存选项。<br />
&nbsp;&nbsp;&nbsp;
max worker threads 服务器配置选项可用于指定为用户连接到 SQL Server 提供支持的线程数。255 这一默认设置对一些配置可能稍微偏高，这要具体取决于并发用户数。由于每个工作线程都已分配，因此即使线程没有正在使用（因为并发连接比分配的工作线程少），可由其它操作（如高速缓冲存储器）更好地利用的内存资源也可能是未使用的。一般情况下，应将该配置值设置为并发连接数，但不能超过 32727。并发连接与用户登录连接不同。SQL Server 实例的工作线程池只需要足够大，以便为同时正在该实例中执行批处理的用户连接提供服务。如果增加工作线程的数量超过默认值，会降低服务器性能。有关更多信息，请参见max worker threads 选项。<br />
<br />
说明 当 SQL Server 运行在 Microsoft Windows98 上时，最大工作线程服务器配置选项不起作用。<br />
&nbsp;&nbsp;&nbsp;
index create memory 服务器配置选项控制创建索引时排序操作所使用的内存量。在生产系统上创建索引通常是不常执行的任务，通常调度为在非峰值时间执行的作业。因此，不常创建索引且在非峰值时间时，增加该值可提高索引创建的性能。不过，最好将 min memory per query 配置选项保持在一个较低的值，这样即使所有请求的内存都不可用，索引创建作业仍能开始。有关更多信息，请参见 index create memory 选项。<br />
&nbsp;&nbsp;&nbsp; min memory per query 服务器配置选项可用于指定分配给查询执行的最小内存量。当系统内有许多查询并发执行时，增大 min memory per query 的值有助于提高消耗大量内存的查询（如大型排序和哈希操作）的性能。不过，不要将 min memory per query 服务器配置选项设置得太高，尤其是在很忙的系统上，因为查询将不得不等到能确保占有请求的最小内存、或等到超过 query wait 服务器配置选项内所指定的值。如果可用内存比执行查询所需的指定最小内存多，则只要查询能对多出的内存加以有效的利用，就可以使用多出的内存。有关更多信息，请参见 min memory per query 选项和 query wait 选项。<br />
<br />
<br />
<br />
使用 I/O 配置选项优化服务器性能<br />
<br />
下列服务器配置选项可用于配置 I/O 的使用并影响服务器性能： <br />
<br />
<br />
<br />
recovery interval <br />
&nbsp;&nbsp;&nbsp;
recovery interval 服务器配置选项控制 Microsoft&amp;reg; SQL Server&#8482; 2000 在每个数据库内发出检查点的时间。默认情况下，SQL Server 确定执行检查点操作的最佳时间。然而，若要确定这是否为适当的设置，需要使用 Windows NT 性能监视器监视数据库文件上的磁盘写入活动。导致磁盘利用率达到 100% 的活动尖峰值会妨害性能。若更改该参数以使检查点进程较少出现，通常可以提高这种情况下的总体性能。但仍须继续监视性能以确定新值是否已对性能产生正面影响。有关更多信息，请参见recovery interval 选项。<br />
5.优化数据库文件<br />
<br />
分区<br />
&nbsp;&nbsp;&nbsp;
将数据库分区可提高其性能并易于维护。通过将一个大表拆分成更小的单个表，只访问一小部分数据的查询可以执行得更快，因为需要扫描的数据较少。而且可以更快地执行维护任务（如重建索引或备份表）。<br />
&nbsp;&nbsp;&nbsp;
实现分区操作时可以不拆分表，而将表物理地放置在个别的磁盘驱动器上。例如，将表放在某个物理驱动器上并将相关的表放在与之分离的驱动器上可提高查询性能，因为当执行涉及表之间联接的查询时，多个磁头同时读取数据。可以使用 Microsoft&amp;reg; SQL Server&#8482; 2000 文件组指定将表放置在哪些磁盘上。<br />
<br />
硬件分区<br />
&nbsp;&nbsp;&nbsp;
硬件分区将数据库设计为利用可用的硬件构架。硬件分区的示例包括： <br />
&nbsp;&nbsp;&nbsp;
允许多线程执行的多处理器，使得可以同时执行许多查询。换句话说，在多处理器上可以同时执行查询的各个组件，因此使单个查询的速度更快。例如，查询内引用的每个表可同时由不同的线程扫描。<br />
&nbsp;&nbsp;&nbsp;
RAID（独立磁盘冗余阵列）设备允许数据在多个磁盘驱动器中条带化，使更多的读/写磁头同时读取数据，因此可以更快地访问数据。在多个驱动器中条带化的表一般比存储在一个驱动器上的相同的表扫描速度要快。换句话说，将表与相关的表分开存储在不同的驱动器上可以显著提高联接那些表的查询的性能。 <br />
<br />
水平分区<br />
&nbsp;&nbsp;&nbsp;
水平分区将一个表分段为多个表，每个表包含相同数目的列和较少的行。例如，可以将一个包含十亿行的表水平分区成 12 个表，每个小表代表特定年份内一个月的数据。任何需要特定月份数据的查询只引用相应月份的表。<br />
&nbsp;&nbsp;&nbsp;
具体如何将表进行水平分区取决于如何分析数据。将表进行分区是为了使查询引用尽可能少的表。否则，查询时须使用过多的 UNION 查询来逻辑合并表，而这会削弱查询性能。有关查询水平分区的表的更多信息，请参见视图使用方案。 <br />
&nbsp;&nbsp;&nbsp;
常用的方法是根据时期/使用对数据进行水平分区。例如，一个表可能包含最近五年的数据，但是只定期访问本年度的数据。在这种情况下，可考虑将数据分区成五个表，每个表只包含一年的数据。<br />
<br />
垂直分区<br />
&nbsp;&nbsp;&nbsp;
垂直分区将一个表分段为多个表，每个表包含较少的列。垂直分区的两种类型是规范化和行拆分。<br />
规范化是个标准数据库进程，该进程从表中删除冗余列并将其放到次表中，次表按主键与外键的关系链接到主表。<br />
&nbsp;&nbsp;&nbsp;
行拆分将原始表垂直分成多个只包含较少列的表。拆分的表内的每个逻辑行与其它表内的相同逻辑行匹配。例如，联接每个拆分的表内的第十行将重新创建原始行。<br />
&nbsp;&nbsp;&nbsp;
与水平分区一样，垂直分区使查询得以扫描较少的数据，因此提高查询性能。例如有一个包含七列的表，通常只引用该表的前四列，那么将该表的后三列拆分到一个单独的表中可获得性能收益。<br />
&nbsp;&nbsp;
应谨慎考虑垂直分区操作，因为分析多个分区内的数据需要有联接表的查询，而如果分区非常大将可能影响性能。<br />
<br />
<br />
<br />
（一）深入浅出理解索引结构<br />
&nbsp;&nbsp;&nbsp;&nbsp;
实际上，您可以把<span style="color: red;">索引理解为一种特殊的目录</span>。微软的SQL SERVER提供了两种索引：聚集索引（clustered index，也称聚类索引、簇集索引）和非聚集索引（nonclustered index，也称非聚类索引、非簇集索引）。下面，我们举例来说明一下聚集索引和非聚集索引的区别：<br />
&nbsp;&nbsp;&nbsp;
其实，我们的汉语字典的正文本身就是一个聚集索引。比如，我们要查&#8220;安&#8221;字，就会很自然地翻开字典的前几页，因为&#8220;安&#8221;的拼音是&#8220;an&#8221;，而按照拼音排序汉字的字典是以英文字母&#8220;a&#8221;开头并以&#8220;z&#8221;结尾的，那么&#8220;安&#8221;字就自然地排在字典的前部。如果您翻完了所有以&#8220;a&#8221;开头的部分仍然找不到这个字，那么就说明您的字典中没有这个字；同样的，如果查&#8220;张&#8221;字，那您也会将您的字典翻到最后部分，因为&#8220;张&#8221;的拼音是&#8220;zhang&#8221;。也就是说，字典的正文部分本身就是一个目录，您不需要再去查其他目录来找到您需要找的内容。<br />
我们把这种正文内容本身就是一种按照一定规则排列的目录称为&#8220;聚集索引&#8221;。<br />
&nbsp;&nbsp;&nbsp;
如果您认识某个字，您可以快速地从自动中查到这个字。但您也可能会遇到您不认识的字，不知道它的发音，这时候，您就不能按照刚才的方法找到您要查的字，而需要去根据&#8220;偏旁部首&#8221;查到您要找的字，然后根据这个字后的页码直接翻到某页来找到您要找的字。但您结合&#8220;部首目录&#8221;和&#8220;检字表&#8221;而查到的字的排序并不是真正的正文的排序方法，比如您查&#8220;张&#8221;字，我们可以看到在查部首之后的检字表中&#8220;张&#8221;的页码是672页，检字表中&#8220;张&#8221;的上面是&#8220;驰&#8221;字，但页码却是63页，&#8220;张&#8221;的下面是&#8220;弩&#8221;字，页面是390页。很显然，这些字并不是真正的分别位于&#8220;张&#8221;字的上下方，现在您看到的连续的&#8220;驰、张、弩&#8221;三字实际上就是他们在非聚集索引中的排序，是字典正文中的字在非聚集索引中的映射。我们可以通过这种方式来找到您所需要的字，但它需要两个过程，先找到目录中的结果，然后再翻到您所需要的页码。<br />
<br />
<br />
<br />
我们把这种目录纯粹是目录，正文纯粹是正文的排序方式称为&#8220;非聚集索引&#8221;。<br />
通过以上例子，我们可以理解到什么是&#8220;聚集索引&#8221;和&#8220;非聚集索引&#8221;。<br />
<br />
<br />
<br />
进一步引申一下，我们可以很容易的理解：每个表只能有一个聚集索引，因为目录只能按照一种方法进行排序。<br />
<br />
<br />
<br />
（二）何时使用聚集索引或非聚集索引<br />
<img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/3423423423424242342141414.jpg" width="600" height="203" /><br />
&nbsp;
事实上，我们可以通过前面聚集索引和非聚集索引的定义的例子来理解上表。如：返回某范围内的数据一项。比如您的某个表有一个时间列，恰好您把聚合索引建立在了该列，这时您查询2004年1月1日至2004年10月1日之间的全部数据时，这个速度就将是很快的，因为您的这本字典正文是按日期进行排序的，聚类索引只需要找到要检索的所有数据中的开头和结尾数据即可；而不像非聚集索引，必须先查到目录中查到每一项数据对应的页码，然后再根据页码查到具体内容。<br />
<br />
<br />
<br />
（三）结合实际，谈索引使用的误区<br />
&nbsp;&nbsp;&nbsp;
理论的目的是应用。虽然我们刚才列出了何时应使用聚集索引或非聚集索引，但在实践中以上规则却很容易被忽视或不能根据实际情况进行综合分析。下面我们将根据在实践中遇到的实际问题来谈一下索引使用的误区，以便于大家掌握索引建立的方法。<br />
1、主键就是聚集索引<br />
&nbsp;&nbsp;&nbsp;
这种想法笔者认为是极端错误的，是对聚集索引的一种浪费。虽然SQL SERVER默认是在主键上建立聚集索引的。<br />
&nbsp;&nbsp;&nbsp; 通常，我们会在每个表中都建立一个ID列，以区分每条数据，并且这个ID列是自动增大的，步长一般为1。我们的这个办公自动化的实例中的列Gid就是如此。此时，如果我们将这个列设为主键，SQL SERVER会将此列默认为聚集索引。这样做有好处，就是可以让您的数据在数据库中按照ID进行物理排序，但笔者认为这样做意义不大。<br />
&nbsp;&nbsp;&nbsp;
显而易见，聚集索引的优势是很明显的，而每个表中只能有一个聚集索引的规则，这使得聚集索引变得更加珍贵。<br />
&nbsp;&nbsp;&nbsp;
从我们前面谈到的聚集索引的定义我们可以看出，使用聚集索引的最大好处就是能够根据查询要求，迅速缩小查询范围，避免全表扫描。在实际应用中，因为ID号是自动生成的，我们并不知道每条记录的ID号，所以我们很难在实践中用ID号来进行查询。这就使让ID号这个主键作为聚集索引成为一种资源浪费。其次，让每个ID号都不同的字段作为聚集索引也不符合&#8220;大数目的不同值情况下不应建立聚合索引&#8221;规则；当然，这种情况只是针对用户经常修改记录内容，特别是索引项的时候会负作用，但对于查询速度并没有影响。<br />
&nbsp;&nbsp;&nbsp;
在办公自动化系统中，无论是系统首页显示的需要用户签收的文件、会议还是用户进行文件查询等任何情况下进行数据查询都离不开字段的是&#8220;日期&#8221;还有用户本身的&#8220;用户名&#8221;。<br />
&nbsp;&nbsp;&nbsp;
通常，办公自动化的首页会显示每个用户尚未签收的文件或会议。虽然我们的where语句可以仅仅限制当前用户尚未签收的情况，但如果您的系统已建立了很长时间，并且数据量很大，那么，每次每个用户打开首页的时候都进行一次全表扫描，这样做意义是不大的，绝大多数的用户1个月前的文件都已经浏览过了，这样做只能徒增数据库的开销而已。事实上，我们完全可以让用户打开系统首页时，数据库仅仅查询这个用户近3个月来未阅览的文件，通过&#8220;日期&#8221;这个字段来限制表扫描，提高查询速度。如果您的办公自动化系统已经建立的2年，那么您的首页显示速度理论上将是原来速度8倍，甚至更快。<br />
&nbsp;&nbsp;&nbsp;
在这里之所以提到&#8220;理论上&#8221;三字，是因为如果您的聚集索引还是盲目地建在ID这个主键上时，您的查询速度是没有这么高的，即使您在&#8220;日期&#8221;这个字段上建立的索引（非聚合索引）。下面我们就来看一下在1000万条数据量的情况下各种查询的速度表现（3个月内的数据为25万条）：<br />
<br />
<br />
<br />
（1）仅在主键上建立聚集索引，并且不划分时间段：<br />
<br />
Select gid,fariqi,neibuyonghu,title from tgongwen<br />
用时：128470毫秒（即：128秒）<br />
<br />
（2）在主键上建立聚集索引，在fariq上建立非聚集索引：<br />
select gid,fariqi,neibuyonghu,title from Tgongwen<br />
where fariqi&gt; dateadd(day,-90,getdate())<br />
用时：53763毫秒（54秒）<br />
<br />
（3）将聚合索引建立在日期列（fariqi）上：<br />
select gid,fariqi,neibuyonghu,title from Tgongwen<br />
where fariqi&gt; dateadd(day,-90,getdate())<br />
用时：2423毫秒（2秒）<br />
<br />
&nbsp;&nbsp;&nbsp;
虽然每条语句提取出来的都是25万条数据，各种情况的差异却是巨大的，特别是将聚集索引建立在日期列时的差异。事实上，如果您的数据库真的有1000万容量的话，把主键建立在ID列上，就像以上的第1、2种情况，在网页上的表现就是超时，根本就无法显示。这也是我摒弃ID列作为聚集索引的一个最重要的因素。<br />
<br />
得出以上速度的方法是：在各个select语句前加：declare @d datetime<br />
set @d=getdate()<br />
并在select语句后加：<br />
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())<br />
<br />
<br />
<br />
2、只要建立索引就能显著提高查询速度<br />
&nbsp;&nbsp;&nbsp;
事实上，我们可以发现上面的例子中，第2、3条语句完全相同，且建立索引的字段也相同；不同的仅是前者在fariqi字段上建立的是非聚合索引，后者在此字段上建立的是聚合索引，但查询速度却有着天壤之别。所以，并非是在任何字段上简单地建立索引就能提高查询速度。<br />
&nbsp;&nbsp;&nbsp;
从建表的语句中，我们可以看到这个有着1000万数据的表中fariqi字段有5003个不同记录。在此字段上建立聚合索引是再合适不过了。在现实中，我们每天都会发几个文件，这几个文件的发文日期就相同，这完全符合建立聚集索引要求的：&#8220;既不能绝大多数都相同，又不能只有极少数相同&#8221;的规则。由此看来，我们建立&#8220;适当&#8221;的聚合索引对于我们提高查询速度是非常重要的。<br />
<br />
<br />
<br />
3、把所有需要提高查询速度的字段都加进聚集索引，以提高查询速度<br />
&nbsp;&nbsp;&nbsp;
上面已经谈到：在进行数据查询时都离不开字段的是&#8220;日期&#8221;还有用户本身的&#8220;用户名&#8221;。既然这两个字段都是如此的重要，我们可以把他们合并起来，建立一个复合索引（compound index）。<br />
&nbsp;&nbsp;&nbsp;
很多人认为只要把任何字段加进聚集索引，就能提高查询速度，也有人感到迷惑：如果把复合的聚集索引字段分开查询，那么查询速度会减慢吗？带着这个问题，我们来看一下以下的查询速度（结果集都是25万条数据）：（日期列fariqi首先排在复合聚集索引的起始列，用户名neibuyonghu排在后列）<br />
<br />
（1）select gid,fariqi,neibuyonghu,title from Tgongwen where fariqi&gt;'2004-5-5' <br />
<br />
查询速度：2513毫秒<br />
<br />
（2）select gid,fariqi,neibuyonghu,title from Tgongwen where fariqi&gt;'2004-5-5' and neibuyonghu='办公室'<br />
<br />
查询速度：2516毫秒<br />
<br />
（3）select gid,fariqi,neibuyonghu,title from Tgongwen where neibuyonghu='办公室'<br />
<br />
查询速度：60280毫秒<br />
<br />
&nbsp;&nbsp;&nbsp;
从以上试验中，我们可以看到如果<span style="color: red;">仅用聚集索引的起始列作为查询条件和同时用到复合聚集索引的全部列的查询速度是几乎一样的</span><span style="color: red;">，甚至比用上全部的复合索引列还要略快（在查询结果集数目一样的情况下）；而如果仅用复合聚集索引的非起始列作为查询条件的话，这个索引是不起任何作用的</span>。当然，语句1、2的查询速度一样是因为查询的条目数一样，如果复合索引的所有列都用上，而且查询结果少的话，这样就会形成&#8220;索引覆盖&#8221;，因而性能可以达到最优。同时，请记住：<span style="color: red;">无论您是否经常使用聚合索引的其他列，但其前导列一定要是使用最频繁的列</span>。 <br />
<img src ="http://www.blogjava.net/jjshcc/aggbug/329391.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-08-19 17:03 <a href="http://www.blogjava.net/jjshcc/archive/2010/08/19/329391.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用索引和统计特性来提高数据库的查询性能（转）</title><link>http://www.blogjava.net/jjshcc/archive/2010/08/18/329259.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Wed, 18 Aug 2010 11:26:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/08/18/329259.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/329259.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/08/18/329259.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/329259.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/329259.html</trackback:ping><description><![CDATA[# 基数：度量在数据集中可以存在多少个唯一值。<br />
# 密度：度量在数据集中唯一值的个数。密度通过如下方法得到：给定键值的行数除以数据表的总行数。优化器将忽略高密度的索引。<br />
# 选择率：度量对于一个特定的查询将返回查询结果的行数。选择率通过如下方法得到：查询关键字的个数除以查询得到的行数。要计算查询规划的相对成本，优化器需要一个有效的选择率来度量。<br />
<br />
&nbsp;&nbsp;&nbsp; 随着列中数据的变化，索引和列统计信息就变得没有用处了，这样将导致优化器在决定如何处理查询时达不到最优性能。因此，根据数据表中数据的变化，SQL服务器系统周期的自动更新这些统计信息。通过对这些数据的采样，这种统计信息的自动更新将使得成本降到最低，而且不需要对全部数据进行分析。<br />
<br />
最佳性能<br />
&nbsp;&nbsp;&nbsp; 在一个复杂的数据库表中设计并指定索引是一件非常棘手的任务。幸运的是，SQL服务器系统有一个内置的调节向导来帮助你建立最优的统计和索引集合。要提高数据库的查询性能，可以通过运行向导来提供一个基于脚本的建议列表。<br />
<br />
&nbsp;&nbsp;&nbsp; 对于SQL服务器查询优化器如何工作这一部分懂得越多，你就会知道对于特定的情形为什么只能用向导的建议来实现。但是，对于动态系统来说，最佳的数据库性能分析部分将需要进行周期性地更新。理解查询索引性能中的每个统计度量的真正含义将有助于你在管理决策方面有一个良好的知识基础。<br />
<br />
特别说明： <br />
&nbsp;&nbsp;&nbsp; 在微软的SQL服务器系统中，对数据库查询功能进行适当的优化需要懂得一些基本的查询索引和性能统计方面的知识。熟悉该系统的优化工作是如何实现的将有助于提高决策的正确性。<br />
<br />
&nbsp;&nbsp;&nbsp; 随着你对微软的SQL服务器数据库实现的逐渐熟悉，性能优化的需求也将进一步增加。建立一个真正实现最优查询功能的数据库环境的第一步是要懂得SQL服务器系统的优化器是如何工作的。<br />
<br />
索引 <br />
&nbsp;&nbsp;&nbsp; 虽然对于特定的查询来说，进行查询规划和性能优化可能只需要少量的成本估算与比较，也可以没有成本估算与比较，但是大多数的查询将从实现完全优化的工作中受益。<span style="color: red;">提高查询性能的最有效的方法之一就是创建一个高效率的索引。一个构架良好的索引在执行查询工作的时候可以避免出现扫描整个数据表的情况</span>。<br />
<br />
&nbsp;&nbsp;&nbsp; 在创建索引的时候，SQL服务器系统将自动度量和存储那些与索引列相关的分布状态值相对应的统计信息。这些统计信息常常被优化器用来评估查询的优化策略是否合理。<br />
<br />
有两种类型的索引：clustered索引和non-clustered索引，根据数据集合的不同，每种类型的索引都有各自独特的优点。<br />
<br />
&nbsp;&nbsp;&nbsp; <span style="color: red;">clustered索引要求数据表中数据按照顺序存储。因为数据已经排序，所以对于<span style="color: #60ff18;">查找一定范围的索引值</span>时clustered 索引是非常有效的。对于查找具有唯一索引值的行信息来说，这种类型的索引性能也优于其他类型的索引</span>。<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;"> non- clustered索引和教科书中的索引非常相似，索引在一个位置而其数据值却在另外一个位置。对于一个数据值的查询搜索来说，首先搜索non- clustered的索引，找到数据值在数据表中的位置，然后直接从这个位置得到数据。non-clustered 索引对于</span><span style="color: #60ff18;">精确匹配查询</span><span style="color: #ff0000;">是非常有用的</span>。<br />
<br />
统计学 <br />
&nbsp;&nbsp;&nbsp; 作为一种常用的规则，和大多数商业使用需求一样，索引的数量应该尽可能少，以减少与每个查询相关的处理过程。如果要分析和优化查询的性能，首先应该度量和收集数据的统计特性。<br />
<br />
SQL服务器系统能够维护索引值的数据统计特性。如果对其进行适当的配置，对于非索引值也能够进行统计度量。<br />
<br />
对于性能优化，数据库管理员应该懂得几个基本的统计概念，这些概念的定义如下：<br />
<br />
更新索引统计<br />
&nbsp;&nbsp; 分布页面并不是每次一个记录更新时都要进行更新.在大型数据库中,这会导致巨大的性能损失.因此,当用户初始创建一个空表时,分布页面仍是空的.它仅在发生如下情况时才被更新:<br />
1.用户在一个已存在数据表上创建一个索引.<br />
2.用户进行了update satatic语句<br />
从系统管理员角度来看,用户应该创建一个工具来自动地更新分布页面.自动更新应该至少每周一次,如果数据量每天增加10%以上则应每天一次.<br />
因为不可能每天都添加索引,用户需要使用update statistics语句更新分布页面,用以优化SQLserver.<br />
<br />
UPDATE STATISTICS <br />
在指定的表或索引视图中，对一个或多个统计组（集合）有关键值分发的信息进行更新。若要基于列生成统计，请参见 CREATE STATISTICS。 <br />
<br />
语法<br />
UPDATE STATISTICS table | view<br />
&nbsp;&nbsp;&nbsp; [ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; index<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | ( statistics_name [ ,...n ] )<br />
&nbsp;&nbsp;&nbsp; ] <br />
&nbsp;&nbsp;&nbsp; [&nbsp;&nbsp;&nbsp; WITH<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ FULLSCAN ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | SAMPLE number { PERCENT | ROWS } ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | RESAMPLE<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ] <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ [ , ] [ ALL | COLUMNS | INDEX ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ [ , ] NORECOMPUTE ] <br />
&nbsp;&nbsp;&nbsp; ] <br />
<br />
参数<br />
table | view<br />
<br />
要更新统计的表或索引视图的名称。表名和视图名必须符合标识符的规则。有关更多信息，请参见使用标识符。由于索引名在每个数据库中不唯一，所以必须指定 table 或 view。可选择指定数据库、表或视图所有者。只有在 Microsoft? SQL Server? 2000 企业版中才支持索引视图。<br />
<br />
index<br />
<br />
要更新统计的索引。索引名必须符合标识符的规则。如果未指定 index，则更新指定表或索引视图中的所有索引的分发统计。若要查看索引名和描述的列表，请带表名或视图名执行 sp_helpindex。<br />
<br />
statistics_name<br />
<br />
要更新的统计组（集合）的名称。统计名称必须符合标识符规则。有关生成统计组的更多信息，请参见 CREATE STATISTICS。<br />
<br />
n<br />
<br />
是表示可以指定多个 statistic_name 组的占位符。<br />
<br />
FULLSCAN<br />
<br />
指定应读取 table 或 view 中的所有行以收集统计。FULLSCAN 提供与 SAMPLE 100 PERCENT 相同的行为。FULLSCAN 不能与 SAMPLE 选项一起使用。<br />
<br />
SAMPLE number { PERCENT | ROWS }<br />
<br />
当为较大的表或视图收集统计时，指定要采样的表或索引视图的百分比或行数。number 只允许使用整数，无论它是 PERCENT 还是 ROWS。若要对较大的表或视图使用默认采样行为，请将 SAMPLE number 和 PERCENT 或 ROWS 一起使用。Microsoft SQL Server 将确保值的采样数不低于某一数目，以保证统计有用。如果 PERCENT、ROWS 或 number 选项导致要采样的行数过小，SQL Server 则自动根据表或视图中的现有行数改正采样。<br />
<br />
说明&nbsp; 默认行为是在目标表或索引视图上进行采样扫描。SQL Server 自动计算所需的样本大小。<br />
<br />
<br />
RESAMPLE<br />
<br />
指定使用从所有现有统计（包括索引）继承的采样速率来收集统计。如果采样速率导致要采样的行过少，SQL Server 则自动根据表或视图中的现有行数改正采样。<br />
<br />
ALL | COLUMNS | INDEX<br />
<br />
指定 UPDATE STATISTICS 语句是否影响列统计、索引统计或所有现有统计。如果未指定选项，则 UPDATE STATISTICS 语句影响所有的统计。每个 UPDATE STATISTICS 语句只能指定一种类型（ALL、COLUMNS 或 INDEX）。 <br />
<br />
NORECOMPUTE<br />
<br />
指定过期统计不自动重新计算。统计过期与否取决于在索引列上进行的 INSERT、UPDATE 和 DELETE 操作的数量。指定该选项时，将导致 SQL Server 禁用自动统计重建功能。若要还原自动统计重新计算，请重新执行 UPDATE STATISTICS（不要 NORECOMPUTE 选项），或者执行 sp_autostats。<br />
<br />
重要&nbsp; 禁用自动统计重新计算会导致 SQL Server 查询优化器对于涉及指定表的查询选择非最佳的策略。<br />
<br />
<br />
注释<br />
SQL Server 保留每个索引中关于键值分发的统计，并且使用这些统计来决定查询处理中使用哪个（或哪些）索引。用户可以通过使用 CREATE STATISTICS 语句生成基于非索引列的统计。查询优化依赖于分发步骤的准确性： <br />
<br />
如果索引中的键值有显著变化，请对此索引重新运行 UPDATE STATISTICS。<br />
<br />
<br />
如果索引列中添加、更改或删除大量数据（即如果键值分发更改），或者用 TRUNCATE TABLE 语句将表截断然后重新填充，请使用 UPDATE STATISTICS。 <br />
若要查看统计最近一次更新的时间，请使用 STATS_DATE 函数。<br />
<br />
只有当能够在计算列上创建索引时，才可以在包含这些计算列的表上创建或更新统计。有关在计算列上创建索引的要求和限制的更多信息，请参见 CREATE INDEX。<br />
<br />
权限<br />
UPDATE STATISTICS 权限默认授予表或视图的所有者，并且该权限不可转让。<br />
<br />
示例<br />
A. 更新单个表的所有统计<br />
本示例更新表 authors 上的所有索引分发统计。<br />
<br />
UPDATE STATISTICS authors<br />
<br />
B. 仅更新单一索引的统计<br />
本示例仅更新表 authors 的索引 au_id_ind 的分发信息。 <br />
<br />
UPDATE STATISTICS authors au_id_ind<br />
<br />
C. 使用 50% 采样更新特定统计组（集合）的统计<br />
本示例首先创建表 authors 中 au_lname 列和 au_fname 列的统计组，然后对其进行更新。<br />
<br />
CREATE STATISTICS anames <br />
&nbsp;&nbsp; ON authors (au_lname, au_fname)<br />
&nbsp;&nbsp; WITH SAMPLE 50 PERCENT<br />
GO<br />
-- Time passes. The UPDATE STATISTICS statement is then executed.<br />
UPDATE STATISTICS authors(anames) <br />
&nbsp;&nbsp; WITH SAMPLE 50 PERCENT<br />
GO<br />
<br />
D. 使用 FULLSCAN 和 NORECOMPUTE 更新特定统计组（集合）的统计<br />
本示例更新表 authors 中的 anames 统计组（集合），强制对表 authors 中的所有行进行完全扫描，并且关闭该统计组（集合）的自动统计更新。<br />
<br />
UPDATE STATISTICS authors(anames)<br />
&nbsp;&nbsp; WITH FULLSCAN, NORECOMPUTE<br />
<br />
sp_updatestats对当前数据库中所有用户定义的表运行 UPDATE STATISTICS。<br />
<br />
语法<br />
sp_updatestats [[@resample =] ''resample'']<br />
<br />
返回代码值<br />
0（成功）或 1（失败）<br />
<br />
参数<br />
[@resample =] ''resample''<br />
<br />
指定 sp_updatestats 将使用 UPDATE STATISTICS 命令的 RESAMPLE 选项。新统计表将继承旧统计表的采样比率。如果未指定 ''resample''，则 sp_updatestats 使用默认采样更新统计表。该参数的数据类型为 varchar(8)，默认值为 ''NO''。<br />
<br />
注释<br />
sp_updatestats 会显示表示其进度的消息。完成更新之后，该存储过程将报告已为所有的表更新了统计信息。 <br />
<br />
权限<br />
只有 DBO 和 sysadmin 固定服务器角色的成员才能执行该过程。<br />
<br />
示例<br />
下例为数据库 pubs 中的表更新统计信息。<br />
<br />
USE pubs<br />
EXEC sp_updatestats <br />
<br />
<br />
Sqlserver7 编程技术内幕提供的方法.<br />
<br />
drop proc pr_updateindex<br />
create proc pr_updateindex<br />
as<br />
&nbsp;set nocount on <br />
&nbsp;declare get_index_curs cursor <br />
&nbsp; for select name--tablename<br />
&nbsp; from sysobjects --systemtable<br />
&nbsp;where type=''u'' -usertable<br />
<br />
&nbsp;declare @holdtable varchar(30)<br />
&nbsp;declare @message varchar(40)<br />
&nbsp;declare @dynamic varchar(51)<br />
<br />
&nbsp;open getindex_curs<br />
&nbsp;fetch next from getindex_curs into @holdtable<br />
&nbsp;while @@fetch_status=0<br />
&nbsp;begin<br />
&nbsp;&nbsp; select @dynamic=''update statistics ''+@holdtable<br />
&nbsp;&nbsp; select @message=''updating''+@holdtable<br />
&nbsp;&nbsp; exec(@dynamic)<br />
&nbsp; print @message<br />
&nbsp; fetch next from getindex_curs into @holdtable<br />
end<br />
&nbsp; close getindex_curs<br />
<br />
<br />
<br />
<br />
Copyright (C) 2003 Cameron Michelis copying and redistribution of this file is permitted provided <br />
this notice and the above comments are preserved.<br />
*/<br />
<br />
Set quoted_identifier off<br />
use master<br />
DECLARE @fillfactor varchar(2)<br />
DECLARE @tablename varchar(30)<br />
DECLARE @tablename_header varchar(75)<br />
DECLARE @dataname varchar(30)<br />
DECLARE @dataname_header varchar(75)<br />
DECLARE datanames_cursor CURSOR FOR SELECT name FROM sysdatabases<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE name not in ('master', 'pubs', 'tempdb', 'model', 'northwind')<br />
/* Variable Initialization */<br />
&nbsp;select @fillfactor = "0" -- Set Fill factor here<br />
&nbsp;&nbsp;&nbsp;&nbsp; -- Note "0" will use original fillfactor.<br />
/* End Variable Initialization */<br />
OPEN datanames_cursor<br />
<br />
&nbsp; FETCH NEXT FROM datanames_cursor INTO @dataname<br />
<br />
&nbsp; WHILE (@@fetch_status &lt;&gt; -1)<br />
&nbsp;&nbsp;&nbsp; BEGIN<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF (@@fetch_status = -2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BEGIN<br />
&nbsp; FETCH NEXT FROM datanames_cursor INTO @dataname<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONTINUE<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END<br />
&nbsp;SELECT @dataname_header = "Database " + RTRIM(UPPER(@dataname))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PRINT " "<br />
&nbsp;PRINT @dataname_header<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PRINT " "<br />
&nbsp;EXEC ("USE " + @dataname + " DECLARE tnames_cursor CURSOR FOR SELECT name from sysobjects where type = 'U'")<br />
&nbsp;Select @dataname_header = RTRIM(UPPER(@dataname))<br />
&nbsp;Exec ("Use " + @dataname) <br />
&nbsp;OPEN tnames_cursor<br />
&nbsp; FETCH NEXT FROM tnames_cursor INTO @tablename<br />
&nbsp; WHILE (@@fetch_status &lt;&gt; -1)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BEGIN<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF (@@fetch_status = -2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp;&nbsp; BEGIN<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FETCH NEXT FROM tnames_cursor INTO @tablename<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONTINUE<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT @tablename_header = "&nbsp; Updating " + RTRIM(UPPER(@tablename))<br />
&nbsp;&nbsp;&nbsp; PRINT ""<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PRINT @tablename_header<br />
&nbsp;&nbsp;&nbsp; EXEC ("USE " + @dataname + " DBCC DBREINDEX (" + @tablename + "," + "''" + "," + @fillfactor + ")")<br />
&nbsp;&nbsp;&nbsp; EXEC ("USE " + @dataname + " UPDATE STATISTICS " + @tablename)<br />
&nbsp;&nbsp;&nbsp; FETCH NEXT FROM tnames_cursor INTO @tablename<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END<br />
&nbsp;DEALLOCATE tnames_cursor<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FETCH NEXT FROM datanames_cursor INTO @dataname<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END<br />
DEALLOCATE datanames_cursor<br />
PRINT ""<br />
PRINT " "<br />
PRINT "Indexing complete for All User Databases"<br />
<br />
SET QUOTED_IDENTIFIER OFF<br />
/* Start with master DB */<br />
USE master<br />
/* Create Variables */<br />
DECLARE &nbsp;&nbsp; &nbsp;@DBName &nbsp;&nbsp; &nbsp;CHAR(64)<br />
DECLARE &nbsp;&nbsp; &nbsp;@TableName &nbsp;&nbsp; &nbsp;CHAR(64)<br />
DECLARE&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;@FQTableName&nbsp;&nbsp; &nbsp;CHAR(64)<br />
DECLARE&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;@TempVar&nbsp;&nbsp; &nbsp;CHAR(256)<br />
/* Create DB List */<br />
DECLARE &nbsp;&nbsp; &nbsp;DBCursor &nbsp;&nbsp; &nbsp;CURSOR FOR<br />
SELECT&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;name<br />
FROM&nbsp;&nbsp; &nbsp;master..sysdatabases<br />
OPEN&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DBCursor<br />
FETCH NEXT<br />
FROM&nbsp;&nbsp; &nbsp;DBCursor<br />
INTO&nbsp;&nbsp; &nbsp;@DBName<br />
/* Create Database Loop */<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
/* Retrieve Table List */<br />
PRINT 'Retrieving Table List for DB ' + @DBName<br />
EXEC ('SELECT name AS TableName INTO ##TableNames FROM [' + @DBName + ']..sysobjects WHERE type = ''U''')<br />
/* Open Table List */<br />
DECLARE&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TableCursor &nbsp;&nbsp; &nbsp;CURSOR FOR<br />
SELECT&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TableName<br />
FROM&nbsp;&nbsp; &nbsp;##TableNames<br />
OPEN TableCursor<br />
FETCH NEXT<br />
FROM&nbsp;&nbsp; &nbsp;TableCursor<br />
INTO&nbsp;&nbsp; &nbsp;@TableName<br />
/* Create Table Loop */<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
/* Add DB Name to Table Name */<br />
SELECT @FQTableName = QUOTENAME(RTRIM(@DBName)) + '..' + QUOTENAME(RTRIM(@TableName))<br />
SELECT @TableName = RTRIM(@DBName) + '..' + RTRIM(@TableName)<br />
/* ReIndex Table */<br />
PRINT 'ReIndexing Table ' + @TableName<br />
DBCC DBREINDEX(@TableName)<br />
/* Update Statics on Table */<br />
PRINT 'Updating Statistics on Table ' + @TableName<br />
EXEC ('UPDATE STATISTICS ' + @FQTableName)<br />
/* Get Next Table Name */<br />
FETCH NEXT<br />
FROM TableCursor<br />
INTO @TableName<br />
END<br />
/* Close Table Cursor */<br />
CLOSE&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TableCursor<br />
DEALLOCATE&nbsp;&nbsp; &nbsp;TableCursor<br />
/* Remove Tempory Table */<br />
DROP TABLE ##TableNames<br />
/* Preform DB Checks */<br />
PRINT 'Preforming DB Checks on ' + @DBName<br />
DBCC CHECKDB (@DBName)<br />
/* Get Next Table Name */<br />
FETCH NEXT<br />
FROM DBCursor<br />
INTO @DBName<br />
END<br />
/* Close DB Curosor */<br />
CLOSE&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DBCursor<br />
DEALLOCATE&nbsp;&nbsp; &nbsp;DBCursor<br />
/* Finished */<br />
<br />
<img src ="http://www.blogjava.net/jjshcc/aggbug/329259.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-08-18 19:26 <a href="http://www.blogjava.net/jjshcc/archive/2010/08/18/329259.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>索引扫描与索引查找</title><link>http://www.blogjava.net/jjshcc/archive/2010/08/18/329233.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Wed, 18 Aug 2010 08:16:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/08/18/329233.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/329233.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/08/18/329233.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/329233.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/329233.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;" class="Apple-style-span"><span style="text-align: left; line-height: 21px; font-family: Arial,Helvetica,sans-serif; font-size: 12px;" class="Apple-style-span">
<p style="line-height: 1.8em ! important; margin: 14pt 0cm 14.5pt;" class="a">&nbsp;&nbsp;&nbsp;&nbsp; 扫描与查找操作均是SQL Server从表或索引中读取数据采用的迭代器，这些也是SQL Server支持的最基本的运算．几乎在每一个查询计划中都可以找到，因此理解它们的不同是很重要的，<span style="color: red;">扫描是在整张表上进行处理</span>，而<span style="color: red;">索引是在整个页级上进行处理</span>，而<span style="color: red;">查找则返回特定谓词上一个或多个范围内的数据行</span>．<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下面让我们看一个扫描的例子（这里使用Northwind数据库）<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT [OrderId] FROM [Orders] WHERE [RequiredDate] = '1998-03-26'<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在Orders表中，并不存在对RequiredDate列的索引，因此，SQL Server必须读取Orders表的每一行来估计每一行的RequiredDate谓词，如果满足该谓词条件（即找到包含&#8217;1998-03-26&#8217;的记录），则返回该行数据．<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为了最大化提升性能，SQL Server尽可能地使用扫描迭代器来估计该谓词，然而，如果该谓词过于复杂或开销过大，SQL Server或许使用别的筛选迭代器来估计．以下是WHERE关键字中的文本计划的过程：<br />
|--Clustered Index Scan(OBJECT:([Orders].[PK_Orders]),<br />
&nbsp; WHERE:([Orders].[RequiredDate]='1998-03-26'))<br />
下图描述了该操作的流程图：<span style="line-height: normal ! important; font-family: 宋体;"><br />
<img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/355374_200811151417471.jpg" width="516" border="0" height="141" /><br style="line-height: normal ! important;" />
</span></p>
<span style="line-height: normal; font-family: 宋体;"></span></span></span>&nbsp;&nbsp;&nbsp; 由于扫描表的每一行数据，不论满足与否，因此，其查询开销对表中的总记录数是均衡的<span style="color: red;">，当表中的数据很少或满足谓词的行比较多时，采用扫描操作有效</span>，如果表中<span style="color: red;">数据量比较大或满足谓词的行较少时，使用扫描将读取更多的页面或执行更多的I/O操作来获取数据，这显而不是最有效的方法</span>．&nbsp; &nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; 下面让我们看一个关于索引查找的例子，下面的例子在OrderdDate列上创建了索引：<br />
&nbsp;&nbsp; SELECT [OrderId] FROM [Orders] WHERE [OrderDate] = '1998-02-26'<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这次SQL Server能够使用索引查找来直接找到满足谓词的那些记录行，这里称该谓词为＂查找＂谓词．大多数情况下，SQL Server并不显式地估计＂查找＂谓词，而索引确保了＂查找＂操作仅返回满足的数据行，以下是＂查找＂谓词的文本计划：<br />
<br />
|--Index Seek(OBJECT:([Orders].[OrderDate]),<br />
<br />
&nbsp; SEEK:([Orders].[OrderDate]=CONVERT_IMPLICIT(datetime,[@1],0)) ORDERED FORWARD)<br />
<br />
注意：SQL Server自动使用＠1参数替换查询文本中的参数<br />
<br />
&nbsp;&nbsp;&nbsp; 由此看来，<span style="color: red;">查找仅扫描满足该谓词的数据页</span>，其查询开销显然要比表中总记录数的开销低，因此，<span style="color: red;">对于高选择度的查询谓词操作，查找通常是最有效的策略</span>．也就是说，对于估计大表中的数据时，使用查找谓词是比较有效率的．<br />
<br />
&nbsp;&nbsp;&nbsp; SQL Server将扫描与查找进行区分，如同将在堆（无聚集索引的对象）上扫描，聚集索引上的扫描，非聚集索引上的扫描进行分区．下表说明了这些出现在的查询计划中的扫描与查找运算．<br style="line-height: normal ! important;" />
<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;" class="Apple-style-span"><span style="text-align: left; line-height: 21px; font-family: Arial,Helvetica,sans-serif; font-size: 12px;" class="Apple-style-span"><span style="line-height: normal; font-family: 宋体;"><span style="line-height: normal; font-family: 宋体;">
<table style="border: medium none; line-height: normal; border-collapse: collapse;" class="MsoNormalTable" border="1" cellpadding="0" cellspacing="0">
    <tbody style="line-height: normal;">
        <tr style="line-height: normal; height: 15.85pt;">
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal"><span style="line-height: normal ! important;"><font style="line-height: normal ! important;" face="Times New Roman" size="2">&nbsp;</font></span></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><strong style="line-height: normal ! important;"><span style="line-height: normal ! important; font-family: 宋体; font-size: 10.5pt;">扫描</span></strong></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><strong style="line-height: normal ! important;"><span style="line-height: normal ! important; font-family: 宋体; font-size: 10.5pt;">查找</span></strong></p>
            </td>
        </tr>
        <tr style="line-height: normal; height: 15.85pt;">
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal"><font style="line-height: normal ! important;" size="2"><strong style="line-height: normal ! important;"><span style="line-height: normal ! important; font-family: 宋体;">堆</span></strong></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><font style="line-height: normal ! important;" size="2"><span style="line-height: normal ! important; font-family: 宋体;">表扫描</span></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 15.85pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><span style="line-height: normal ! important;"><font style="line-height: normal ! important;" face="Times New Roman" size="2">&nbsp;</font></span></p>
            </td>
        </tr>
        <tr style="line-height: normal; height: 16.5pt;">
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal"><font style="line-height: normal ! important;" size="2"><strong style="line-height: normal ! important;"><span style="line-height: normal ! important; font-family: 宋体;">聚集索引</span></strong></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><font style="line-height: normal ! important;" size="2"><span style="line-height: normal ! important; font-family: 宋体;">聚集索引找描</span></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><font style="line-height: normal ! important;" size="2"><span style="line-height: normal ! important; font-family: 宋体;">聚集索引查找</span></font></p>
            </td>
        </tr>
        <tr style="line-height: normal; height: 16.5pt;">
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal"><font style="line-height: normal ! important;" size="2"><strong style="line-height: normal ! important;"><span style="line-height: normal ! important; font-family: 宋体;">非聚集索引</span></strong></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><font style="line-height: normal ! important;" size="2"><span style="line-height: normal ! important; font-family: 宋体;">索引扫描</span></font></p>
            </td>
            <td style="padding: 0cm 5.4pt; line-height: normal; background-color: transparent; width: 85pt; height: 16.5pt;" valign="top" width="113">
            <p style="text-align: center; line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal" align="center"><span style="line-height: normal ! important; font-family: 宋体;"><font style="line-height: normal ! important;" size="2">索引查找</font></span></p>
            </td>
        </tr>
    </tbody>
</table>
</span></span></span></span>可查找的谓词与覆盖列<br />
<br />
&nbsp;&nbsp;&nbsp;<span style="color: red;"> SQL Server在执行索引查找之前，它需要确定索引的键是否满足查询中的谓词，我们称该谓词为＂可查找的谓词＂，SQL Server必须确定该索引是否包含或＂覆盖＂查询中引用的列集合</span>．下面描述了如何确定哪个谓词是可查找的，哪个谓词不是可查找的，哪些列需要索引覆盖．<br />
<br />
单列索引<br />
<br />
&nbsp;&nbsp;<span style="color: red;"> 在单列索引上判断谓词是否是可查找的是很容易的，SQL Server使用单列索引来响应多数简单的比较（包括相等和不等（大于，小于等））或者更复杂的表达式</span>，如在列上运算的函数和LIKE %谓词，这些运算符将阻止SQL Server使用索引查找．<br />
<br />
例如，假设我们在Col1列上创建了单列索引，可以在以下谓词上进行索引查找：<br />
<br />
&#216; [Col1] = 3.14<br />
<br />
&#216; [Col1] &gt; 100<br />
<br />
&#216; [Col1] BETWEEN 0 AND 99<br />
<br />
&#216; [Col1] LIKE 'abc%'<br />
<br />
&#216; [Col1] IN (2, 3, 5, 7)<br />
<br />
然页，在以下谓词上将不能使用索引查找：<br />
<br />
&#216; ABS([Col1]) = 1<br />
<br />
&#216; [Col1] + 1 = 9<br />
<br />
&#216; [Col1] LIKE '%abc'<br />
<br />
下面我通过一些例子来介绍单列索引：<br />
<br />
首先创建一些架构对象：<br />
create table person<br />
(id int, last_name varchar(30), first_name varchar(30))<br />
<br />
create unique clustered index person_id<br />
on person (id)<br />
create index person_name<br />
on person (last_name, first_name)<br />
<br />
&nbsp;&nbsp;&nbsp; 以下是三个查询及其各自的文本查询计划，第一个查询在person_name索引上进行查找，第二个查询<span style="color: red;">首先在第一个键列上进行索引查找</span>，然后使用residual谓词来估计first_name,第三个查询不能使用索引查找，而是使用了索引扫描来处理residual谓词．<br />
<br />
select id from person where last_name = 'Doe' and first_name = 'John'<br />
<br />
&nbsp;|--Index Seek(OBJECT:([person].[person_name]), SEEK:([person].[last_name]='Doe' <span style="color: red;">AND</span> [person].[first_name]='John'))<br />
<br />
&nbsp;<br />
<br />
select id from person where <span style="color: red;">last_name &gt; 'Doe'</span> and first_name = 'John'<br />
<br />
&nbsp;|--Index Seek(OBJECT:([person].[person_name]), SEEK:([person].[last_name] &gt; 'Doe'),<span style="color: red;"> WHERE</span>:([person].[first_name]='John'))<br />
<br />
&nbsp;<br />
<br />
select id from person where last_name like '%oe' and first_name = 'John'<br />
<br />
&nbsp;|--Index <span style="color: red;">Scan</span>(OBJECT:([person].[person_name]), <span style="color: red;">WHERE</span>:([person].[first_name]='John'<span style="color: red;"> AND</span> [person].[last_name] like '%oe'))<br />
<br />
&nbsp;<br />
<br />
上面三条查询的图形查询计划：<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;" class="Apple-style-span"><span style="text-align: left; line-height: 21px; font-family: Arial,Helvetica,sans-serif; font-size: 12px;" class="Apple-style-span"><span style="line-height: normal; font-family: 宋体;"><span style="line-height: normal; font-family: 宋体;">
<p style="line-height: 1.8em ! important; margin: 0cm 0cm 0pt;" class="MsoNormal"><span style="line-height: normal ! important; font-family: 宋体;">
<span style="line-height: normal ! important; font-family: 宋体;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/355374_200811151420481.jpg" width="642" border="0" height="380" /></span></span></p>
</span></span></span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/329233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-08-18 16:16 <a href="http://www.blogjava.net/jjshcc/archive/2010/08/18/329233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL 2005使用正则表达式</title><link>http://www.blogjava.net/jjshcc/archive/2010/07/27/327191.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Tue, 27 Jul 2010 02:24:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/07/27/327191.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/327191.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/07/27/327191.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/327191.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/327191.html</trackback:ping><description><![CDATA[<span style="border-collapse: separate; color: #000000; font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 12px;">
<div style="padding-left: 3px; font-weight: bolder; font-size: 13px; padding-bottom: 3px; padding-top: 3px; border-bottom: 1px solid #dcdcdc; background-color: #f5f5f5;"><a id="ctl03_TitleUrl" href="http://www.cnblogs.com/MaoBisheng/archive/2010/03/17/1688464.html" style="color: navy; text-decoration: none;">SQL 2005使用正则表达式</a></div>
<div style="padding: 10px 10px 5px 3px; font-size: 13px; line-height: 1.4; margin-left: 5px;">
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">SQL Server 2005支持用CLR语言（C# .NET、VB.NET）编写过程、触发器和函数，因此使得正则匹配，数据提取能够在SQL中灵活运用，大大提高了SQL处理字符串，文本等内容的灵活性及高效性。</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">操作步骤：</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">1.新建一个SQL Server项目（输入用户名，密码，选择DB），新建好后，可以在属性中更改的</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">2.新建一个类&#8220;RegexMatch.cs&#8221;，选择用户定义的函数</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">可以看到，该类为一个部分类：public partial class UserDefinedFunctions</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">现在可以在该类中写方法了，注意方法的属性为：[Microsoft.SqlServer.Server.SqlFunction]</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">现在类中增加以下两个方法：</p>
<br />
<div onclick="cnblogs_code_show('c1ca1967-18c7-425b-8676-012dfbe337c1')" style="background-color: #f5f5f5; font-family: 'Courier New'; font-size: 13px; border: 1px solid #cccccc; padding: 5px; overflow: auto;"><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_c1ca1967-18c7-425b-8676-012dfbe337c1" onclick="cnblogs_code_hide('c1ca1967-18c7-425b-8676-012dfbe337c1',event)" style="border-width: 0px; vertical-align: middle; padding-right: 5px;"  alt="" /><span style="line-height: 15px; font-size: 13px; border: 1px solid #808080; background-color: #ffffff; padding: 2px; font-family: 'Courier New';">是否匹配正则表达式</span>
<div id="cnblogs_code_open_c1ca1967-18c7-425b-8676-012dfbe337c1">
<div><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;summary&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">&nbsp;是否匹配正则表达式<br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/summary&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="input"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">输入的字符串</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="pattern"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">正则表达式</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="ignoreCase"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">是否忽略大小写</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">[Microsoft.SqlServer.Server.SqlFunction]<br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">public</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">static</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">bool</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexMatch(</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;input,&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;pattern,&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">bool</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;ignoreCase)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">bool</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;isMatch&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">false</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">if</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">!</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.IsNullOrEmpty(input)&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&amp;&amp;</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">!</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.IsNullOrEmpty(pattern))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">try</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Match&nbsp;match&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">null</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">if</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(ignoreCase)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;match&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;Regex.Match(input,&nbsp;pattern,&nbsp;RegexOptions.Multiline&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">|</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexOptions.IgnoreCase&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">|</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexOptions.Compiled);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">else</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;match&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;Regex.Match(input,&nbsp;pattern,&nbsp;RegexOptions.Multiline&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">|</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexOptions.Compiled);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">if</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(match.Success)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isMatch&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">true</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">catch</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;{&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">return</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;isMatch;<br />
}</span></div>
</div>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><br />
</p>
<div onclick="cnblogs_code_show('4fb05ded-648f-4d72-947b-3db62182e4df')" style="background-color: #f5f5f5; font-family: 'Courier New'; font-size: 13px; border: 1px solid #cccccc; padding: 5px; overflow: auto;"><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_4fb05ded-648f-4d72-947b-3db62182e4df" onclick="cnblogs_code_hide('4fb05ded-648f-4d72-947b-3db62182e4df',event)" style="border-width: 0px; vertical-align: middle; padding-right: 5px;"  alt="" /><span style="line-height: 15px; font-size: 13px; border: 1px solid #808080; background-color: #ffffff; padding: 2px; font-family: 'Courier New';">获取正则表达式分组中的字符</span>
<div id="cnblogs_code_open_4fb05ded-648f-4d72-947b-3db62182e4df">
<div><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;summary&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">&nbsp;获取正则表达式分组中的字符<br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/summary&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="input"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">输入的字符串</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="pattern"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">正则表达式</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="groupId"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">分组的位置</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;param&nbsp;name="maxReturnLength"&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;">返回字符的最大长度</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;/param&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #008000;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">///</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #808080;"><br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">[Microsoft.SqlServer.Server.SqlFunction]<br />
</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">public</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">static</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;GetRegexMatchGroups(</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;input,&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;pattern,&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">int</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;groupId,&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">int</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;maxReturnLength)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;strReturn&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.Empty;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">if</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">!</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.IsNullOrEmpty(input)&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&amp;&amp;</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">!</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.IsNullOrEmpty(pattern))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">try</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Match&nbsp;match&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;Regex.Match(input,&nbsp;pattern,&nbsp;RegexOptions.Multiline&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">|</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexOptions.IgnoreCase&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">|</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;RegexOptions.Compiled);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">if</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(match.Success&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&amp;&amp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(groupId&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&lt;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;match.Groups.Count))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strReturn&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;match.Groups[groupId].Value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strReturn&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;(strReturn.Length&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&lt;=</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;maxReturnLength)&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">?</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;strReturn&nbsp;:&nbsp;strReturn.Substring(</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #800080;">0</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">,&nbsp;maxReturnLength);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">catch</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">return</span>&nbsp;<span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">string</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">.Empty;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">return</span><span style="line-height: 15px; font-size: 13px; font-family: 'Courier New'; color: #000000;">&nbsp;strReturn;<br />
}</span></div>
</div>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><br />
</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">3.下一步就是部署的问题了，点击项目右键--》部署即可</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">提示部署成功了，可以在数据库的标量值函数中多了这两个方法了。</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">使用方法和正常的SQL函数一样：</p>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><a target="_blank" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=select&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99" style="color: #0000ff; text-decoration: none;">select</a> dbo.RegexMatch('<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/Book/103.aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/book/("d+).aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">true</span>')<br />
消息 6263，级别 16，状态 1，第 1 行<br />
禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项。</pre>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">——出现此错误，配置下：</p>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;">exec sp_configure 'clr enabled',1;<br />
reconfigure with <span style="line-height: 15px; font-size: 13px; color: #0000ff;">override</span>;</pre>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><strong><font size="4">是否匹配：</font></strong></p>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><a target="_blank" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=select&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99" style="color: #0000ff; text-decoration: none;">select</a> dbo.RegexMatch('<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/Book/103.aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/book/("d+).aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">true</span>')</pre>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">返回1，表示匹配成功</p>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><a target="_blank" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=select&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99" style="color: #0000ff; text-decoration: none;">select</a> dbo.RegexMatch('<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/Book/103.aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/book/("d+).aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">false</span>')</pre>
</div>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;">表示0，匹配失败（不忽略大小写）。</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><strong><font size="4">数据提取：</font></strong></p>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><a target="_blank" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=select&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99" style="color: #0000ff; text-decoration: none;">select</a> dbo.GetRegexMatchGroups('<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/Book/103.aspx</span>','<span style="line-height: 15px; font-size: 13px; color: #8b0000;">/book/("d+).aspx</span>',1,50)</pre>
</div>
返回103，非常方便的提取。<br />
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><br />
注意：SQL中使用CLR时，尽量使用try catch&#8230;以免出现异常</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><br />
</p>
<p style="font-size: 13px; line-height: 1.4; margin: 5px auto; text-indent: 0px;"><span style="border-collapse: separate; color: #000000; font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 12px;">作者：<a href="http://maobisheng.cnblogs.com/" style="color: navy; text-decoration: none;">MaoBisheng</a><br />
出处：<a href="http://maobisheng.cnblogs.com/" style="color: navy; text-decoration: none;">http://maobisheng.cnblogs.com/</a>&nbsp;<br />
本文版权归作者和博客园共有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</span></span></p>
</div>
</span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/327191.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-07-27 10:24 <a href="http://www.blogjava.net/jjshcc/archive/2010/07/27/327191.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>了解SQL Server执行计划</title><link>http://www.blogjava.net/jjshcc/archive/2010/07/07/325500.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Wed, 07 Jul 2010 14:28:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/07/07/325500.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/325500.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/07/07/325500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/325500.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/325500.html</trackback:ping><description><![CDATA[<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; color: black; font-family: SimSun;">当
需要分析某个查询的效能时，最好的方式之一查看这个查询的执行计划。执行计划描述</span><span style="font-size: 9pt; color: black; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: black; font-family: SimSun;">查
询优化器如何实际运行</span><span style="font-size: 9pt; color: black; font-family: Verdana;">(</span><span style="font-size: 9pt; color: black; font-family: SimSun;">或者将会如何运行</span><span style="font-size: 9pt; color: black; font-family: Verdana;">)</span><span style="font-size: 9pt; color: black; font-family: SimSun;">一
个特定的查询。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; color: black; font-family: SimSun;">查
看查询的执行计划有几种不同的方式。它们包括：</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; font-family: SimSun;">查询分析器里有一个叫做</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示实际执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">的选项</span><span style="font-size: 9pt; font-family: Verdana;">(</span><span style="font-size: 9pt; font-family: SimSun;">位于</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">查询</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">下拉菜单中</span><span style="font-size: 9pt; font-family: Verdana;">)</span><span style="font-size: 9pt; font-family: SimSun;">。如果打开了这个选项，那么
无论何时在查询分析器中运行一个查询，都会得到一个显示在单独窗口的查询执行计划</span><span style="font-size: 9pt; font-family: Verdana;">(</span><span style="font-size: 9pt; font-family: SimSun;">以图形的格式</span><span style="font-size: 9pt; font-family: Verdana;">)</span><span style="font-size: 9pt; font-family: SimSun;">。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果只是想看下执行计划而不想
运行查询，那么可以选择</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示预估的执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">选项</span><span style="font-size: 9pt; font-family: Verdana;">(</span><span style="font-size: 9pt; font-family: SimSun;">位于</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">查询</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">下拉菜单中</span><span style="font-size: 9pt; font-family: Verdana;">)</span><span style="font-size: 9pt; font-family: SimSun;">。当选择这个选项后，执行计
划会马上显示出来</span><span style="font-size: 9pt; font-family: Verdana;">(</span><span style="font-size: 9pt; font-family: SimSun;">以图形的格式</span><span style="font-size: 9pt; font-family: Verdana;">)</span><span style="font-size: 9pt; font-family: SimSun;">。两者的不同之处在于当实际运行一个查询时，当前的服务器上的运算也会被考虑进去。大多数情况下，两种方
式产生的执行计划产生的结果是相似的。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">当建立一个</span><span style="font-size: 9pt; font-family: Verdana;">SQL Server
Profiler</span><span style="font-size: 9pt; font-family: SimSun;">追踪时，可以收集的一个事件是</span><span style="font-size: 9pt; font-family: Verdana;">MISC: Execution Plan.</span><span style="font-size: 9pt; font-family: SimSun;">这个信息</span><span style="font-size: 9pt; font-family: Verdana;">(</span><span style="font-size: 9pt; font-family: SimSun;">以文本的形式</span><span style="font-size: 9pt; font-family: Verdana;">)</span><span style="font-size: 9pt; font-family: SimSun;">显示查询优化器用来执行查询
的计行计划。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">可以在查询分析器上执行</span><span style="font-size: 9pt; font-family: Verdana;">SET
SHOWPLAN_TEXT ON</span><span style="font-size: 9pt; font-family: SimSun;">命令。这条命令被执行后，所有在当前这个查询分析器会话中执行的查询都不会运行，而是会显示一个基于文本的
执行计划。执行某条用到临时表的查询时，必须在执行查询先运行</span><span style="font-size: 9pt; font-family: Verdana;">SET STATISTICS PROFILE ON</span><span style="font-size: 9pt; font-family: SimSun;">语句。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">上面这些选项中，我更喜欢使用</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示实际执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">这个选项。它以图形的方式输
出信息，并且考虑到了当前服务器上的那些运算。</span><span style="font-size: 9pt; font-family: Verdana;">[7.0, 2000] Updated 8-5-2005</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果在执行计划中看到如下所示
的任何一项，就应该将它们视作警告信号并调查它们以找出潜在的性能问题。从性能方面来说，下面所示的每一项都是不理想的。</span></p>
<p style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; font-family: Verdana;">Index or
table scans(</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">索引或者表扫描</span></strong><strong><span style="font-size: 9pt; font-family: Verdana;">)</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">：</span></strong><span style="font-size: 9pt; font-family: SimSun;">可能意味着需要更好的或者额外
的索引。</span></p>
<p style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; font-family: Verdana;">Bookmark
Lookups(</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">书签查找</span></strong><strong><span style="font-size: 9pt; font-family: Verdana;">)</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">：</span></strong><span style="font-size: 9pt; font-family: SimSun;">考虑修改当前的聚集索引，使用复盖索引，限制</span><span style="font-size: 9pt; font-family: Verdana;">SELECT</span><span style="font-size: 9pt; font-family: SimSun;">语句中的字段数量。</span></p>
<p style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; font-family: Verdana;">Filter(</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">过滤</span></strong><strong><span style="font-size: 9pt; font-family: Verdana;">)</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">：</span></strong><span style="font-size: 9pt; font-family: SimSun;">在</span><span style="font-size: 9pt; font-family: Verdana;">WHERE</span><span style="font-size: 9pt; font-family: SimSun;">从句中移除用到的任何函数，
不要在</span><span style="font-size: 9pt; font-family: Verdana;">SQL</span><span style="font-size: 9pt; font-family: SimSun;">语句中包含视图，可能需要额外的索引。</span></p>
<p style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; font-family: Verdana;">Sort(</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">排序</span></strong><strong><span style="font-size: 9pt; font-family: Verdana;">)</span></strong><strong><span style="font-size: 9pt; font-family: SimSun;">：</span></strong><span style="font-size: 9pt; font-family: SimSun;">数据是否真的需要排序？可否
使用索引来避免排序？在客户端排序是否会更加有效率？</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">无一例外地避免这些操作是不可
能的，但是避免得越多，查询性能就会越快。</span><span style="font-size: 9pt; font-family: Verdana;">[7.0,2000,2005]</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果有在存储过程中或者其它</span><span style="font-size: 9pt; font-family: Verdana;">T-SQL</span><span style="font-size: 9pt; font-family: SimSun;">批处理代码中用到了临时表，
就不能在查询分析器或</span><span style="font-size: 9pt; font-family: Verdana;">Management Studio</span><span style="font-size: 9pt; font-family: SimSun;">使用</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示预估的执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">选项来评估查询。必须实际运
行这个存储过程或者批处理代码。这是因为使用</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示预估的执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">选项来运行一个查询时，它并没有实际被运行，临时表也没有创建。由于临时表没
有被创建，参考到临时表的代码就会失败，导致预估的执行计划不能成创建成功。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">从另一方面来说，如果使用的是
表变量而不是临时表，则可以使用</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">显示预估的执行计划</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">选项。</span><span style="font-size: 9pt; font-family: Verdana;">[7.0,2000,2005] Updated
8-5-2005</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果在查询分析器或</span><span style="font-size: 9pt; font-family: Verdana;">Management
Studio</span><span style="font-size: 9pt; font-family: SimSun;">中对一个非常复杂的查询的执行计划进行分析，可能会觉得它的执行计划既难于看懂也难于分析。那么，按照查询的逻辑将它拆分成几
个部分，然后分别对这些部分进行分析会容易得多。</span><span style="font-size: 9pt; font-family: Verdana;">[7.0,2000,2005] Updated 8-5-2005</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">图形执行计划并不总是容易读懂
和解释。查看执行计划时记住如下几点：</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">非常复杂的执行计划会被分成多
个部分，它们分别列出在屏幕上。每个部分分别代表查询优化器为了得到最终结果而必须执行的单个处理或步骤。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">执行计划的每个步骤经常会被拆
分成一个个更小的子步骤。不幸的是，它们是从右至左显示在屏幕上的。这意味着你必须滚动到图形执行计划的最右边去查看每个步骤是从哪儿开始的。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">每个步骤与子步骤间通过箭头连
接，藉此显示查询执行的路径。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">最后，查询的所有部分在屏幕顶
部的左边汇总到一起。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果将鼠标移动到任何执行计划
任何步骤或者子步骤的上面，就会显示一个弹出式窗口，上面显示该步骤或子步骤的更加详细的信息。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: SimSun;">如果将鼠标移动到连接步骤或子
步骤的箭头上，就可以看到一个弹出式窗口，上面显示有多少笔记录从一个步骤或子步骤移动到另一个步骤或子步骤。</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: 9pt; font-family: Verdana;">[7.0, 2000,
2005] Updated 8-5-2005</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">图形执行计划上连接每个图标的箭头粗细不同。</span><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">箭头的粗细表示每个图标之间移动的数据行数量以及数据行大小移动所需的相对成本。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">箭头越粗，相对成本就越高。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">可以使用这个指示器来快速测量一个查询。你可能会特别关注粗箭头以了解它如何影响到查询的效能。例如，粗线头应该在图形执行计
划的右边，而非左边。如果看到它们在左边，就意味着太多的数据行被返回，这个执行计划也不是最佳的执行计划。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0,2000,2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">执行计划的每个部分都被分配了一个成本百分比。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">它表示这个部分耗用了整个执行计划的多少资源。当对一个执行计划进行分析的时候，应该将精力集中于有着高成本百分比的那些部
分。这样就可以在有限的时间里找到可能性最大的问题，从而回报了你在时间上的投资。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0,
2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">你可能会注意到一个执行计划的某些部分被执行了不止一次。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">作为执行计划分析的一部分，应该将你的一些时间集中在任何执行了超过一次的那些部分上，看看是否有什么方式减少它们执行的次
数。执行的次数越少，查询的速度就越快。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">在执行计划中你可以看到</span><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">I/O</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">与</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">成本</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。它们没有</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">实际</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的意义，例如代
表特定资源的使用量。查询优化器使用这些数字来做出最佳选择。它们可用来参考的一个意义是，较小的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">I/O</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">或</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">成本比较大的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">I/O</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">或</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">成本使用更少的服务器资源。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查看</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">图形执行计划时，</span><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">可以查找的非常有
用的一个东西就是查询优化器如何为给定的查询使用索引来从表中获取数据。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">通过查看是否有用到索引，以及索引如何被使用，都有助于判断当前的索引是否使得查询执行得尽可能的快。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">将鼠标移到图形执行计划上的表名</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">以及它的图标</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">上面，就会弹出一个窗口，从它上面可以看到一些信息。这些信息让你知道是否有用到索引来从表中获取数据，以及它是如何使用的。
这些信息包括：</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Table Scan(</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">表扫描</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">：如果看到这个信息，就说明数据表上没有聚
集索引，或者查询优化器没有使用索引来查找。意即资料表的每一行都被检查到。如果资料表相对较小的话，表扫描可以非常快速，有时甚至快过使用索引。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">因此，当看到有执行表扫描时，第一件要做的事就是看看数据表有多少数据行。如果
不是太多的话，那么表扫描可能提供了最好的总体效能。但如果数据表大的话，表扫描就极可能需要长时间来完成，查询效能就大受影响。在这种情况下，就需要仔
细研究，为数据表增加一个适当的索引用于这个查询。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">假设你发现某查询使用了表扫描，有一个合适的非聚集索引，但它没有用到。这意味
着什么呢？为什么这个索引没有用到呢？如果需要获得的数据量相对数据表大小来说非常大，或者数据选择性不高</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">意味着同一个字段中重复的值很多</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">，表扫描经常会比索引扫描快。例如，如果一个数据表有</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">10000</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">个数据行，查询返回</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">1000</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">行，如果这个表没有聚集索引的话，那么表扫描将比使用一个非聚集索引
更快。或者如果数据表有</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">10000</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">个数据行，且同一
个字段</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(WHERE</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">条件句有用到这
个字段</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">上有</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">1000</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">笔重复的数据，表扫描也会比使用非聚集索引更快。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查看图形执行计划上的数据表上的弹出式窗口时，请注意</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">预估的资料行数</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(Estimated Row Count)&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。这个数字是查询优化器作出
的多少个数据行会被返回的最佳推测。如果执行了表扫描且</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">预估的数据行数</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">数值很高的话，就意味着返回的记录数很多，查询优化器认为执行表扫描比使用可用的非聚集索引更快。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Index Seek(</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">索引查找</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">：索引查找意味着查询优化器使用了数据表上
的非聚集索引来查找数据。性能通常会很快，尤其是当只有少数的数据行被返回时。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Clustered Index Seek(</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">聚集索引查找</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">：这指查询优化器使用了数据表上的聚集索引来查找数据，性能很快。实际上，这是</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">能做的最快的索引查找类型。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Clustered Index Scan(</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">聚集索引扫描</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">：聚集索引扫描与表扫描相似，不同的是聚集索引扫描是在一个建有聚集索引的数据表上执行的。和一般的表扫描一样，聚集索引扫描
可能表明存在效能问题。一般来说，有两种原因会引此聚集索引扫描的执行。第一个原因，相对于数据表上的整体数据行数目，可能需要获取太多的数据行。查看</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">预估的数据行数量</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(Estimated Row Count)&#8221;</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">可以对此加以验证。第二个原
因，可能是由于</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">WHERE</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">条件句中用到的字
段选择性不高。在任何情况下，与标准的表扫描不同，聚集索引扫描并不会总是去查找数据表中的所有数据，所以聚集索引扫描一般都会比标准的表扫描要快。通常
来说，要将聚集索引扫描改成聚集索引查找，你唯一能做的是重写查询语句，让语句限制性更多，从而返回更少的数据行。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">绝大多数情况下，查询优化器会对连接进行分析，按最有效率的顺序，使用最有效率
的连接类型来对数据表进行连接。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">但并不总是如此。在图形执行计划中你可以看
到代表查询所使用到的各种不同连接类型的图标。此外，每个连接图标都有两个箭头指向它。指向连接图标的上面的箭头代表该连接的外部表，下面的箭头则代表这
个连接的内部表。箭头的另一头则指向被连接的数据表名。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">有时在多表连接的查询中，箭头的另一头指向的并不是一个数据表，而是另一个连接。如果将鼠标移到指向外部连接与内部连接的箭头
上，就可以看到一个弹出式窗口，告诉你有多少数据行被发送至这个连接来进行处理。外部表应该总是比内部表含有更少的数据行。如果不是，则说明查询优化器所
选择的连接顺序可能不正确</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">下面是关于这个话题的更多
信息</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">首先，让我们来看看连接类型。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">可以使用三种不同的技术来连接资料表：嵌套循环</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(nested
loop)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">，散列</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(hash)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">，以及合并</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(merge)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。一般来说，嵌套循环是最快的连接类型，但如果不可能使用嵌套循环的话，则会用到散列或者合并作为合适的连接类型。两者都比嵌
套循环连接慢。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">当连接大表时，则合并连接可能是最佳选项，而非嵌套循环连接。唯一的明确这一点的方式是对两者都进行测试以查看哪一个最有效
率。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果你怀疑某个查询速度慢的原因可能是因为它所使用的连接类型不理想，那么你可以使用连接提示来复盖查询优化器的选择。在使用
连接提示之前，你需要花费一些时间去了解一下每种连接类型以及它们的工作方式。这是一个复杂的话题，超出了本文的讨论范围。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查询优化器选择最有效率的连接类型来连接数据表。例如，嵌套循环连接的外部表应该是连接的两个表中较小的那个表。散列连接也是
一样，它的外部表应该是较小的那个表。如果你觉得查询优化器选择的连接顺序是错误的，可以使用连接提示来复盖它。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">很多情况下，唯一的确认使用连接提示改变连接类型或连接顺序是提升还是降低了效能的方式，就是对它们进行测试，看看发生了什
么。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果你的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">有多个</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">，并且没有修改</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的默认设置来限制</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">使用服务器上所有</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的能力，那么</span><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查询优化器会考虑使用平行处
理</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(parallelism)</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">来执行某些查询</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。平行处理指在多个</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">上同时运行一个查询的能力。很多情况下，一个运行在多个处理器上的查询比仅运行在单个处理器上的查询要快，但并不总是这样。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查询优化器并不会总是使用平行处理，即使在它能使用的时候。这是因为查询优化器在决定使用平行处理前会考虑到各种不同的因素。
例如当前</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">上处于活动状态的连接数量，</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">忙碌程度，是否有足够的内存来运行平行化查询，需要处理的数据行数量，以及这个查询的类型。查询优化器收集到这些真实的数据
后，再决定平行处理是不是运行这个查询的最佳选择。你可能会发现，某次一个查询没有用到平行处理，但稍后某次再次运行同样的查询时，却又用到了平行处理。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">有时，使用多个处理器所需的花费会大于使用它们能所能节省的资源。尽管查询处理器的确会衡量使用平行查询的正反两面的影响，但
它的猜想并不总是正确的。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果怀疑平行处理防碍了某条查询的性能，你可以使用</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">OPTION
(MAXDOP 1)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示来关闭该查询的平行处理。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">决定是否使用平行处理的唯一方式是通过这两种方式对查询进行测试，看看发生了什么。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查看图形执行计划时，你可能会发现某个图标的文字用红色显示，</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">而非通常情况下的黑色。这意味着相关的表的一些统计数据遗失，统计数据是查询优化器生成一个好的执行计划所必须的。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; font-family: SimSun;">遗失的统计数据可以通过右键
这个图标，并选择</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">创建遗失的统计资料</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">来创建。这时会弹出</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">创建遗失的统计数据</span><span style="font-size: 9pt; font-family: Verdana;">&#8221;</span><span style="font-size: 9pt; font-family: SimSun;">对话框，通过它可以很容易地
创建遗失的统计数据。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; font-family: SimSun;">当可以选择去更新遗失的统计
资料时，应该总是这样做，因为这样极有可能让你正在分析的查询语句从中获得效能上的好处。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0,
2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">有时你会在图形执行计划上看到标识了</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;Assert&#8221;</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的图标。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">这意味着查询优化器正在验证查询语句是否有违反引用完整性或者条件约束。如果没有，则没有问题。但如果有的话，查询优化器将无
法为该查询建立执行计划，同时会产生一个错误。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">你常常会在图形执行计划上看到标识成</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&#8221;</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">书签查找</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(Bookmark Lookup)&#8221;</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的图标。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">书签查找相当常见。书签查找的本质是告诉你查询处理器必须从数据表或者聚集索引中来查找它所需要的数据行，而不是从非聚集索引
中直接读取。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">打比方说，如果一个查询语句的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SELECT,JOIN</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">以及</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">WHERE</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">子句中的所有字段，都不存在于那个用来定位符合查询条件的数据行的非聚集索引中，那么查询优化器就不得不做额外的工作在数据表
或聚集索引中查找那些满足这个查询语句的字段。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">另一种引起书签查找的原因是使用了</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SELECT *</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。由于在绝大多情况下它会返回比你实际所需更多的数据，所以应该永不使用</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SELECT *.</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">从性能方面来说，书签查找是不理想的。因为它会请求额外的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">I/O</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">开销在字段中查找以返回所需的数据行。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果认为</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">书签查找防碍了查询的性能，那么有四种选择可以用来避免它：可以建立</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">WHERE</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">子句会用到的聚集索引，利用索引交集的优势，建立覆盖的非聚集索
引，或者</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果是</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server 2000/2005</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">企业版的话</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">可以建立索引视图。如果这些都不可能，或者使用它们中的任何一个都会
耗用比书签查找更多的资源，那么书签查找就是最佳的选择了。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">有时查询优化器需要在</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">tempdb</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">数据库中建立临时工作表。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果是这样的话，就意味着图形执行计划中有标识成</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Index Spool,
Row Count Spool</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">或者</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Table
Spool</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">的图标。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">任何时候，使用到工作表一般都会防碍到性能，因为需要额外的</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">I/O</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">开销来维护这个工作表。理想情况下应该不要用到工作表。不幸的是并不能总是避免用到工作表。有时当使用工作表比其它选择更有效
率时，它的使用实际上会增强性能。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">不论何种情况，图形执行计划中的工作表都应该引起你的警觉。应该仔细检查这样的查询语句，看看是否有办法重写查询来避免用到工
作表。有可能没有办法。但如果有的话，你就朝提升这个查询的性能方面前进了一步。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0,
2000, 2005]</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">在图形执行计划上看到流聚合</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">(</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Stream Aggregate</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">)</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">图标就意味着有对
一个单一的输入进行了聚合。</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">当使用了</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">DISTINCT</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">子句，或者任何聚合函数时，如</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">AVG, COUNT, MAX, MIN,</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">或者</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SUM</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">等，流聚合操作就相当常见。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">&nbsp;</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">查询分析器与</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Management Studio</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">不是唯一的可以生成</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">、</span><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">显示查询执行计划的工具。</span></strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server Profiler</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">也可以显示执行计划，但格式
是文本形式的。使用</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">SQL Server Profiler</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">来显示执行计划的一个优势是，它能为实际运行的大量查询产生执行计划。如果使用查询分析器和</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Management Studio</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">，则一次只能运行一个。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">使用</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Profiler</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">捕获</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">、显示执行计划时，必须使用如下的配置生成一个追踪：</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;"><span style="color: maroon; font-family: SimSun;">捕获事件</span></span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Performance: Execution Plan</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Performance: Show Plan All</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Performance: Show Plan Statistics</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Performance: Show Plan Text</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;"><span style="color: maroon; font-family: SimSun;">显示的字段</span></span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">StartTime</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Duration</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">TextData</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">CPU</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Reads</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Writes</span></p>
<p style="margin: 0cm 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;"><span style="color: maroon; font-family: SimSun;">过滤条件</span></span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: 10pt; color: #333333; font-family: Symbol;">&#183;</span><span style="font-size: 7pt; color: #333333;"><span style="font-family: Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">Duration</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">。你会想指定最大的查询执行时间，例如</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">5</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">秒钟，由此避免得到太大量的数据。</span></p>
<p style="margin: 15pt 0cm 0pt 42pt; text-indent: -18pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">当然，你可以在你的追踪中捕获更多的没有例在上面的信息，上面例出的只是一个指南而已。但必须记住不要去捕获太多的数据，否
则，追踪的运行会影响服务器的性能。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[7.0, 2000, 2005]</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: navy; font-family: Verdana;">*****</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">如果在查询中使用了</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: Verdana;">OPTION FAST</span></strong><strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示，</span></strong><span style="font-size: 9pt; color: #333333; font-family: SimSun;">那就必须小心执行计划的结果可能不是你所期望的。这时你所看到的执行计划基于使用了</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">FAST</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示的结果，而不是整个查询语句的实际执行计划。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: Verdana;">FAST</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示用来告知果询优化器尽可能快地返回指定行数的数据行，即便这样做
会防碍查询的整体性能。使用这个提示的目的在于为使用者快速返回特定行数的记录，由此让他们产生速度非常快速的错觉。。当返回指定行数的数据行后，剩余的
数据行按照它们通常的速度返回。</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: small;">&nbsp;</span></p>
<p style="margin: 0cm 0cm 0pt; line-height: 19.2pt;"><span style="font-size: 9pt; color: #333333; font-family: SimSun;">因此，如果使用了</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">FAST</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示，那么生成的执行计划只是基于那些</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">FAST</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">返回的数据行，而非查询要返回的所有数据行。如果想看所有数据行的执行计划，那么就必须移除这个</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">FAST</span><span style="font-size: 9pt; color: #333333; font-family: SimSun;">提示。</span><span style="font-size: 9pt; color: #333333; font-family: Verdana;">[2000,2005]</span></p>
<img src ="http://www.blogjava.net/jjshcc/aggbug/325500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-07-07 22:28 <a href="http://www.blogjava.net/jjshcc/archive/2010/07/07/325500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL server2005服务器登录名、角色、数据库用户、角色、架构的关系</title><link>http://www.blogjava.net/jjshcc/archive/2010/06/29/324761.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Tue, 29 Jun 2010 04:31:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/06/29/324761.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/324761.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/06/29/324761.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/324761.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/324761.html</trackback:ping><description><![CDATA[M<span style="border-collapse: separate; color: #000000; font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: #333333; font-family: Verdana,Arial,Tahoma; font-size: 14px; line-height: 25px;">
<p style="padding: 0px; margin: 0px;">S SQL2005对2000进行了很大的改进，而用户关系这部分也变得相当复杂了，很多朋友都对此一知半解！下面，我将把我应用中总结的和大家分享下，先从概念入手，希望对不理解的朋友有点提示。</p>
<p style="padding: 0px; margin: 0px;">今天我们要说的包括服务器登录名Server Login，服务器角色Server Role，数据库用户DB User，数据库架构DB Schema，数据库角色DB Role 。以上几个名词应该从服务器与数据库来区分，服务器包含一到多个数据库，其中：</p>
<p style="padding: 0px; margin: 0px;"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; color: #008000;">服务器登录名</span></strong>，指有权限登录到某服务器的用户；</p>
<p style="padding: 0px; margin: 0px;"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; color: #008000;">服务器角色</span></strong>，指一组固定的服务器用户，默认有9组；</p>
<ul style="padding: 0px; margin: 0px; list-style-type: none;">
    <li style="padding: 0px; margin: 0px;">登录名一定属于某些角色，默认为public</li>
    <li style="padding: 0px; margin: 0px;">服务器角色不容许更改</li>
    <li style="padding: 0px; margin: 0px;">登录后也不一定有权限操作数据库</li>
</ul>
<p style="padding: 0px; margin: 0px;"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; color: #339966;">数据库用户</span></strong>，指有权限能操作数据库的用户；</p>
<p style="padding: 0px; margin: 0px;"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; color: #339966;">数据库角色</span></strong>，指一组固定的有某些权限的数据库角色；</p>
<p style="padding: 0px; margin: 0px;"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; color: #339966;">数据库架构</span></strong>，指数据库对象的容器；</p>
<ul style="padding: 0px; margin: 0px; list-style-type: none;">
    <li style="padding: 0px; margin: 0px;">数据库用户对应于服务器登录名以便登录者可以操作数据库</li>
    <li style="padding: 0px; margin: 0px;">数据库角色可以添加，可以定制不同权限　　</li>
    <li style="padding: 0px; margin: 0px;">数据库架构，类似于数据库对象的命名空间，用户通过架构访问数据库对象</li>
</ul>
<p style="padding: 0px; margin: 0px;">而通过下图可以让这些概念清晰一些：</p>
<p style="padding: 0px; margin: 0px;">　　　<img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/1216034F6-0.jpg" width="409" height="697" />　</p>
<p style="padding: 0px; margin: 0px;">即：</p>
<ol style="padding: 0px; margin: 0px;">
    <li style="padding: 0px; margin: 0px;">服务器登录名属于某组服务器角色；</li>
    <li style="padding: 0px; margin: 0px;">服务器登录名需要于数据库的用户映射后才拥有操作数据库的权限</li>
    <li style="padding: 0px; margin: 0px;">数据库用户属于某组数据库角色以获取操作数据库的权限</li>
    <li style="padding: 0px; margin: 0px;">数据库角色拥有对应的数据库架构，数据库用户可以通过角色直接拥有架构</li>
    <li style="padding: 0px; margin: 0px;">数据库用户有默认架构，写SQL语句可以直接以&#8220;对象名&#8221;访问</li>
    <li style="padding: 0px; margin: 0px;">非默认架构则要以&#8220;架构名.对象名&#8221;访问</li>
</ol>
<p style="padding: 0px; margin: 0px;">因此，新建一个非SA账户并建立数据库的过程可以如下：</p>
<p style="padding: 0px; margin: 0px;">1、新建登录名Login1<br style="padding: 0px; margin: 0px;" />
<img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/1216034558-1.jpg" width="901" height="731" /><br style="padding: 0px; margin: 0px;" />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;">2、新建数据库DB1</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="/uploads/allimg/091105/12160362S-2.jpg" style="padding: 0px; margin: 0px; border-style: none;" />&nbsp;<img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/12160362S-2.jpg" width="901" height="731" /></p>
<p style="padding: 0px; margin: 0px;">3、新建DB1的架构Schema1</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/121603D95-3.jpg" width="901" height="731" /><br />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;">4、新建BD1的用户User1，登录名对应Login1，默认架构选择Schema1，角色选择db_owner</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/1216034M8-4.jpg" width="901" height="731" /><br />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;">5、在登录名Login1的属性窗口里选择&#8220;用户映射&#8221;，勾选DB1，在用户里填写User1，默认架构选择"Schema1"</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/12160333K-5.jpg" width="901" height="731" /><br />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;">6、至此，新建表名会是Schema1.Table1，其他对象也如此</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/121603D19-6.jpg" width="531" height="357" /><br />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;"><img alt="" src="http://www.blogjava.net/images/blogjava_net/jjshcc/技术截图/12160314b-7.jpg" width="554" height="411" /><br />
&nbsp;</p>
<p style="padding: 0px; margin: 0px;">7、当然还可以新建其他架构的对象Schema2，只有User1拥有该架构，一样可以访问，如Schema2.Table2</p>
<p style="padding: 0px; margin: 0px;">值得注意的是，当为登录映射数据库用户的时候，多个数据库可以有相同名称的用户，而单独为某个数据库新建的用户，如User1，则在其他数据库里不允许同名。</p>
</span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/324761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-06-29 12:31 <a href="http://www.blogjava.net/jjshcc/archive/2010/06/29/324761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server 用户管理：用 SQL 语句创建数据库用户（SQL Server 2005）</title><link>http://www.blogjava.net/jjshcc/archive/2010/06/28/324714.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Mon, 28 Jun 2010 13:56:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/06/28/324714.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/324714.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/06/28/324714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/324714.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/324714.html</trackback:ping><description><![CDATA[<p>
要想成功访问 SQL Server 数据库中的数据，
我们需要两个方面的授权：一、获得准许连接 SQL Server 服务器的权利；
二、获得访问特定数据库中数据的权利（select, update, delete, create table ...）。
假设，我们准备建立一个 dba 数据库帐户，用来管理数据库 mydb。
</p>
<h2>
1. 首先在 SQL Server 服务器级别，创建登陆帐户（create login）
</h2>
<pre>create login dba with password='sqlstudy', default_database=mydb<br />
</pre>
<p>
登陆帐户名为：&#8220;dba&#8221;，登陆密码：&#8220;sqlstudy&#8221;，默认连接到的数据库：&#8220;mydb&#8221;。
这时候，dba 帐户就可以连接到 SQL Server 服务器上了。但是此时还不能
访问数据库中的对象（严格的说，此时 dba 帐户默认是 guest 数据库用户身份，
可以访问 guest 能够访问的数据库对象）。
</p>
<p>
要使 dba 帐户能够在 mydb 数据库中访问自己需要的对象，
需要在数据库 mydb 中建立一个&#8220;数据库用户&#8221;，赋予这个&#8220;数据库用户&#8221;
某些访问权限，并且把登陆帐户&#8220;dba&#8221; 和这个&#8220;数据库用户&#8221; 映射起来。
习惯上，&#8220;数据库用户&#8221; 的名字和 &#8220;登陆帐户&#8221;的名字相同，即：&#8220;dba&#8221;。
创建&#8220;数据库用户&#8221;和建立映射关系只需要一步即可完成：
</p>
<h2>
2. 创建数据库用户（create user）：
</h2>
<pre>create user dba for login dba with default_schema=dbo<br />
</pre>
<p>
并指定数据库用户&#8220;dba&#8221; 的默认 schema 是&#8220;dbo&#8221;。这意味着
用户&#8220;dba&#8221; 在执行&#8220;select * from t&#8221;，实际上执行的是
&#8220;select * from dbo.t&#8221;。
</p>
<h2>
3. 通过加入数据库角色，赋予数据库用户&#8220;dba&#8221;权限：
</h2>
<pre>exec sp_addrolemember 'db_owner', 'dba'<br />
</pre>
<p>
此时，dba 就可以全权管理数据库 mydb 中的对象了。
</p>
<p>
如果想让 SQL Server 登陆帐户&#8220;dba&#8221;访问多个数据库，比如 mydb2。
可以让 sa 执行下面的语句：
</p>
<pre>use mydb2<br />
go<br />
<br />
create user dba for login dba with default_schema=dbo<br />
go<br />
<br />
exec sp_addrolemember 'db_owner', 'dba'<br />
go<br />
</pre>
<p>
此时，dba 就可以有两个数据库 mydb, mydb2 的管理权限了！
</p>
<h2>
4. 禁用、启用登陆帐户：
</h2>
<pre>alter login dba disable<br />
alter login dba enable<br />
</pre>
<h2>
5. 登陆帐户改名：
</h2>
<pre>alter login dba with name=dba_tom<br />
</pre>
<p>
提示：在 SQL Server 2005 中也可以给 sa 改名。
<a target="_blank" href="http://www.sqlstudy.com/sql_article.php?id=2008061304" title="SQL
Server 2005 安全性增强：给超级用户 sa 改名">
《SQL Server 2005 安全性增强：给超级用户 sa 改名》</a>
</p>
<h2>
6. 登陆帐户改密码：
</h2>
<pre>alter login dba with password='sqlstudy.com'<br />
</pre>
<h2>
7. 数据库用户改名：
</h2>
<pre>alter user dba with name=dba_tom<br />
</pre>
<h2>
8. 更改数据库用户 defult_schema：
</h2>
<pre>alter user dba with default_schema=sales<br />
</pre>
<h2>
9. 删除数据库用户：
</h2>
<pre>drop user dba<br />
</pre>
<h2>
10. 删除 SQL Server登陆帐户：
</h2>
<pre>drop login dba<br />
</pre>
<img src ="http://www.blogjava.net/jjshcc/aggbug/324714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-06-28 21:56 <a href="http://www.blogjava.net/jjshcc/archive/2010/06/28/324714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sql语句查询当天 本周 本月记录的where条件</title><link>http://www.blogjava.net/jjshcc/archive/2010/06/08/323053.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Tue, 08 Jun 2010 06:40:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2010/06/08/323053.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/323053.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2010/06/08/323053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/323053.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/323053.html</trackback:ping><description><![CDATA[<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Simsun; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="font-family: Verdana,Arial; font-size: 12px; line-height: 18px; text-align: left;">--查询当天：&nbsp;&nbsp;&nbsp;<br />
select * from info where DateDiff(dd,datetime,getdate())=0&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;<br />
--查询24小时内的:&nbsp;&nbsp;&nbsp;<br />
select * from info where DateDiff(hh,datetime,getDate())&lt;=24&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;<br />
--info为表名,datetime为数据库中的字段值&nbsp;&nbsp;<br />
<br />
--查询当天：<br />
select * from info where DateDiff(dd,datetime,getdate())=0<br />
<br />
--查询24小时内的:<br />
select * from info where DateDiff(hh,datetime,getDate())&lt;=24<br />
<br />
--info为表名,datetime为数据库中的字段值<br />
<br />
<br />
<br />
--查询当天记录另类的方法&nbsp;&nbsp;&nbsp;<br />
Select *&nbsp;&nbsp;&nbsp;<br />
FROM j_GradeShop&nbsp;&nbsp;&nbsp;<br />
Where (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)&nbsp;&nbsp;&nbsp;<br />
orDER BY GAddTime DESC&nbsp;&nbsp;&nbsp;<br />
<br />
--查询当天记录另类的方法<br />
Select *<br />
FROM j_GradeShop<br />
Where (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)<br />
orDER BY GAddTime DESC<br />
<br />
<br />
<br />
DATEDIFF 函数:<br />
<br />
语法:<br />
<br />
<br />
DATEDIFF ( datepart , startdate , enddate )<br />
<br />
备注:<br />
<br />
enddate 减去 startdate。如果 startdate 晚于 enddate，则返回负值。<br />
<br />
如果结果超出整数值范围，则 DATEDIFF 将产生错误。对于毫秒，最大数是 24 天 20 小时 31 分钟零 23.647 秒。对于秒，最大数是 68 年。<br />
<br />
跨分钟、秒和毫秒等边界计算的方法使得 DATEDIFF 指定的结果在所有数据类型中均一致。结果是带正负号的整数值，它等于跨第一个和第二个日期间的 datepart 边界数。例如，在 1 月 4 日（星期日）和 1 月 11 日（星期日）之间的星期数是 1。<br />
<br />
<br />
<br />
可以再MSSQL中测试:<br />
Sql代码<br />
<br />
--两个时间差刚好是24&nbsp;&nbsp;&nbsp;<br />
--打印的方式&nbsp;&nbsp;&nbsp;<br />
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;<br />
--查询的方式&nbsp;&nbsp;&nbsp;<br />
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')&nbsp;&nbsp;<br />
<br />
--两个时间差刚好是24<br />
--打印的方式<br />
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')<br />
<br />
--查询的方式<br />
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')<br />
<br />
<br />
Sql代码<br />
<br />
--本月记录&nbsp;&nbsp;&nbsp;<br />
Select * FROM 表 Where datediff(month,[dateadd],getdate())=0&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;<br />
--本周记录&nbsp;&nbsp;&nbsp;<br />
Select * FROM 表 Where datediff(week,[dateadd],getdate())=0&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;<br />
--包括本年这些查询方式是一样的&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
--本月记录&nbsp;<br />
Select * FROM 表 Where datediff(month,[dateadd],getdate())=0<br />
<br />
--本周记录&nbsp;<br />
Select * FROM 表 Where datediff(week,[dateadd],getdate())=0<br />
<br />
--包括本年这些查询方式是一样的<br />
<br />
<br />
<br />
sql server中的时间函数<br />
<br />
<br />
1.&nbsp;&nbsp; 当前系统日期、时间<br />
&nbsp;&nbsp;&nbsp;&nbsp; select getdate()&nbsp;&nbsp;<br />
<br />
<br />
<br />
2. dateadd&nbsp;&nbsp; 在向指定日期加上一段时间的基础上，返回新的 datetime 值<br />
&nbsp;&nbsp;&nbsp;&nbsp;例如：向日期加上2天<br />
&nbsp;&nbsp;&nbsp;&nbsp;select dateadd(day,2,'2004-10-15')&nbsp;&nbsp; --返回：2004-10-17 00:00:00.000<br />
<br />
<br />
<br />
3. datediff 返回跨两个指定日期的日期和时间边界数。<br />
&nbsp;&nbsp;&nbsp;&nbsp;select datediff(day,'2004-09-01','2004-09-18')&nbsp;&nbsp;&nbsp;&nbsp;--返回：17<br />
<br />
<br />
<br />
4. datepart 返回代表指定日期的指定日期部分的整数。<br />
&nbsp;&nbsp; Select DATEPART(month, '2004-10-15')&nbsp;&nbsp; --返回 10<br />
<br />
<br />
<br />
5. datename 返回代表指定日期的指定日期部分的字符串<br />
&nbsp;&nbsp;&nbsp;&nbsp;Select datename(weekday, '2004-10-15')&nbsp;&nbsp; --返回：星期五<br />
<br />
<br />
<br />
6. day(), month(),year() --可以与datepart对照一下<br />
<br />
select 当前日期=convert(varchar(10),getdate(),120)<br />
,当前时间=convert(varchar(8),getdate(),114)<br />
<br />
select datename(dw,'2004-10-15')<br />
<br />
select 本年第多少周=datename(week,'2004-10-15')<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,今天是周几=datename(weekday,'2004-10-15')<br />
&nbsp;&nbsp;<br />
<br />
<br />
函数 &nbsp;&nbsp;&nbsp;&nbsp;参数/功能<br />
GetDate( ) &nbsp;&nbsp;&nbsp;&nbsp;返回系统目前的日期与时间<br />
DateDiff (interval,date1,date2) &nbsp;&nbsp;&nbsp;&nbsp;以interval 指定的方式，返回date2 与date1两个日期之间的差值 date2-date1<br />
DateAdd (interval,number,date) &nbsp;&nbsp;&nbsp;&nbsp;以interval指定的方式，加上number之后的日期<br />
DatePart (interval,date) &nbsp;&nbsp;&nbsp;&nbsp;返回日期date中，interval指定部分所对应的整数值<br />
DateName (interval,date) &nbsp;&nbsp;&nbsp;&nbsp;返回日期date中，interval指定部分所对应的字符串名称<br />
<br />
参数 interval的设定值如下：<br />
<br />
<br />
值 &nbsp;&nbsp;&nbsp;&nbsp;缩 写（Sql Server） &nbsp;&nbsp;&nbsp;&nbsp;Access 和 ASP &nbsp;&nbsp;&nbsp;&nbsp;说明<br />
Year &nbsp;&nbsp;&nbsp;&nbsp;Yy &nbsp;&nbsp;&nbsp;&nbsp;yyyy &nbsp;&nbsp;&nbsp;&nbsp;年 1753 ~ 9999<br />
Quarter &nbsp;&nbsp;&nbsp;&nbsp;Qq &nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;季 1 ~ 4<br />
Month &nbsp;&nbsp;&nbsp;&nbsp;Mm &nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;月1 ~ 12<br />
Day of year &nbsp;&nbsp;&nbsp;&nbsp;Dy &nbsp;&nbsp;&nbsp;&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;一年的日数,一年中的第几日 1-366<br />
Day &nbsp;&nbsp;&nbsp;&nbsp;Dd &nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;日，1-31<br />
Weekday &nbsp;&nbsp;&nbsp;&nbsp;Dw &nbsp;&nbsp;&nbsp;&nbsp;w &nbsp;&nbsp;&nbsp;&nbsp;一周的日数，一周中的第几日 1-7<br />
Week &nbsp;&nbsp;&nbsp;&nbsp;Wk &nbsp;&nbsp;&nbsp;&nbsp;ww &nbsp;&nbsp;&nbsp;&nbsp;周，一年中的第几周 0 ~ 51<br />
Hour &nbsp;&nbsp;&nbsp;&nbsp;Hh &nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;时0 ~ 23<br />
Minute &nbsp;&nbsp;&nbsp;&nbsp;Mi &nbsp;&nbsp;&nbsp;&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;分钟0 ~ 59<br />
Second &nbsp;&nbsp;&nbsp;&nbsp;Ss &nbsp;&nbsp;&nbsp;&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;秒 0 ~ 59<br />
Millisecond &nbsp;&nbsp;&nbsp;&nbsp;Ms &nbsp;&nbsp;&nbsp;&nbsp;- &nbsp;&nbsp;&nbsp;&nbsp;毫秒 0 ~ 999<br />
<br />
access 和 asp 中用date()和now()取得系统日期时间；其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中，这些函数的用法也类似<br />
<br />
举例：<br />
1.GetDate() 用于sql server :select GetDate()<br />
<br />
2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒<br />
DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天<br />
<br />
3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1，周六为7)<br />
DatePart('d','2005-7-25 22:56:32')返回值为 25即25号<br />
DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天<br />
DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年<br />
</span></span>
<img src ="http://www.blogjava.net/jjshcc/aggbug/323053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2010-06-08 14:40 <a href="http://www.blogjava.net/jjshcc/archive/2010/06/08/323053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>