SQL Index创建例子
测试例子如下:
1.建立测试表:
CREATE Table Library ( BookId int identity (1, 1), BookName varchar(100), Dept varchar(100), SubjectId int );
2.插入数据:
DECLARE @Dept varchar(20) = 'CS' DECLARE @BookId int = 1 DECLARE @Count int = 1 WHILE @Count >= 1 and @Count <= 500000 BEGIN INSERT INTO Library(BookName, Dept, SubjectId) VALUES ( 'Software' + convert( varchar(100), @BookId ), @Dept, @BookId ) SET @BookId += 1 SET @Count += 1 END
3.开始测试,如果不加索引,不管是查找全部或通过BookId查找或通过BookName查找,结果需要的开销都是2.41328,如下所示:
1)通过BookId查找
2)通过BookName查找
4.建立索引后,这时再执行上面的通过BookName查找,发现需要的开销变为0.0032831
Create Index Index_BookName On Library (BookName)
额外说明:哪里可以看到这个执行计划?
答:在菜单“查询”-》显示估计的执行计划
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/keeplearningandsharing/p/15875271.html