文档详情

第六章 创建和优化索引.doc

发布:2017-12-17约4.04千字共10页下载文档
文本预览下载声明
第六章 创建和优化索引 1.与索引有关的选项:(1)堆 (2)聚集索引 (3)非聚集索引。 2. 聚集索引 (1)在这种情况下使用聚集索引 1.唯一的或包含多个不同值的列。2.经常用于排序从表中检索的数据的列。3. 经常顺序访问的列。 (2)在这种情况下不使用聚集索引 1.索引列中的数据频繁更改。2.索引键为宽键。 3.创建索引的语法: 表: 1.创建唯一、非聚集索引 (1)create unique nonclustered index 索引名称 on 表名(列名) (2)实例:create unique nonclustered index suoyin on biao(id) (3).用索引检索表 (1) select * from biao with(index=suoyin) 检索结果如上图:(聚集索引关键词为clustered) 2.创建符合索引 (1)语法: create unique nonclustered index 索引名称on 表名(列名,列名......)create unique nonclustered index suo on biao(id,name) (3)通过索引检索: select id,name from biao with(index=suo) 检索结果: 3.碎片检测: (1)语法:selecta.index_id,name,avg_fragmentation_in_percent from sys.dm_db_index_physical_stats (db_id(数据库名称),object_id(表名),null,null,null)as a join sys.indexes as b on object_id=b.object_id and a.index_id=b.index_id (2)实例:select a.index_id,name,avg_fragmentation_in_percent from sys.dm_db_index_physical_stats (db_id(school),object_id(student),null,null,null)as a join sys.indexes as b on object_id=b.object_id and a.index_id=b.index_id (3)检索结果: (4)当碎片=30%则重新组合索引: 1.语法: Alter index 索引名 on 表名 Reorganize 2.实例: Alter index suoyin on biao Reorganize 如果要把所有的索引都重新组合则把索引名改为 all (5)当碎片30%重新生成索引: alter index 索引名称 on 表名 rebuild 4.创建全文索引: (1)创建文件组: 数据库-右击-属性-文件组-增加-起名字(默认值打对号)-文件-起名-确定。 (2)创建全文目录: 1.语法: Create fulltext catalog 全文目录名称 On filegroup 文件组名称 In path ‘路径’ as default (3)创建唯一索引 (4)创建全文索引: 1.语法: Create fulltext index on 表名(非唯一列名) Key index 唯一索引名称 On 全文目录名称 With change_tracking tuto; 第七章 实现数据完整性 1.数据完整性的类型 (1)域或列的完整性 (2)实体完整性 (3)引用完整性 2.约束分为: 1.主键约束(primary key)(2)默认约束(default)(3)检查约束(check)(4)唯一约束(unique)(5)外键约束(foreign key) 2.创建的语法: (1)primary key Alter table 表名 Add constraint 约束名称 primary key (列名) (2)default Alter table 表名 Add constraint 约束名称 default (’默认文字’)for 列名 (3)check Alter table 表名 Add constraint 约束名称 check (条件) (4)unique Alter table 表名 Add constraint 约束名称 unique (列名) (5)foreign key Alter table 表名-外键表名 Add cons
显示全部
相似文档