一、索引
简介:
索引分为:聚集索引 和 非聚集索引;
聚集索引:正文内容本身就是一种按照一定规则排列的目录
非聚集索引:目录纯粹是目录,正文纯粹是正文的顺序方式
作用: 大大提高数据库的检索速度,改善数据库性能
使用原则:
1、每一个表只能创建一个聚集索引
2、每个表最多可创建249个非聚集索引
3、在经常查询的字段上建立索引
4、text、image和bit数据类型的裂伤不要建立索引
5、外键列可以建立索引
6、主键列必须建立索引
7、重复值比较多,查询较少的列上不要建立索引
使用SQL创建一个非聚集索引
IF EXISTS(SELECT * FROM sysindexes WHERE name='索引名') GO DROP INDEX 表名.索引名 CREATE NONCLUSTERED INDEX 索引名 ON 表名(字段名) WITH FILLFACTOR=30 -- 设置填充因子 GO IF EXISTS(SELECT * FROM sysindexes WHERE name='IX_UserInfo_Address') GO DROP INDEX UserInfo.IX_UserInfo_Address CREATE NONCLUSTERED INDEX IX_UserInfo_Address ON UserInfo(Address) WITH FILLFACTOR=30 GO
索引的使用
-- 如何使用索引 SELECT * FROM UserInfo WITH (INDEX =IX_UserInfo_Address) -- 指定索引 WHERE userAddress LIKE '%河南%' -- 使用系统存储过程查看索引 EXEC sp_helpindex 表名 EXEC sp_helpindex UserInfo -- 检测碎片 -- 1、一般碎片比例大于30%使用重新生成索引 -- 2、小于30%大于50% 使用重新组织索引 -- 3、小于5% 不必处理
二、约束