SQL SERVER索引使用实例

添加索引
IF EXISTS (SELECT name FROM sysindexes
          WHERE name = 'IX_writtenExam')
   DROP INDEX testtable.IX_writtenExam
/*--笔试列创建非聚集索引:填充因子为%--*/
CREATE NONCLUSTERED INDEX IX_writtenExam
     ON testtable(id)
          WITH FILLFACTOR= 30
GO

查询测试
/*-----指定按索引IX_writtenExam 查询----*/
declare @startTime datetime
SET @startTime = GETDATE();
SELECT sum(ID) FROM testtable with (INDEX=IX_writtenExam)
declare @endtime datetime
SET @endtime = GETDATE();
print datediff(ms,@startTime,@endtime)

SELECT sum(ID) FROM testtable

添加数据
SET IDENTITY_INSERT TestTable ON
declare @i int
set @i=1
while @i<=400000
begin
    insert into TestTable([id], FirstName, LastName, Country,Note) values(@i, 'FirstName_XXX','LastName_XXX','Country_XXX','Note_XXX')
    set @i=@i+1
end
SET IDENTITY_INSERT TestTable OFF

posted @ 2012-05-10 21:29  宁静.致远  阅读(378)  评论(0编辑  收藏  举报