对于sqlserver中所谈到的索引,我明白他的作用是优化查询,提高性能.
  可是大多数书上都只是说到这样的概念并没有 写一个例子,
  我自己创建了一个表,加了十几个字段,用循环加了十几万行数据,刚开始没有加索引查询是10秒,加了索引
还是10秒,好象是没有用到索引.  对于索引的概念不需要再请教,希望大家能贴个例子,运行一下就能够切实体会到索引的优点,谢了哈!

解决方案 »

  1.   

    简单的测试,性能相差10倍
    create table a(id int,b char(10))
    declare @i int,@j int,@t1 datetime,@t2 datetime,@t3 datetime,@t4 datetime
    set @i = 1
    while @i<1001
     begin 
      insert a select @i,rtrim(@i)
      set @i = @i +1
     end
    select @i = 1,@t1 = getdate()
    while @i<1001
     begin 
      select @j=id from a where id = @i
      set @i = @i+1
     end
    select @t2 = getdate()CREATE  INDEX [idindex] ON [dbo].[a]([id]) ON [PRIMARY]select @i = 1,@t3 = getdate()
    while @i<1001
     begin 
      select @j=id from a where id = @i
      set @i = @i+1
     end
    select @t4 = getdate()select rtrim(datediff(ms,@t1,@t2))+'毫秒' 无索引耗时,rtrim(datediff(ms,@t3,@t4))+'毫秒' 有索引耗时
    /*
    无索引耗时            有索引耗时            
    ---------------- ---------------- 
    493毫秒            40毫秒
    */
    drop table a